Implement use strict linting (#7223)

* implement use strict linting

* commit changes

* add additional check for strict
This commit is contained in:
Anthony Dresser
2019-09-16 17:36:31 -07:00
committed by GitHub
parent 603a79d094
commit d3ea9c3168
4 changed files with 62 additions and 30 deletions

View File

@@ -189,20 +189,10 @@ const tslintBaseFilter = [
'!extensions/vscode-api-tests/testWorkspace2/**', '!extensions/vscode-api-tests/testWorkspace2/**',
'!extensions/**/*.test.ts', '!extensions/**/*.test.ts',
'!extensions/html-language-features/server/lib/jquery.d.ts', '!extensions/html-language-features/server/lib/jquery.d.ts',
// {{SQL CARBON EDIT}} '!extensions/big-data-cluster/src/bigDataCluster/controller/apiGenerated.ts' // {{SQL CARBON EDIT}}
'!extensions/big-data-cluster/src/bigDataCluster/controller/apiGenerated.ts'
]; ];
// {{SQL CARBON EDIT}} const sqlFilter = ['src/sql/**']; // {{SQL CARBON EDIT}}
// const useStrictFilter = [
// 'src/**'
// ];
const sqlFilter = [
'src/sql/**'
];
// {{SQL CARBON EDIT}}
const tslintCoreFilter = [ const tslintCoreFilter = [
'src/**/*.ts', 'src/**/*.ts',
@@ -336,23 +326,6 @@ function hygiene(some) {
this.emit('data', file); this.emit('data', file);
}); });
// {{SQL CARBON EDIT}}
// Check for unnecessary 'use strict' lines. These are automatically added by the alwaysStrict compiler option so don't need to be added manually
// const useStrict = es.through(function (file) {
// const lines = file.__lines;
// // Only take the first 10 lines to reduce false positives- the compiler will throw an error if it's not the first non-comment line in a file
// // (10 is used to account for copyright and extraneous newlines)
// lines.slice(0, 10).forEach((line, i) => {
// if (/\s*'use\s*strict\s*'/.test(line)) {
// console.error(file.relative + '(' + (i + 1) + ',1): Unnecessary \'use strict\' - this is already added by the compiler');
// errorCount++;
// }
// });
// this.emit('data', file);
// });
// {{SQL CARBON EDIT}} END
const formatting = es.map(function (file, cb) { const formatting = es.map(function (file, cb) {
tsfmt.processString(file.path, file.contents.toString('utf8'), { tsfmt.processString(file.path, file.contents.toString('utf8'), {
verify: false, verify: false,

View File

@@ -0,0 +1,28 @@
"use strict";
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
Object.defineProperty(exports, "__esModule", { value: true });
const Lint = require("tslint");
class Rule extends Lint.Rules.TypedRule {
applyWithProgram(sourceFile, program) {
if (program.getCompilerOptions().alwaysStrict) {
return this.applyWithWalker(new NoUselessStrictRuleWalker(sourceFile, this.getOptions()));
}
return [];
}
}
exports.Rule = Rule;
class NoUselessStrictRuleWalker extends Lint.RuleWalker {
visitStringLiteral(node) {
this.checkStringLiteral(node);
super.visitStringLiteral(node);
}
checkStringLiteral(node) {
const text = node.getText();
if (text === '\'use strict\'' || text === '"use strict"') {
this.addFailureAtNode(node, 'use strict directive is unnecessary');
}
}
}

View File

@@ -0,0 +1,30 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as Lint from 'tslint';
import * as ts from 'typescript';
export class Rule extends Lint.Rules.TypedRule {
public applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): Lint.RuleFailure[] {
if (program.getCompilerOptions().alwaysStrict) {
return this.applyWithWalker(new NoUselessStrictRuleWalker(sourceFile, this.getOptions()));
}
return [];
}
}
class NoUselessStrictRuleWalker extends Lint.RuleWalker {
protected visitStringLiteral(node: ts.StringLiteral): void {
this.checkStringLiteral(node);
super.visitStringLiteral(node);
}
private checkStringLiteral(node: ts.StringLiteral): void {
const text = node.getText();
if (text === '\'use strict\'' || text === '"use strict"') {
this.addFailureAtNode(node, 'use strict directive is unnecessary');
}
}
}

View File

@@ -228,7 +228,8 @@
"no-new-buffer": true, "no-new-buffer": true,
"translation-remind": true, "translation-remind": true,
"no-standalone-editor": true, "no-standalone-editor": true,
"no-nls-in-standalone-editor": true "no-nls-in-standalone-editor": true,
"no-useless-strict": true
}, },
"defaultSeverity": "warning" "defaultSeverity": "warning"
} }