-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat(eslint-plugin): Add rule triple-slash-reference #625
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jessetrinity
merged 9 commits into
typescript-eslint:master
from
jessetrinity:addRuleNoReferenceImport
Jul 2, 2019
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
55f6277
feat(eslint-plugin): added no-reference-import rule
b675bc8
docs: added docs for no-reference-import
ac3cd5d
fix(eslint-plugin): collect references when visiting the program node
ecc65e1
feat(eslint-plugin): updated rule to cover more reference directives
6fca146
fix(eslint-plugin): deprecated rule and added coverage
5a805b9
Merge branch 'master' into addRuleNoReferenceImport
bradzacher c9a14ab
Merge branch 'master' into addRuleNoReferenceImport
bradzacher bd1d67c
Merge branch 'master' into addRuleNoReferenceImport
jessetrinity 1549cf1
Update README.md
jessetrinity File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
packages/eslint-plugin/docs/rules/triple-slash-reference.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Sets preference level for triple slash directives versus ES6-style import declarations. (triple-slash-reference) | ||
|
||
Use of triple-slash reference type directives is discouraged in favor of the newer `import` style. This rule allows you to ban use of `/// <reference path="" />`, `/// <reference types="" />`, or `/// <reference lib="" />` directives. | ||
|
||
Consider using this rule in place of [`no-triple-slash-reference`](./no-triple-slash-reference.md) which has been deprecated. | ||
|
||
## Rule Details | ||
|
||
With `{ "path": "never", "types": "never", "lib": "never" }` options set, the following will all be **incorrect** usage: | ||
|
||
```ts | ||
/// <reference path="foo" /> | ||
/// <reference types="bar" /> | ||
/// <reference lib="baz" /> | ||
``` | ||
|
||
Examples of **incorrect** code for the `{ "types": "prefer-import" }` option. Note that these are only errors when **both** stlyes are used for the **same** module: | ||
|
||
```ts | ||
/// <reference types="foo" /> | ||
import * as foo from 'foo'; | ||
``` | ||
|
||
```ts | ||
/// <reference types="foo" /> | ||
import foo = require('foo'); | ||
``` | ||
|
||
With `{ "path": "always", "types": "always", "lib": "always" }` options set, the following will all be **correct** usage: | ||
|
||
```ts | ||
/// <reference path="foo" /> | ||
/// <reference types="bar" /> | ||
/// <reference lib="baz" /> | ||
``` | ||
|
||
Examples of **correct** code for the `{ "types": "prefer-import" }` option: | ||
|
||
```ts | ||
import * as foo from 'foo'; | ||
``` | ||
|
||
```ts | ||
import foo = require('foo'); | ||
``` | ||
|
||
## When To Use It | ||
|
||
If you want to ban use of one or all of the triple slash reference directives, or any time you might use triple-slash type reference directives and ES6 import declarations in the same file. | ||
|
||
## When Not To Use It | ||
|
||
If you want to use all flavors of triple slash reference directives. | ||
|
||
## Compatibility | ||
|
||
- TSLint: [no-reference](http://palantir.github.io/tslint/rules/no-reference/) | ||
- TSLint: [no-reference-import](https://palantir.github.io/tslint/rules/no-reference-import/) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
packages/eslint-plugin/src/rules/triple-slash-reference.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
import * as util from '../util'; | ||
import { | ||
Literal, | ||
Node, | ||
TSExternalModuleReference, | ||
} from '@typescript-eslint/typescript-estree/dist/ts-estree/ts-estree'; | ||
import { TSESTree } from '@typescript-eslint/typescript-estree'; | ||
|
||
type Options = [ | ||
{ | ||
lib?: 'always' | 'never'; | ||
path?: 'always' | 'never'; | ||
types?: 'always' | 'never' | 'prefer-import'; | ||
} | ||
]; | ||
type MessageIds = 'tripleSlashReference'; | ||
|
||
export default util.createRule<Options, MessageIds>({ | ||
name: 'triple-slash-reference', | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: | ||
'Sets preference level for triple slash directives versus ES6-style import declarations', | ||
category: 'Best Practices', | ||
recommended: false, | ||
}, | ||
messages: { | ||
tripleSlashReference: | ||
'Do not use a triple slash reference for {{module}}, use `import` style instead.', | ||
}, | ||
schema: [ | ||
{ | ||
type: 'object', | ||
properties: { | ||
lib: { | ||
enum: ['always', 'never'], | ||
}, | ||
path: { | ||
enum: ['always', 'never'], | ||
}, | ||
types: { | ||
enum: ['always', 'never', 'prefer-import'], | ||
}, | ||
}, | ||
additionalProperties: false, | ||
}, | ||
], | ||
}, | ||
defaultOptions: [ | ||
{ | ||
lib: 'always', | ||
path: 'never', | ||
types: 'prefer-import', | ||
}, | ||
], | ||
create(context, [{ lib, path, types }]) { | ||
let programNode: Node; | ||
const sourceCode = context.getSourceCode(); | ||
const references: ({ | ||
comment: TSESTree.Comment; | ||
importName: string; | ||
})[] = []; | ||
|
||
function hasMatchingReference(source: Literal) { | ||
references.forEach(reference => { | ||
if (reference.importName === source.value) { | ||
context.report({ | ||
node: reference.comment, | ||
messageId: 'tripleSlashReference', | ||
data: { | ||
module: reference.importName, | ||
}, | ||
}); | ||
} | ||
}); | ||
} | ||
return { | ||
ImportDeclaration(node) { | ||
if (programNode) { | ||
const source = node.source as Literal; | ||
hasMatchingReference(source); | ||
} | ||
}, | ||
TSImportEqualsDeclaration(node) { | ||
if (programNode) { | ||
const source = (node.moduleReference as TSExternalModuleReference) | ||
.expression as Literal; | ||
hasMatchingReference(source); | ||
} | ||
}, | ||
Program(node) { | ||
if (lib === 'always' && path === 'always' && types == 'always') { | ||
return; | ||
} | ||
programNode = node; | ||
const referenceRegExp = /^\/\s*<reference\s*(types|path|lib)\s*=\s*["|'](.*)["|']/; | ||
const commentsBefore = sourceCode.getCommentsBefore(programNode); | ||
|
||
commentsBefore.forEach(comment => { | ||
if (comment.type !== 'Line') { | ||
return; | ||
} | ||
const referenceResult = referenceRegExp.exec(comment.value); | ||
|
||
if (referenceResult) { | ||
if ( | ||
(referenceResult[1] === 'types' && types === 'never') || | ||
(referenceResult[1] === 'path' && path === 'never') || | ||
(referenceResult[1] === 'lib' && lib === 'never') | ||
) { | ||
context.report({ | ||
node: comment, | ||
messageId: 'tripleSlashReference', | ||
data: { | ||
module: referenceResult[2], | ||
}, | ||
}); | ||
return; | ||
} | ||
if (referenceResult[1] === 'types' && types === 'prefer-import') { | ||
references.push({ comment, importName: referenceResult[2] }); | ||
} | ||
} | ||
}); | ||
}, | ||
}; | ||
}, | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.