This repository was archived by the owner on Jan 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathno-unused-vars.js
68 lines (61 loc) · 2.3 KB
/
no-unused-vars.js
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
/**
* @fileoverview Prevent TypeScript-specific variables being falsely marked as unused
* @author James Henry
*/
"use strict";
const baseRule = require("eslint/lib/rules/no-unused-vars");
const util = require("../util");
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = Object.assign({}, baseRule, {
meta: {
type: "problem",
docs: {
description: "Disallow unused variables",
extraDescription: [util.tslintRule("no-unused-variable")],
category: "Variables",
url: util.metaDocsUrl("no-unused-vars"),
recommended: "warn",
},
schema: baseRule.meta.schema,
},
create(context) {
const rules = baseRule.create(context);
/**
* Mark this function parameter as used
* @param {Identifier} node The node currently being traversed
* @returns {void}
*/
function markThisParameterAsUsed(node) {
if (node.name) {
const variable = context
.getScope()
.variables.find(scopeVar => scopeVar.name === node.name);
if (variable) {
variable.eslintUsed = true;
}
}
}
//----------------------------------------------------------------------
// Public
//----------------------------------------------------------------------
return Object.assign({}, rules, {
"FunctionDeclaration Identifier[name='this']": markThisParameterAsUsed,
"FunctionExpression Identifier[name='this']": markThisParameterAsUsed,
"TSTypeReference Identifier"(node) {
context.markVariableAsUsed(node.name);
},
"ClassImplements Identifier"(node) {
context.markVariableAsUsed(node.name);
},
"TSParameterProperty Identifier"(node) {
// just assume parameter properties are used
context.markVariableAsUsed(node.name);
},
"TSEnumMember Identifier"(node) {
context.markVariableAsUsed(node.name);
},
});
},
});