eslint-plugin-unused-imports
Advanced tools
Comparing version 3.0.0 to 3.1.0
--- | ||
name: Bug report | ||
about: Create a report to help us improve | ||
title: '' | ||
labels: '' | ||
assignees: '' | ||
title: "" | ||
labels: "" | ||
assignees: "" | ||
--- | ||
@@ -12,5 +11,5 @@ | ||
If the issue is with something being marked wrongly as a unused import and therefore being removed. It is an issue with the imported package (`@typescript-eslint/eslint-plugin` for TS or `eslint` for JS) and its `no-unused-vars` rule. I cannot do anything about this except updating if a fix is made upstream. | ||
If the issue is with something being marked wrongly as a unused import and therefore being removed. It is an issue with the imported package (`@typescript-eslint/eslint-plugin` for TS or `eslint` for JS) and its `no-unused-vars` rule. I cannot do anything about this except updating if a fix is made upstream. | ||
If new rules are added `no-unused-vars` upstream which should be autofixed, mark your issue *rule addition*. | ||
If new rules are added `no-unused-vars` upstream which should be autofixed, mark your issue _rule addition_. | ||
@@ -17,0 +16,0 @@ Now if something is not marked an import and being removed by the autofixer, it is an issue I can do something about. |
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.1.0", | ||
"configurations": [ | ||
{ | ||
"type": "node", | ||
"request": "launch", | ||
"name": "Debug current test file", | ||
"runtimeExecutable": "npm", | ||
"runtimeArgs": ["test", "--testPathPattern", "${file}", "--coverage", "false"], | ||
"port": 9229, | ||
"cwd": "${fileDirname}", | ||
"timeout": 10000, | ||
"console": "integratedTerminal" | ||
} | ||
] | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.1.0", | ||
"configurations": [ | ||
{ | ||
"type": "node", | ||
"request": "launch", | ||
"name": "Debug current test file", | ||
"runtimeExecutable": "npm", | ||
"runtimeArgs": ["test", "--testPathPattern", "${file}", "--coverage", "false"], | ||
"port": 9229, | ||
"cwd": "${fileDirname}", | ||
"timeout": 10000, | ||
"console": "integratedTerminal" | ||
} | ||
] | ||
} | ||
@@ -1,97 +0,14 @@ | ||
const rule = require("../rules/no-unused-imports"); | ||
RuleTester = require("eslint").RuleTester; | ||
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2015, sourceType: "module", } }); | ||
const ruleTester = new RuleTester({ | ||
parserOptions: { ecmaVersion: 2015, sourceType: "module" }, | ||
}); | ||
ruleTester.run("no-unused-imports", rule, { | ||
valid: [ | ||
{ | ||
code: ` | ||
import x from "package"; | ||
import { a, b } from "./utils"; | ||
import y from "package"; | ||
const cases = require("./cases"); | ||
const c = a() + b + x() + y(); | ||
`, | ||
} | ||
], | ||
invalid: [ | ||
{ | ||
code: ` | ||
import x from "package"; | ||
import { a, b } from "./utils"; | ||
import y from "package"; | ||
const c = b(x, y); | ||
`, | ||
errors: ["'a' is defined but never used."], | ||
output: ` | ||
import x from "package"; | ||
import { b } from "./utils"; | ||
import y from "package"; | ||
const c = b(x, y); | ||
` | ||
}, | ||
{ | ||
code: ` | ||
import { a, b } from "./utils"; | ||
import y from "package"; | ||
/** | ||
* this is a jsdoc! | ||
*/ | ||
const c = a(y); | ||
`, | ||
errors: ["'b' is defined but never used."], | ||
output: ` | ||
import { a } from "./utils"; | ||
import y from "package"; | ||
/** | ||
* this is a jsdoc! | ||
*/ | ||
const c = a(y); | ||
` | ||
}, | ||
{ | ||
code: ` | ||
import { a } from "./utils"; | ||
import y from "package"; | ||
const c = 4; | ||
console.log(y); | ||
`, | ||
errors: ["'a' is defined but never used."], | ||
output: ` | ||
import y from "package"; | ||
const c = 4; | ||
console.log(y); | ||
` | ||
}, | ||
{ | ||
code: ` | ||
import y from "package"; | ||
import { a } from "./utils"; | ||
/** | ||
* c is the number 4 | ||
*/ | ||
const c = 4; | ||
console.log(y); | ||
`, | ||
errors: ["'a' is defined but never used."], | ||
output: ` | ||
import y from "package"; | ||
/** | ||
* c is the number 4 | ||
*/ | ||
const c = 4; | ||
console.log(y); | ||
` | ||
} | ||
] | ||
}); | ||
global.eslintUnusedImportsForceLoadJSLint = "true"; | ||
let rule; | ||
jest.isolateModules(() => { | ||
rule = require("../rules/no-unused-imports"); | ||
}); | ||
ruleTester.run("no-unused-imports", rule, cases); |
@@ -12,6 +12,6 @@ /** | ||
module.exports.rules = { | ||
"no-unused-vars": noUnusedVars, | ||
"no-unused-imports": noUnusedImports, | ||
"no-unused-vars-ts": noUnusedVars, | ||
"no-unused-imports-ts": noUnusedImports, | ||
"no-unused-vars": noUnusedVars, | ||
"no-unused-imports": noUnusedImports, | ||
"no-unused-vars-ts": noUnusedVars, | ||
"no-unused-imports-ts": noUnusedImports, | ||
}; |
@@ -11,15 +11,8 @@ /** | ||
let rule; | ||
try { | ||
const tslint = require("@typescript-eslint/eslint-plugin"); | ||
rule = tslint.rules["no-unused-vars"]; | ||
} catch (_) { | ||
const eslint = require("eslint"); | ||
rule = new eslint.Linter().getRules().get("no-unused-vars"); | ||
} | ||
const rule = require("./load-rule"); | ||
rule.meta.fixable = "code"; | ||
rule.meta.docs.url = "https://github.com/sweepline/eslint-plugin-unused-imports/blob/master/docs/rules/no-unused-imports.md"; | ||
rule.meta.docs.url = | ||
"https://github.com/sweepline/eslint-plugin-unused-imports/blob/master/docs/rules/no-unused-imports.md"; | ||
rule.meta.docs.extendsBaseRule = false; | ||
module.exports = ruleComposer.filterReports(rule, unusedImportsPredicate); |
@@ -11,14 +11,7 @@ /** | ||
let rule; | ||
try { | ||
const tslint = require("@typescript-eslint/eslint-plugin"); | ||
rule = tslint.rules["no-unused-vars"]; | ||
} catch (_) { | ||
const eslint = require("eslint"); | ||
rule = new eslint.Linter().getRules().get("no-unused-vars"); | ||
} | ||
const rule = require("./load-rule"); | ||
rule.meta.fixable = "code"; | ||
rule.meta.docs.url = "https://github.com/sweepline/eslint-plugin-unused-imports/blob/master/docs/rules/no-unused-vars.md"; | ||
rule.meta.docs.url = | ||
"https://github.com/sweepline/eslint-plugin-unused-imports/blob/master/docs/rules/no-unused-vars.md"; | ||
module.exports = ruleComposer.filterReports(rule, unusedVarsPredicate); |
exports.unusedVarsPredicate = (problem, context) => { | ||
const { node } = problem; | ||
const { parent } = node; | ||
const { node } = problem; | ||
const { parent } = node; | ||
// If parent is null just let the composed rule handle it | ||
if (parent == null) { | ||
return problem; | ||
} | ||
// If parent is null just let the composed rule handle it | ||
if (parent == null) { | ||
return problem; | ||
} | ||
// Remove these 3 cases, pass any other trough. | ||
switch (parent.type) { | ||
case "ImportSpecifier": | ||
case "ImportDefaultSpecifier": | ||
case "ImportNamespaceSpecifier": | ||
return false; | ||
default: | ||
return problem; | ||
} | ||
// Remove these 3 cases, pass any other trough. | ||
switch (parent.type) { | ||
case "ImportSpecifier": | ||
case "ImportDefaultSpecifier": | ||
case "ImportNamespaceSpecifier": | ||
return false; | ||
default: | ||
return problem; | ||
} | ||
}; | ||
@@ -25,74 +25,81 @@ | ||
exports.unusedImportsPredicate = (problem, context) => { | ||
const { sourceCode } = context; | ||
const { sourceCode } = context; | ||
const { node } = problem; | ||
const { parent } = node; | ||
const { node } = problem; | ||
const { parent } = node; | ||
// If parent is null just let the composed rule handle it | ||
if (parent == null) { | ||
return problem; | ||
} | ||
// If parent is null just let the composed rule handle it | ||
if (parent == null) { | ||
return problem; | ||
} | ||
// Only handle these 3 cases. | ||
switch (parent.type) { | ||
case "ImportSpecifier": | ||
case "ImportDefaultSpecifier": | ||
case "ImportNamespaceSpecifier": | ||
break; | ||
default: | ||
return false; | ||
} | ||
// Only handle these 3 cases. | ||
switch (parent.type) { | ||
case "ImportSpecifier": | ||
case "ImportDefaultSpecifier": | ||
case "ImportNamespaceSpecifier": | ||
break; | ||
default: | ||
return false; | ||
} | ||
problem.fix = (fixer) => { | ||
if (!parent) { | ||
return null; | ||
} | ||
const grandParent = parent.parent; | ||
problem.fix = (fixer) => { | ||
if (!parent) { | ||
return null; | ||
} | ||
const grandParent = parent.parent; | ||
if (!grandParent) { | ||
return null; | ||
} | ||
if (!grandParent) { | ||
return null; | ||
} | ||
// Only one import | ||
if (grandParent.specifiers.length === 1) { | ||
const nextToken = sourceCode.getTokenAfter(grandParent, includeCommentsFilter); | ||
const newLinesBetween = nextToken ? nextToken.loc.start.line - grandParent.loc.start.line : 0; | ||
const endOfReplaceRange = nextToken ? nextToken.range[0] : grandParent.range[1] | ||
const count = Math.max(0, newLinesBetween - 1); | ||
// Only one import | ||
if (grandParent.specifiers.length === 1) { | ||
const nextToken = sourceCode.getTokenAfter(grandParent, includeCommentsFilter); | ||
const newLinesBetween = nextToken | ||
? nextToken.loc.start.line - grandParent.loc.start.line | ||
: 0; | ||
const endOfReplaceRange = nextToken ? nextToken.range[0] : grandParent.range[1]; | ||
const count = Math.max(0, newLinesBetween - 1); | ||
return [ | ||
fixer.remove(grandParent), | ||
fixer.replaceTextRange([grandParent.range[1], endOfReplaceRange], "\n".repeat(count)), | ||
]; | ||
} | ||
return [ | ||
fixer.remove(grandParent), | ||
fixer.replaceTextRange( | ||
[grandParent.range[1], endOfReplaceRange], | ||
"\n".repeat(count), | ||
), | ||
]; | ||
} | ||
// Not last specifier | ||
if (parent !== grandParent.specifiers[grandParent.specifiers.length - 1]) { | ||
const comma = sourceCode.getTokenAfter(parent, commaFilter); | ||
const prevNode = sourceCode.getTokenBefore(parent); | ||
// Not last specifier | ||
if (parent !== grandParent.specifiers[grandParent.specifiers.length - 1]) { | ||
const comma = sourceCode.getTokenAfter(parent, commaFilter); | ||
const prevNode = sourceCode.getTokenBefore(parent); | ||
return [ | ||
fixer.removeRange([prevNode.range[1], parent.range[0]]), | ||
fixer.remove(parent), | ||
fixer.remove(comma), | ||
]; | ||
} | ||
return [ | ||
fixer.removeRange([prevNode.range[1], parent.range[0]]), | ||
fixer.remove(parent), | ||
fixer.remove(comma), | ||
]; | ||
} | ||
// Default export and a single normal left, ex. "import default, { package1 } from 'module';" | ||
if ( | ||
grandParent.specifiers.filter((specifier) => specifier.type === "ImportSpecifier").length === | ||
1 | ||
) { | ||
const start = sourceCode.getTokenBefore(parent, commaFilter); | ||
const end = sourceCode.getTokenAfter(parent, { filter: (token) => token.value === "}" }); | ||
// Default export and a single normal left, ex. "import default, { package1 } from 'module';" | ||
if ( | ||
grandParent.specifiers.filter((specifier) => specifier.type === "ImportSpecifier") | ||
.length === 1 | ||
) { | ||
const start = sourceCode.getTokenBefore(parent, commaFilter); | ||
const end = sourceCode.getTokenAfter(parent, { | ||
filter: (token) => token.value === "}", | ||
}); | ||
return fixer.removeRange([start.range[0], end.range[1]]); | ||
} | ||
return fixer.removeRange([start.range[0], end.range[1]]); | ||
} | ||
return fixer.removeRange([ | ||
sourceCode.getTokenBefore(parent, commaFilter).range[0], | ||
parent.range[1], | ||
]); | ||
}; | ||
return problem; | ||
return fixer.removeRange([ | ||
sourceCode.getTokenBefore(parent, commaFilter).range[0], | ||
parent.range[1], | ||
]); | ||
}; | ||
return problem; | ||
}; |
{ | ||
"name": "eslint-plugin-unused-imports", | ||
"version": "3.0.0", | ||
"description": "Report and remove unused es6 modules", | ||
"keywords": [ | ||
"eslint", | ||
"eslintplugin", | ||
"eslint-plugin", | ||
"import", | ||
"unused", | ||
"modules", | ||
"autofix" | ||
], | ||
"author": "Mikkel Holmer Pedersen", | ||
"main": "lib/index.js", | ||
"scripts": { | ||
"test": "jest" | ||
}, | ||
"peerDependencies": { | ||
"@typescript-eslint/eslint-plugin": "^6.0.0", | ||
"eslint": "^8.0.0" | ||
}, | ||
"peerDependenciesMeta": { | ||
"@typescript-eslint/eslint-plugin": { | ||
"optional": true | ||
} | ||
}, | ||
"dependencies": { | ||
"eslint-rule-composer": "^0.3.0" | ||
}, | ||
"devDependencies": { | ||
"@typescript-eslint/eslint-plugin": "^6.0.0", | ||
"@typescript-eslint/parser": "^6.0.0", | ||
"eslint": "^8.0.1", | ||
"jest": "^27.2.5" | ||
}, | ||
"engines": { | ||
"node": "^12.22.0 || ^14.17.0 || >=16.0.0" | ||
}, | ||
"license": "MIT", | ||
"repository": "https://github.com/sweepline/eslint-plugin-unused-imports", | ||
"homepage": "https://github.com/sweepline/eslint-plugin-unused-imports", | ||
"bugs": "https://github.com/sweepline/eslint-plugin-unused-imports/issues" | ||
"name": "eslint-plugin-unused-imports", | ||
"version": "3.1.0", | ||
"description": "Report and remove unused es6 modules", | ||
"keywords": [ | ||
"eslint", | ||
"eslintplugin", | ||
"eslint-plugin", | ||
"import", | ||
"unused", | ||
"modules", | ||
"autofix" | ||
], | ||
"author": "Mikkel Holmer Pedersen", | ||
"main": "lib/index.js", | ||
"scripts": { | ||
"test": "jest" | ||
}, | ||
"peerDependencies": { | ||
"@typescript-eslint/eslint-plugin": "6 - 7", | ||
"eslint": "8" | ||
}, | ||
"peerDependenciesMeta": { | ||
"@typescript-eslint/eslint-plugin": { | ||
"optional": true | ||
} | ||
}, | ||
"dependencies": { | ||
"eslint-rule-composer": "^0.3.0" | ||
}, | ||
"devDependencies": { | ||
"@typescript-eslint/eslint-plugin": "^7.0.0", | ||
"@typescript-eslint/parser": "^7.0.0", | ||
"eslint": "^8.0.1", | ||
"jest": "^29.2.5", | ||
"prettier": "^3.2.5", | ||
"ts-jest": "^29.1.2" | ||
}, | ||
"engines": { | ||
"node": "^12.22.0 || ^14.17.0 || >=16.0.0" | ||
}, | ||
"license": "MIT", | ||
"repository": "https://github.com/sweepline/eslint-plugin-unused-imports", | ||
"homepage": "https://github.com/sweepline/eslint-plugin-unused-imports", | ||
"bugs": "https://github.com/sweepline/eslint-plugin-unused-imports/issues" | ||
} |
@@ -7,5 +7,5 @@ # eslint-plugin-unused-imports | ||
* Version 3.x.x is for eslint 8 with @typescript-eslint/eslint-plugin 6 | ||
* Version 2.x.x is for eslint 8 with @typescript-eslint/eslint-plugin 5 | ||
* Version 1.x.x is for eslint 6 and 7. | ||
- Version 3.x.x is for eslint 8 with @typescript-eslint/eslint-plugin 6 - 7 | ||
- Version 2.x.x is for eslint 8 with @typescript-eslint/eslint-plugin 5 | ||
- Version 1.x.x is for eslint 6 and 7. | ||
@@ -47,3 +47,3 @@ ## _Important for version 1.1_ | ||
{ | ||
"plugins": ["unused-imports"] | ||
"plugins": [<other_plugins_you_use>, "unused-imports"] | ||
} | ||
@@ -56,10 +56,15 @@ ``` | ||
{ | ||
"rules": { | ||
"no-unused-vars": "off", // or "@typescript-eslint/no-unused-vars": "off", | ||
"unused-imports/no-unused-imports": "error", | ||
"unused-imports/no-unused-vars": [ | ||
"warn", | ||
{ "vars": "all", "varsIgnorePattern": "^_", "args": "after-used", "argsIgnorePattern": "^_" } | ||
] | ||
} | ||
"rules": { | ||
"no-unused-vars": "off", // or "@typescript-eslint/no-unused-vars": "off", | ||
"unused-imports/no-unused-imports": "error", | ||
"unused-imports/no-unused-vars": [ | ||
"warn", | ||
{ | ||
"vars": "all", | ||
"varsIgnorePattern": "^_", | ||
"args": "after-used", | ||
"argsIgnorePattern": "^_", | ||
}, | ||
], | ||
}, | ||
} | ||
@@ -70,3 +75,3 @@ ``` | ||
- `no-unused-imports` | ||
- `no-unused-vars` | ||
- `no-unused-imports` | ||
- `no-unused-vars` |
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
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
15214
16
307
74
6
1