Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Commit

Permalink
Support outlawing /// <reference> style imports. (#1139)
Browse files Browse the repository at this point in the history
The reasoning is that these are superseded by ES6 style imports, and the syntax
overall isn't too pleasant.
  • Loading branch information
mprobst authored and jkillian committed Apr 20, 2016
1 parent 71d4b95 commit 3c80d83
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ A sample configuration file with all options is available [here](https://github.
* `no-inferrable-types` disallows explicit type declarations for variables or parameters initialized to a number, string, or boolean.
* `no-internal-module` disallows internal `module` (use `namespace` instead).
* `no-null-keyword` disallows use of the `null` keyword literal
* `no-reference` disallows `/// <reference>` imports (use ES6-style imports instead).
* `no-require-imports` disallows invocation of `require()` (use ES6-style imports instead).
* `no-shadowed-variable` disallows shadowed variable declarations.
* `no-string-literal` disallows object access via string literals.
Expand Down
35 changes: 35 additions & 0 deletions src/rules/noReferenceRule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* @license
* Copyright 2016 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 {
public static FAILURE_STRING = "<reference> is not allowed, use imports";

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(new NoReferenceWalker(sourceFile, this.getOptions()));
}
}

class NoReferenceWalker extends Lint.RuleWalker {
public visitSourceFile(node: ts.SourceFile) {
for (let ref of node.referencedFiles) {
this.addFailure(this.createFailure(ref.pos, ref.end - ref.pos, Rule.FAILURE_STRING));
}
}
}
1 change: 1 addition & 0 deletions src/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
"rules/noInternalModuleRule.ts",
"rules/noInvalidThisRule.ts",
"rules/noNullKeywordRule.ts",
"rules/noReferenceRule.ts",
"rules/noRequireImportsRule.ts",
"rules/noShadowedVariableRule.ts",
"rules/noStringLiteralRule.ts",
Expand Down
2 changes: 2 additions & 0 deletions test/rules/no-reference/test.ts.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/// <reference path="something.ts"/>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [<reference> is not allowed, use imports]
5 changes: 5 additions & 0 deletions test/rules/no-reference/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"no-reference": true
}
}
1 change: 1 addition & 0 deletions test/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
"../src/rules/noInternalModuleRule.ts",
"../src/rules/noInvalidThisRule.ts",
"../src/rules/noNullKeywordRule.ts",
"../src/rules/noReferenceRule.ts",
"../src/rules/noRequireImportsRule.ts",
"../src/rules/noShadowedVariableRule.ts",
"../src/rules/noStringLiteralRule.ts",
Expand Down

0 comments on commit 3c80d83

Please sign in to comment.