diff --git a/docs/_data/rules.json b/docs/_data/rules.json index 311d7c1e058..f98b7f7ddd6 100644 --- a/docs/_data/rules.json +++ b/docs/_data/rules.json @@ -1228,27 +1228,6 @@ ], "type": "functionality" }, - { - "ruleName": "use-strict", - "description": "Requires using ECMAScript 5's strict mode.", - "optionsDescription": "\nTwo arguments may be optionally provided:\n\n* `check-module` checks that all top-level modules are using strict mode.\n* `check-function` checks that all top-level functions are using strict mode.", - "options": { - "type": "array", - "items": { - "type": "string", - "enum": [ - "check-module", - "check-function" - ] - }, - "minLength": 0, - "maxLength": 2 - }, - "optionExamples": [ - "[true, \"check-module\"]" - ], - "type": "functionality" - }, { "ruleName": "variable-name", "description": "Checks variable names for various errors.", diff --git a/docs/rules/use-strict/index.html b/docs/rules/use-strict/index.html deleted file mode 100644 index a90611acb6b..00000000000 --- a/docs/rules/use-strict/index.html +++ /dev/null @@ -1,37 +0,0 @@ ---- -ruleName: use-strict -description: Requires using ECMAScript 5's strict mode. -optionsDescription: |- - - Two arguments may be optionally provided: - - * `check-module` checks that all top-level modules are using strict mode. - * `check-function` checks that all top-level functions are using strict mode. -options: - type: array - items: - type: string - enum: - - check-module - - check-function - minLength: 0 - maxLength: 2 -optionExamples: - - '[true, "check-module"]' -type: functionality -optionsJSON: |- - { - "type": "array", - "items": { - "type": "string", - "enum": [ - "check-module", - "check-function" - ] - }, - "minLength": 0, - "maxLength": 2 - } -layout: rule -title: 'Rule: use-strict' ---- \ No newline at end of file diff --git a/src/rules/useStrictRule.ts b/src/rules/useStrictRule.ts deleted file mode 100644 index d7aa8335c74..00000000000 --- a/src/rules/useStrictRule.ts +++ /dev/null @@ -1,121 +0,0 @@ -/** - * @license - * Copyright 2013 Palantir Technologies, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import * as ts from "typescript"; - -import * as Lint from "../lint"; - -export class Rule extends Lint.Rules.AbstractRule { - /* tslint:disable:object-literal-sort-keys */ - public static metadata: Lint.IRuleMetadata = { - ruleName: "use-strict", - description: "Requires using ECMAScript 5's strict mode.", - optionsDescription: Lint.Utils.dedent` - Two arguments may be optionally provided: - - * \`check-module\` checks that all top-level modules are using strict mode. - * \`check-function\` checks that all top-level functions are using strict mode.`, - options: { - type: "array", - items: { - type: "string", - enum: ["check-module", "check-function"], - }, - minLength: 0, - maxLength: 2, - }, - optionExamples: ['[true, "check-module"]'], - type: "functionality", - }; - /* tslint:enable:object-literal-sort-keys */ - - public static FAILURE_STRING = "missing 'use strict'"; - - public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { - const useStrictWalker = new UseStrictWalker(sourceFile, this.getOptions()); - return this.applyWithWalker(useStrictWalker); - } -} - -class UseStrictWalker extends Lint.ScopeAwareRuleWalker<{}> { - private static OPTION_CHECK_FUNCTION = "check-function"; - private static OPTION_CHECK_MODULE = "check-module"; - - private static USE_STRICT_STRING = "use strict"; - - public createScope(): {} { - return {}; - } - - public visitModuleDeclaration(node: ts.ModuleDeclaration) { - if (!Lint.hasModifier(node.modifiers, ts.SyntaxKind.DeclareKeyword) - && this.hasOption(UseStrictWalker.OPTION_CHECK_MODULE) - && node.body != null - && node.body.kind === ts.SyntaxKind.ModuleBlock) { - let firstModuleDeclaration = getFirstInModuleDeclarationsChain(node); - let hasOnlyModuleDeclarationParents = firstModuleDeclaration.parent.kind === ts.SyntaxKind.SourceFile; - - if (hasOnlyModuleDeclarationParents) { - this.handleBlock(firstModuleDeclaration, node.body); - } - } - - super.visitModuleDeclaration(node); - } - - public visitFunctionDeclaration(node: ts.FunctionDeclaration) { - // current depth is 2: global scope and the scope created by this function - if (this.getCurrentDepth() === 2 && - this.hasOption(UseStrictWalker.OPTION_CHECK_FUNCTION) && - node.body != null) { - this.handleBlock(node, node.body); - } - - super.visitFunctionDeclaration(node); - } - - private handleBlock(node: ts.Declaration, block: ts.Block | ts.ModuleBlock) { - let isFailure = true; - - if (block.statements != null && block.statements.length > 0) { - const firstStatement = block.statements[0]; - - if (firstStatement.kind === ts.SyntaxKind.ExpressionStatement) { - const firstChild = firstStatement.getChildAt(0); - - if (firstChild.kind === ts.SyntaxKind.StringLiteral - && ( firstChild).text === UseStrictWalker.USE_STRICT_STRING) { - isFailure = false; - } - } - } - - if (isFailure) { - this.addFailure(this.createFailure(node.getStart(), node.getFirstToken().getWidth(), Rule.FAILURE_STRING)); - } - } -} - -function getFirstInModuleDeclarationsChain(node: ts.ModuleDeclaration): ts.ModuleDeclaration { - let current = node; - - while (current.parent.kind === ts.SyntaxKind.ModuleDeclaration) { - current = current.parent; - } - - return current; -} diff --git a/src/tsconfig.json b/src/tsconfig.json index 5a55ca1ce33..a4db5f9f864 100644 --- a/src/tsconfig.json +++ b/src/tsconfig.json @@ -150,7 +150,6 @@ "rules/typedefRule.ts", "rules/typedefWhitespaceRule.ts", "rules/useIsnanRule.ts", - "rules/useStrictRule.ts", "rules/variableNameRule.ts", "rules/whitespaceRule.ts", "test/lines.ts", diff --git a/test/rules/use-strict/test.ts.lint b/test/rules/use-strict/test.ts.lint deleted file mode 100644 index e80f0664713..00000000000 --- a/test/rules/use-strict/test.ts.lint +++ /dev/null @@ -1,62 +0,0 @@ -declare function foo(): void; - -function testDoubleQuotes() { - // hello - "use strict"; // bye - - var i = 3; -} - -function testSingleQuotes() { - 'use strict'; -} - -function testNoUseStrict() { -~~~~~~~~ [missing 'use strict'] -} - -module TestModule { - // hello - "use strict"; // bye - - var i = 3; -} - -module TestNoUseStrictModule { -~~~~~~ [missing 'use strict'] - // hello - var i = 3; -} - -module TestNoUseStrictModule.Namespaced.AndAgain { -~~~~~~ [missing 'use strict'] - // hello - var i = 3; -} - -function checkDepth() { - "use strict"; - - function innerFunction() { - - } -} - -module TestModuleWithFunction { - "use strict"; - - function hello() { - // there shouldn't be a failure here since it isn't top level - } -} - -declare module foo { - // shouldn't error because of the declare - export class bar { - } -} - -function shouldPassUseStrictRule(): string { - "use strict" - return "This is a test" -} diff --git a/test/rules/use-strict/tslint.json b/test/rules/use-strict/tslint.json deleted file mode 100644 index 3784cf991b7..00000000000 --- a/test/rules/use-strict/tslint.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "rules": { - "use-strict": [true, "check-function", "check-module"] - } -} diff --git a/test/tsconfig.json b/test/tsconfig.json index d64527e9e25..1643ad32432 100644 --- a/test/tsconfig.json +++ b/test/tsconfig.json @@ -145,7 +145,6 @@ "../src/rules/typedefRule.ts", "../src/rules/typedefWhitespaceRule.ts", "../src/rules/useIsnanRule.ts", - "../src/rules/useStrictRule.ts", "../src/rules/variableNameRule.ts", "../src/rules/whitespaceRule.ts", "../src/test.ts",