Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
feat(eslint-plugin): add 'no-unnecessary-qualifier' rule#231
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
uniqueiniquity merged 12 commits intotypescript-eslint:masterfromuniqueiniquity:noUnnecQualFeb 11, 2019
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
12 commits Select commitHold shift + click to select a range
ae7e0fd
feat(eslint-plugin): add no-unnecessary-qualifier rule
55a7289
Merge branch 'master' into noUnnecQual
ef32e1d
docs: add documentation
8dfaf03
docs: update README and ROADMAP
d29eaf2
Merge branch 'master' into noUnnecQual
uniqueiniquity47f4504
test: increase code coverage
51f3e70
docs: add simple correct usage of qualifiers
01b2c55
Merge branch 'master' into noUnnecQual
armano2918ee62
Merge branch 'master' into noUnnecQual
0fcb5f9
chore: migrate to ts
eee94b1
refactor: use expressive selector
7257790
Merge branch 'master' into noUnnecQual
uniqueiniquityFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
3 changes: 2 additions & 1 deletionpackages/eslint-plugin/README.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
3 changes: 2 additions & 1 deletionpackages/eslint-plugin/ROADMAP.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
79 changes: 79 additions & 0 deletionspackages/eslint-plugin/docs/rules/no-unnecessary-qualifier.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,79 @@ | ||
# Warns when a namespace qualifier is unnecessary. (no-unnecessary-qualifier) | ||
## Rule Details | ||
This rule aims to let users know when a namespace or enum qualifier is unnecessary, | ||
whether used for a type or for a value. | ||
Examples of **incorrect** code for this rule: | ||
```ts | ||
namespace A { | ||
export type B = number; | ||
const x: A.B = 3; | ||
} | ||
``` | ||
```ts | ||
namespace A { | ||
export const x = 3; | ||
export const y = A.x; | ||
} | ||
``` | ||
```ts | ||
enum A { | ||
B, | ||
C = A.B | ||
} | ||
``` | ||
```ts | ||
namespace A { | ||
export namespace B { | ||
export type T = number; | ||
const x: A.B.T = 3; | ||
} | ||
} | ||
``` | ||
Examples of **correct** code for this rule: | ||
uniqueiniquity marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
```ts | ||
namespace X { | ||
export type T = number; | ||
} | ||
namespace Y { | ||
export const x: X.T = 3; | ||
} | ||
``` | ||
```ts | ||
enum A { | ||
X, | ||
Y | ||
} | ||
enum B { | ||
Z = A.X | ||
} | ||
``` | ||
```ts | ||
namespace X { | ||
export type T = number; | ||
namespace Y { | ||
type T = string; | ||
const x: X.T = 0; | ||
} | ||
} | ||
``` | ||
## When Not To Use It | ||
If you don't care about having unneeded namespace or enum qualifiers, then you don't need to use this rule. | ||
## Further Reading | ||
- TSLint: [no-unnecessary-qualifier](https://palantir.github.io/tslint/rules/no-unnecessary-qualifier/) |
208 changes: 208 additions & 0 deletionspackages/eslint-plugin/src/rules/no-unnecessary-qualifier.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,208 @@ | ||
/** | ||
* @fileoverview Warns when a namespace qualifier is unnecessary. | ||
* @author Benjamin Lichtman | ||
*/ | ||
import { TSESTree } from '@typescript-eslint/typescript-estree'; | ||
import ts from 'typescript'; | ||
import * as tsutils from 'tsutils'; | ||
import * as util from '../util'; | ||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
export default util.createRule({ | ||
name: 'no-unnecessary-qualifier', | ||
meta: { | ||
docs: { | ||
category: 'Best Practices', | ||
description: 'Warns when a namespace qualifier is unnecessary.', | ||
recommended: false, | ||
tslintName: 'no-unnecessary-qualifier' | ||
}, | ||
fixable: 'code', | ||
messages: { | ||
unnecessaryQualifier: | ||
"Qualifier is unnecessary since '{{ name }}' is in scope." | ||
}, | ||
schema: [], | ||
type: 'suggestion' | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
const namespacesInScope: ts.Node[] = []; | ||
let currentFailedNamespaceExpression: TSESTree.Node | null = null; | ||
const parserServices = util.getParserServices(context); | ||
const esTreeNodeToTSNodeMap = parserServices.esTreeNodeToTSNodeMap; | ||
const program = parserServices.program; | ||
const checker = program.getTypeChecker(); | ||
const sourceCode = context.getSourceCode(); | ||
//---------------------------------------------------------------------- | ||
// Helpers | ||
//---------------------------------------------------------------------- | ||
function tryGetAliasedSymbol( | ||
symbol: ts.Symbol, | ||
checker: ts.TypeChecker | ||
): ts.Symbol | null { | ||
return tsutils.isSymbolFlagSet(symbol, ts.SymbolFlags.Alias) | ||
? checker.getAliasedSymbol(symbol) | ||
: null; | ||
} | ||
function symbolIsNamespaceInScope(symbol: ts.Symbol): boolean { | ||
const symbolDeclarations = symbol.getDeclarations() || []; | ||
if ( | ||
symbolDeclarations.some(decl => | ||
namespacesInScope.some(ns => ns === decl) | ||
) | ||
) { | ||
return true; | ||
} | ||
const alias = tryGetAliasedSymbol(symbol, checker); | ||
return alias !== null && symbolIsNamespaceInScope(alias); | ||
} | ||
function getSymbolInScope( | ||
node: ts.Node, | ||
flags: ts.SymbolFlags, | ||
name: string | ||
): ts.Symbol | undefined { | ||
// TODO:PERF `getSymbolsInScope` gets a long list. Is there a better way? | ||
const scope = checker.getSymbolsInScope(node, flags); | ||
return scope.find(scopeSymbol => scopeSymbol.name === name); | ||
} | ||
function symbolsAreEqual(accessed: ts.Symbol, inScope: ts.Symbol): boolean { | ||
return accessed === checker.getExportSymbolOfSymbol(inScope); | ||
} | ||
function qualifierIsUnnecessary( | ||
qualifier: TSESTree.Node, | ||
name: TSESTree.Identifier | ||
): boolean { | ||
const tsQualifier = esTreeNodeToTSNodeMap.get(qualifier); | ||
const tsName = esTreeNodeToTSNodeMap.get(name); | ||
if (!(tsQualifier && tsName)) return false; // TODO: throw error? | ||
const namespaceSymbol = checker.getSymbolAtLocation(tsQualifier); | ||
if ( | ||
typeof namespaceSymbol === 'undefined' || | ||
!symbolIsNamespaceInScope(namespaceSymbol) | ||
) { | ||
return false; | ||
} | ||
const accessedSymbol = checker.getSymbolAtLocation(tsName); | ||
if (typeof accessedSymbol === 'undefined') { | ||
return false; | ||
} | ||
// If the symbol in scope is different, the qualifier is necessary. | ||
const fromScope = getSymbolInScope( | ||
tsQualifier, | ||
accessedSymbol.flags, | ||
sourceCode.getText(name) | ||
); | ||
return ( | ||
typeof fromScope === 'undefined' || | ||
symbolsAreEqual(accessedSymbol, fromScope) | ||
); | ||
} | ||
function visitNamespaceAccess( | ||
node: TSESTree.Node, | ||
qualifier: TSESTree.Node, | ||
name: TSESTree.Identifier | ||
): void { | ||
// Only look for nested qualifier errors if we didn't already fail on the outer qualifier. | ||
if ( | ||
!currentFailedNamespaceExpression && | ||
qualifierIsUnnecessary(qualifier, name) | ||
) { | ||
currentFailedNamespaceExpression = node; | ||
context.report({ | ||
node: qualifier, | ||
messageId: 'unnecessaryQualifier', | ||
data: { | ||
name: sourceCode.getText(name) | ||
}, | ||
fix(fixer) { | ||
return fixer.removeRange([qualifier.range[0], name.range[0]]); | ||
} | ||
}); | ||
} | ||
} | ||
function enterDeclaration(node: TSESTree.Node): void { | ||
const tsDeclaration = esTreeNodeToTSNodeMap.get(node); | ||
if (tsDeclaration) { | ||
namespacesInScope.push(tsDeclaration); | ||
} | ||
} | ||
function exitDeclaration(node: TSESTree.Node) { | ||
if (esTreeNodeToTSNodeMap.has(node)) { | ||
namespacesInScope.pop(); | ||
} | ||
} | ||
function resetCurrentNamespaceExpression(node: TSESTree.Node): void { | ||
if (node === currentFailedNamespaceExpression) { | ||
currentFailedNamespaceExpression = null; | ||
} | ||
} | ||
function isPropertyAccessExpression( | ||
node: TSESTree.Node | ||
): node is TSESTree.MemberExpression { | ||
return node.type === 'MemberExpression' && !node.computed; | ||
} | ||
function isEntityNameExpression(node: TSESTree.Node): boolean { | ||
return ( | ||
node.type === 'Identifier' || | ||
(isPropertyAccessExpression(node) && | ||
isEntityNameExpression(node.object)) | ||
); | ||
} | ||
//---------------------------------------------------------------------- | ||
// Public | ||
//---------------------------------------------------------------------- | ||
return { | ||
TSModuleDeclaration: enterDeclaration, | ||
TSEnumDeclaration: enterDeclaration, | ||
'ExportNamedDeclaration[declaration.type="TSModuleDeclaration"]': enterDeclaration, | ||
'ExportNamedDeclaration[declaration.type="TSEnumDeclaration"]': enterDeclaration, | ||
'TSModuleDeclaration:exit': exitDeclaration, | ||
'TSEnumDeclaration:exit': exitDeclaration, | ||
'ExportNamedDeclaration[declaration.type="TSModuleDeclaration"]:exit': exitDeclaration, | ||
'ExportNamedDeclaration[declaration.type="TSEnumDeclaration"]:exit': exitDeclaration, | ||
TSQualifiedName(node: TSESTree.TSQualifiedName): void { | ||
visitNamespaceAccess(node, node.left, node.right); | ||
}, | ||
'MemberExpression[computed=false]': function( | ||
node: TSESTree.MemberExpression | ||
): void { | ||
const property = node.property as TSESTree.Identifier; | ||
if (isEntityNameExpression(node.object)) { | ||
visitNamespaceAccess(node, node.object, property); | ||
} | ||
}, | ||
'TSQualifiedName:exit': resetCurrentNamespaceExpression, | ||
'MemberExpression:exit': resetCurrentNamespaceExpression | ||
}; | ||
} | ||
}); |
1 change: 1 addition & 0 deletionspackages/eslint-plugin/tests/fixtures/foo.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 @@ | ||
export type T = number; |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
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.