eslint-plugin-typescript
Advanced tools
Comparing version 0.13.0 to 0.14.0-rc.1
# Prevent TypeScript-specific constructs from being erroneously flagged as unused (no-unused-vars) | ||
This rule only has an effect when the `no-unused-vars` core rule is enabled. | ||
It ensures that TypeScript-specific constructs, such as implemented interfaces, are not erroneously flagged as unused. | ||
## Configuration | ||
***This rule only has an effect when the `no-unused-vars` core rule is enabled.*** | ||
See [the core ESLint docs](https://eslint.org/docs/rules/no-unused-vars) for how to configure the base `no-unused-vars` rule. | ||
```JSON | ||
{ | ||
"rules": { | ||
"no-unused-vars": "error", | ||
"typescript/no-unused-vars": "error", | ||
} | ||
} | ||
``` | ||
## Rule Details | ||
@@ -8,0 +20,0 @@ |
@@ -94,3 +94,8 @@ /** | ||
]; | ||
const aliasTypes = ["TSLastTypeNode", "TSArrayType", "TSTypeReference"]; | ||
const aliasTypes = [ | ||
"TSLastTypeNode", | ||
"TSArrayType", | ||
"TSTypeReference", | ||
"TSLiteralType" | ||
]; | ||
@@ -97,0 +102,0 @@ //---------------------------------------------------------------------- |
@@ -126,3 +126,8 @@ /** | ||
case "TSTypeParameter": { | ||
markTypeAnnotationAsUsed(annotation.constraint); | ||
if (annotation.constraint) { | ||
markTypeAnnotationAsUsed(annotation.constraint); | ||
} | ||
if (annotation.default) { | ||
markTypeAnnotationAsUsed(annotation.default); | ||
} | ||
break; | ||
@@ -215,2 +220,14 @@ } | ||
/** | ||
* Checks the given expression and marks any type parameters as used. | ||
* @param {ASTNode} node the relevant AST node. | ||
* @returns {void} | ||
* @private | ||
*/ | ||
function markExpressionAsUsed(node) { | ||
if (node.typeParameters && node.typeParameters.params) { | ||
node.typeParameters.params.forEach(markTypeAnnotationAsUsed); | ||
} | ||
} | ||
/** | ||
* Checks the given interface and marks it as used. | ||
@@ -236,3 +253,3 @@ * Generic arguments are also included in the check. | ||
/** | ||
* Checks the given function return type and marks it as used. | ||
* Checks the given function and marks return types and type parameter constraints as used. | ||
* @param {ASTNode} node the relevant AST node. | ||
@@ -242,3 +259,6 @@ * @returns {void} | ||
*/ | ||
function markFunctionReturnTypeAsUsed(node) { | ||
function markFunctionOptionsAsUsed(node) { | ||
if (node.typeParameters && node.typeParameters.params) { | ||
node.typeParameters.params.forEach(markTypeAnnotationAsUsed); | ||
} | ||
if (node.returnType) { | ||
@@ -250,3 +270,3 @@ markTypeAnnotationAsUsed(node.returnType); | ||
/** | ||
* Checks the given class and marks super classes, interfaces and decoratores as used. | ||
* Checks the given class and marks super classes, interfaces, type parameter constraints and decorators as used. | ||
* @param {ASTNode} node the relevant AST node. | ||
@@ -289,14 +309,14 @@ * @returns {void} | ||
FunctionDeclaration: markFunctionReturnTypeAsUsed, | ||
FunctionExpression: markFunctionReturnTypeAsUsed, | ||
ArrowFunctionExpression: markFunctionReturnTypeAsUsed, | ||
CallExpression(node) { | ||
if (node.typeParameters && node.typeParameters.params) { | ||
node.typeParameters.params.forEach( | ||
markTypeAnnotationAsUsed | ||
); | ||
} | ||
TSParameterProperty(node) { | ||
// just assume parameter properties are used | ||
markVariableAsUsed(context, node.parameter.name); | ||
}, | ||
FunctionDeclaration: markFunctionOptionsAsUsed, | ||
FunctionExpression: markFunctionOptionsAsUsed, | ||
ArrowFunctionExpression: markFunctionOptionsAsUsed, | ||
CallExpression: markExpressionAsUsed, | ||
NewExpression: markExpressionAsUsed, | ||
Decorator: markDecoratorAsUsed, | ||
@@ -303,0 +323,0 @@ TSInterfaceHeritage: markExtendedInterfaceAsUsed, |
@@ -43,2 +43,10 @@ /** | ||
/** | ||
* @param {Scope} scope - a scope to check | ||
* @returns {boolean} `true` if the scope is toplevel | ||
*/ | ||
function isTopLevelScope(scope) { | ||
return scope.type === "module" || scope.type === "global"; | ||
} | ||
/** | ||
* Checks whether or not a given variable is a function declaration. | ||
@@ -61,6 +69,14 @@ * | ||
function isOuterClass(variable, reference) { | ||
return ( | ||
variable.defs[0].type === "ClassName" && | ||
variable.scope.variableScope !== reference.from.variableScope | ||
); | ||
if (variable.defs[0].type !== "ClassName") { | ||
return false; | ||
} | ||
if (variable.scope.variableScope === reference.from.variableScope) { | ||
// allow the same scope only if it's the top level global/module scope | ||
if (!isTopLevelScope(variable.scope.variableScope)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
@@ -75,6 +91,14 @@ | ||
function isOuterVariable(variable, reference) { | ||
return ( | ||
variable.defs[0].type === "Variable" && | ||
variable.scope.variableScope !== reference.from.variableScope | ||
); | ||
if (variable.defs[0].type !== "Variable") { | ||
return false; | ||
} | ||
if (variable.scope.variableScope === reference.from.variableScope) { | ||
// allow the same scope only if it's the top level global/module scope | ||
if (!isTopLevelScope(variable.scope.variableScope)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
@@ -81,0 +105,0 @@ |
{ | ||
"name": "eslint-plugin-typescript", | ||
"version": "0.13.0", | ||
"version": "0.14.0-rc.1", | ||
"description": "TypeScript plugin for ESLint", | ||
@@ -20,3 +20,5 @@ "keywords": [ | ||
"mocha": "mocha tests --recursive --reporter=dot", | ||
"test": "npm run lint && npm run mocha && npm run docs:check", | ||
"pretest": "npm run lint", | ||
"test": "mocha tests --recursive --reporter=dot", | ||
"posttest": "npm run docs:check", | ||
"precommit": "npm test && lint-staged" | ||
@@ -38,4 +40,4 @@ }, | ||
"prettier": "^1.11.1", | ||
"typescript": "~2.8.1", | ||
"typescript-eslint-parser": "^15.0.0" | ||
"typescript": "~2.9", | ||
"typescript-eslint-parser": "^17.0.1" | ||
}, | ||
@@ -42,0 +44,0 @@ "lint-staged": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
0
191914
53
2948
2