/
no-unused-vars.ts
174 lines (159 loc) · 5.86 KB
/
no-unused-vars.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import { TSESTree, TSESLint } from '@typescript-eslint/experimental-utils';
import { PatternVisitor } from '@typescript-eslint/scope-manager';
import baseRule from 'eslint/lib/rules/no-unused-vars';
import * as util from '../util';
type MessageIds = util.InferMessageIdsTypeFromRule<typeof baseRule>;
type Options = util.InferOptionsTypeFromRule<typeof baseRule>;
export default util.createRule<Options, MessageIds>({
name: 'no-unused-vars',
meta: {
type: 'problem',
docs: {
description: 'Disallow unused variables',
category: 'Variables',
recommended: 'warn',
extendsBaseRule: true,
},
schema: baseRule.meta.schema,
messages: baseRule.meta.messages,
},
defaultOptions: [],
create(context) {
const rules = baseRule.create(context);
/**
* Gets a list of TS module definitions for a specified variable.
* @param variable eslint-scope variable object.
*/
function getModuleDeclarations(
variable: TSESLint.Scope.Variable,
): TSESTree.TSModuleDeclaration[] {
const functionDefinitions: TSESTree.TSModuleDeclaration[] = [];
variable.defs.forEach(def => {
// FunctionDeclarations
if (def.type === 'TSModuleName') {
functionDefinitions.push(def.node);
}
});
return functionDefinitions;
}
/**
* Determine if an identifier is referencing an enclosing module name.
* @param ref The reference to check.
* @param nodes The candidate function nodes.
* @returns True if it's a self-reference, false if not.
*/
function isSelfReference(
ref: TSESLint.Scope.Reference,
nodes: TSESTree.Node[],
): boolean {
let scope: TSESLint.Scope.Scope | null = ref.from;
while (scope) {
if (nodes.indexOf(scope.block) >= 0) {
return true;
}
scope = scope.upper;
}
return false;
}
return {
...rules,
'TSConstructorType, TSConstructSignatureDeclaration, TSDeclareFunction, TSEmptyBodyFunctionExpression, TSFunctionType, TSMethodSignature'(
node:
| TSESTree.TSConstructorType
| TSESTree.TSConstructSignatureDeclaration
| TSESTree.TSDeclareFunction
| TSESTree.TSEmptyBodyFunctionExpression
| TSESTree.TSFunctionType
| TSESTree.TSMethodSignature,
): void {
// function type signature params create variables because they can be referenced within the signature,
// but they obviously aren't unused variables for the purposes of this rule.
for (const param of node.params) {
visitPattern(param, name => {
context.markVariableAsUsed(name.name);
});
}
},
TSEnumDeclaration(): void {
// enum members create variables because they can be referenced within the enum,
// but they obviously aren't unused variables for the purposes of this rule.
const scope = context.getScope();
for (const variable of scope.variables) {
context.markVariableAsUsed(variable.name);
}
},
TSMappedType(node): void {
// mapped types create a variable for their type name, but it's not necessary to reference it,
// so we shouldn't consider it as unused for the purpose of this rule.
context.markVariableAsUsed(node.typeParameter.name.name);
},
TSModuleDeclaration(): void {
const childScope = context.getScope();
const scope = util.nullThrows(
context.getScope().upper,
util.NullThrowsReasons.MissingToken(childScope.type, 'upper scope'),
);
for (const variable of scope.variables) {
// check if the only reference to a module's name is a self-reference in its body
const moduleNodes = getModuleDeclarations(variable);
const isModuleDefinition = moduleNodes.length > 0;
if (
!isModuleDefinition ||
// ignore unreferenced module definitions, as the base rule will report on them
variable.references.length === 0
) {
continue;
}
const isVariableOnlySelfReferenced = variable.references.every(
ref => {
return isSelfReference(ref, moduleNodes);
},
);
if (isVariableOnlySelfReferenced) {
context.report({
node: variable.identifiers[0],
messageId: 'unusedVar',
data: {
varName: variable.name,
action: 'defined',
additional: '',
},
});
}
}
},
[[
'TSParameterProperty > AssignmentPattern > Identifier.left',
'TSParameterProperty > Identifier.parameter',
].join(', ')](node: TSESTree.Identifier): void {
// just assume parameter properties are used as property usage tracking is beyond the scope of this rule
context.markVariableAsUsed(node.name);
},
':matches(FunctionDeclaration, FunctionExpression, ArrowFunctionExpression) > Identifier[name="this"].params'(
node: TSESTree.Identifier,
): void {
// this parameters should always be considered used as they're pseudo-parameters
context.markVariableAsUsed(node.name);
},
// TODO
'*[declare=true] Identifier'(node: TSESTree.Identifier): void {
context.markVariableAsUsed(node.name);
const scope = context.getScope();
const { variableScope } = scope;
if (variableScope !== scope) {
const superVar = variableScope.set.get(node.name);
if (superVar) {
superVar.eslintUsed = true;
}
}
},
};
function visitPattern(
node: TSESTree.Node,
cb: (node: TSESTree.Identifier) => void,
): void {
const visitor = new PatternVisitor({}, node, cb);
visitor.visit(node);
}
},
});