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

@@ -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');
}
}
}