Skip to content

Commit df2e53b

Browse files
More tslint fixes. (microsoft#4878)
(for microsoft#611)
1 parent 99de007 commit df2e53b

33 files changed

Lines changed: 293 additions & 132 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,4 @@ obj/**
2828
tmp/**
2929
.python-version
3030
.vs/
31+
!build/

build/constants.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,9 @@
22
// Licensed under the MIT License.
33
'use strict';
44
Object.defineProperty(exports, "__esModule", { value: true });
5-
const fs = require("fs");
6-
const path = require("path");
7-
exports.ExtensionRootDir = path.join(__dirname, '..');
8-
const jsonFileWithListOfOldFiles = path.join(__dirname, 'existingFiles.json');
9-
function getListOfExcludedFiles() {
10-
const files = JSON.parse(fs.readFileSync(jsonFileWithListOfOldFiles).toString());
11-
return files.map(file => path.join(exports.ExtensionRootDir, file.replace(/\//g, path.sep)));
12-
}
13-
exports.filesNotToCheck = getListOfExcludedFiles();
5+
const util = require("./util");
6+
exports.ExtensionRootDir = util.ExtensionRootDir;
7+
// This is a list of files that existed before MS got the extension.
8+
exports.existingFiles = util.getListOfFiles('existingFiles.json');
9+
exports.contributedFiles = util.getListOfFiles('contributedFiles.json');
1410
exports.isCI = process.env.TRAVIS === 'true' || process.env.TF_BUILD !== undefined;

build/constants.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,12 @@
33

44
'use strict';
55

6-
import * as fs from 'fs';
7-
import * as path from 'path';
6+
import * as util from './util';
87

9-
export const ExtensionRootDir = path.join(__dirname, '..');
8+
export const ExtensionRootDir = util.ExtensionRootDir;
109

11-
const jsonFileWithListOfOldFiles = path.join(__dirname, 'existingFiles.json');
12-
function getListOfExcludedFiles() {
13-
const files = JSON.parse(fs.readFileSync(jsonFileWithListOfOldFiles).toString()) as string[];
14-
return files.map(file => path.join(ExtensionRootDir, file.replace(/\//g, path.sep)));
15-
}
16-
17-
export const filesNotToCheck: string[] = getListOfExcludedFiles();
10+
// This is a list of files that existed before MS got the extension.
11+
export const existingFiles: string[] = util.getListOfFiles('existingFiles.json');
12+
export const contributedFiles: string[] = util.getListOfFiles('contributedFiles.json');
1813

1914
export const isCI = process.env.TRAVIS === 'true' || process.env.TF_BUILD !== undefined;

build/contributedFiles.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[
2+
]

build/tslint-rules/baseRuleWalker.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@
44
Object.defineProperty(exports, "__esModule", { value: true });
55
const path = require("path");
66
const Lint = require("tslint");
7-
const constants_1 = require("../constants");
7+
const util = require("../util");
88
class BaseRuleWalker extends Lint.RuleWalker {
9-
constructor() {
10-
super(...arguments);
11-
this.filesToIgnore = constants_1.filesNotToCheck;
12-
}
13-
sholdIgnoreCcurrentFile(node) {
9+
shouldIgnoreCurrentFile(node, filesToIgnore) {
1410
const sourceFile = node.getSourceFile();
15-
return sourceFile && sourceFile.fileName && this.filesToIgnore.indexOf(sourceFile.fileName.replace(/\//g, path.sep)) >= 0;
11+
if (sourceFile && sourceFile.fileName) {
12+
const filename = path.resolve(util.ExtensionRootDir, sourceFile.fileName);
13+
if (filesToIgnore.indexOf(filename.replace(/\//g, path.sep)) >= 0) {
14+
return true;
15+
}
16+
}
17+
return false;
1618
}
1719
}
1820
exports.BaseRuleWalker = BaseRuleWalker;

build/tslint-rules/baseRuleWalker.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,17 @@
66
import * as path from 'path';
77
import * as Lint from 'tslint';
88
import * as ts from 'typescript';
9-
import { filesNotToCheck } from '../constants';
9+
import * as util from '../util';
1010

1111
export class BaseRuleWalker extends Lint.RuleWalker {
12-
private readonly filesToIgnore = filesNotToCheck;
13-
protected sholdIgnoreCcurrentFile(node: ts.Node) {
12+
protected shouldIgnoreCurrentFile(node: ts.Node, filesToIgnore: string[]): boolean {
1413
const sourceFile = node.getSourceFile();
15-
return sourceFile && sourceFile.fileName && this.filesToIgnore.indexOf(sourceFile.fileName.replace(/\//g, path.sep)) >= 0;
14+
if (sourceFile && sourceFile.fileName) {
15+
const filename = path.resolve(util.ExtensionRootDir, sourceFile.fileName);
16+
if (filesToIgnore.indexOf(filename.replace(/\//g, path.sep)) >= 0) {
17+
return true;
18+
}
19+
}
20+
return false;
1621
}
1722
}

build/tslint-rules/copyrightAndStrictHeaderRule.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
Object.defineProperty(exports, "__esModule", { value: true });
55
const os_1 = require("os");
66
const Lint = require("tslint");
7+
const constants_1 = require("../constants");
78
const baseRuleWalker_1 = require("./baseRuleWalker");
9+
const ignoredFiles = [...constants_1.existingFiles, ...constants_1.contributedFiles];
810
const copyrightHeader = [
911
'// Copyright (c) Microsoft Corporation. All rights reserved.',
1012
'// Licensed under the MIT License.',
@@ -24,14 +26,20 @@ const allowedCopyrightHeaders = [
2426
const failureMessage = 'Header must contain either \'use strict\' or [copyright] & \'use strict\' in the Python Extension files';
2527
class NoFileWithoutCopyrightHeader extends baseRuleWalker_1.BaseRuleWalker {
2628
visitSourceFile(sourceFile) {
27-
if (!this.sholdIgnoreCcurrentFile(sourceFile)) {
29+
if (!this.shouldIgnoreCurrentFile(sourceFile)) {
2830
const sourceFileContents = sourceFile.getFullText();
2931
if (sourceFileContents) {
3032
this.validateHeader(sourceFile, sourceFileContents);
3133
}
3234
}
3335
super.visitSourceFile(sourceFile);
3436
}
37+
shouldIgnoreCurrentFile(node) {
38+
if (super.shouldIgnoreCurrentFile(node, ignoredFiles)) {
39+
return true;
40+
}
41+
return false;
42+
}
3543
validateHeader(_sourceFile, sourceFileContents) {
3644
for (const allowedHeader of allowedCopyrightHeaders) {
3745
if (sourceFileContents.startsWith(allowedHeader)) {

build/tslint-rules/copyrightAndStrictHeaderRule.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66
import { EOL } from 'os';
77
import * as Lint from 'tslint';
88
import * as ts from 'typescript';
9+
import { existingFiles, contributedFiles } from '../constants';
910
import { BaseRuleWalker } from './baseRuleWalker';
1011

12+
const ignoredFiles = [...existingFiles, ...contributedFiles];
13+
1114
const copyrightHeader = [
1215
'// Copyright (c) Microsoft Corporation. All rights reserved.',
1316
'// Licensed under the MIT License.',
@@ -28,7 +31,7 @@ const failureMessage = 'Header must contain either \'use strict\' or [copyright]
2831

2932
class NoFileWithoutCopyrightHeader extends BaseRuleWalker {
3033
public visitSourceFile(sourceFile: ts.SourceFile) {
31-
if (!this.sholdIgnoreCcurrentFile(sourceFile)) {
34+
if (!this.shouldIgnoreCurrentFile(sourceFile)) {
3235
const sourceFileContents = sourceFile.getFullText();
3336
if (sourceFileContents) {
3437
this.validateHeader(sourceFile, sourceFileContents);
@@ -37,6 +40,12 @@ class NoFileWithoutCopyrightHeader extends BaseRuleWalker {
3740

3841
super.visitSourceFile(sourceFile);
3942
}
43+
protected shouldIgnoreCurrentFile(node: ts.Node) {
44+
if (super.shouldIgnoreCurrentFile(node, ignoredFiles)) {
45+
return true;
46+
}
47+
return false;
48+
}
4049
private validateHeader(_sourceFile: ts.SourceFile, sourceFileContents: string) {
4150
for (const allowedHeader of allowedCopyrightHeaders) {
4251
if (sourceFileContents.startsWith(allowedHeader)) {

build/tslint-rules/messagesMustBeLocalizedRule.js

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,26 @@
22
// Licensed under the MIT License.
33
'use strict';
44
Object.defineProperty(exports, "__esModule", { value: true });
5+
const path = require("path");
56
const Lint = require("tslint");
67
const ts = require("typescript");
8+
const util = require("../util");
79
const baseRuleWalker_1 = require("./baseRuleWalker");
810
const methodNames = [
9-
// From IApplicationShell (vscode.window)
11+
// From IApplicationShell (vscode.window):
1012
'showErrorMessage', 'showInformationMessage',
1113
'showWarningMessage', 'setStatusBarMessage',
12-
// From IOutputChannel (vscode.OutputChannel)
14+
// From IOutputChannel (vscode.OutputChannel):
1315
'appendLine', 'appendLine'
1416
];
17+
// tslint:ignore-next-line:no-suspicious-comments
18+
// TODO: Ideally we would not ignore any files.
19+
const ignoredFiles = util.getListOfFiles('unlocalizedFiles.json');
20+
const ignoredPrefix = path.normalize('src/test');
1521
const failureMessage = 'Messages must be localized in the Python Extension (use src/client/common/utils/localize.ts)';
1622
class NoStringLiteralsInMessages extends baseRuleWalker_1.BaseRuleWalker {
1723
visitCallExpression(node) {
18-
const prop = node.expression;
19-
if (!this.sholdIgnoreCcurrentFile(node) &&
20-
ts.isPropertyAccessExpression(node.expression) &&
21-
methodNames.indexOf(prop.name.text) >= 0) {
24+
if (!this.shouldIgnoreNode(node)) {
2225
node.arguments
2326
.filter(arg => ts.isStringLiteral(arg) || ts.isTemplateLiteral(arg))
2427
.forEach(arg => {
@@ -27,6 +30,34 @@ class NoStringLiteralsInMessages extends baseRuleWalker_1.BaseRuleWalker {
2730
}
2831
super.visitCallExpression(node);
2932
}
33+
shouldIgnoreCurrentFile(node) {
34+
//console.log('');
35+
//console.log(node.getSourceFile().fileName);
36+
//console.log(ignoredFiles);
37+
if (super.shouldIgnoreCurrentFile(node, ignoredFiles)) {
38+
return true;
39+
}
40+
const sourceFile = node.getSourceFile();
41+
if (sourceFile && sourceFile.fileName) {
42+
if (sourceFile.fileName.startsWith(ignoredPrefix)) {
43+
return true;
44+
}
45+
}
46+
return false;
47+
}
48+
shouldIgnoreNode(node) {
49+
if (this.shouldIgnoreCurrentFile(node)) {
50+
return true;
51+
}
52+
if (!ts.isPropertyAccessExpression(node.expression)) {
53+
return true;
54+
}
55+
const prop = node.expression;
56+
if (methodNames.indexOf(prop.name.text) < 0) {
57+
return true;
58+
}
59+
return false;
60+
}
3061
}
3162
class Rule extends Lint.Rules.AbstractRule {
3263
apply(sourceFile) {

build/tslint-rules/messagesMustBeLocalizedRule.ts

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,29 @@
33

44
'use strict';
55

6+
import * as path from 'path';
67
import * as Lint from 'tslint';
78
import * as ts from 'typescript';
9+
import * as util from '../util';
810
import { BaseRuleWalker } from './baseRuleWalker';
911

1012
const methodNames = [
11-
// From IApplicationShell (vscode.window)
13+
// From IApplicationShell (vscode.window):
1214
'showErrorMessage', 'showInformationMessage',
1315
'showWarningMessage', 'setStatusBarMessage',
14-
// From IOutputChannel (vscode.OutputChannel)
16+
// From IOutputChannel (vscode.OutputChannel):
1517
'appendLine', 'appendLine'
1618
];
19+
// tslint:ignore-next-line:no-suspicious-comments
20+
// TODO: Ideally we would not ignore any files.
21+
const ignoredFiles = util.getListOfFiles('unlocalizedFiles.json');
22+
const ignoredPrefix = path.normalize('src/test');
1723

1824
const failureMessage = 'Messages must be localized in the Python Extension (use src/client/common/utils/localize.ts)';
1925

2026
class NoStringLiteralsInMessages extends BaseRuleWalker {
2127
protected visitCallExpression(node: ts.CallExpression): void {
22-
const prop = node.expression as ts.PropertyAccessExpression;
23-
if (!this.sholdIgnoreCcurrentFile(node) &&
24-
ts.isPropertyAccessExpression(node.expression) &&
25-
methodNames.indexOf(prop.name.text) >= 0) {
28+
if (!this.shouldIgnoreNode(node)) {
2629
node.arguments
2730
.filter(arg => ts.isStringLiteral(arg) || ts.isTemplateLiteral(arg))
2831
.forEach(arg => {
@@ -31,6 +34,34 @@ class NoStringLiteralsInMessages extends BaseRuleWalker {
3134
}
3235
super.visitCallExpression(node);
3336
}
37+
protected shouldIgnoreCurrentFile(node: ts.Node) {
38+
//console.log('');
39+
//console.log(node.getSourceFile().fileName);
40+
//console.log(ignoredFiles);
41+
if (super.shouldIgnoreCurrentFile(node, ignoredFiles)) {
42+
return true;
43+
}
44+
const sourceFile = node.getSourceFile();
45+
if (sourceFile && sourceFile.fileName) {
46+
if (sourceFile.fileName.startsWith(ignoredPrefix)) {
47+
return true;
48+
}
49+
}
50+
return false;
51+
}
52+
private shouldIgnoreNode(node: ts.CallExpression) {
53+
if (this.shouldIgnoreCurrentFile(node)) {
54+
return true;
55+
}
56+
if (!ts.isPropertyAccessExpression(node.expression)) {
57+
return true;
58+
}
59+
const prop = node.expression as ts.PropertyAccessExpression;
60+
if (methodNames.indexOf(prop.name.text) < 0) {
61+
return true;
62+
}
63+
return false;
64+
}
3465
}
3566

3667
export class Rule extends Lint.Rules.AbstractRule {

0 commit comments

Comments
 (0)