first commit

This commit is contained in:
2026-06-24 09:48:54 +02:00
commit 41e62ddcad
33739 changed files with 4266226 additions and 0 deletions
+21
View File
@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2022 Mikkel Holmer Pedersen
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+67
View File
@@ -0,0 +1,67 @@
# eslint-plugin-unused-imports
Find and remove unused es6 module imports. It works by splitting up the `no-unused-vars` rule depending on it being an import statement in the AST and providing an autofix rule to remove the nodes if they are imports. This plugin composes the rule `no-unused-vars` of either the typescript or js plugin so be aware that the other plugins needs to be installed and reporting correctly for this to do so.
## _Versions_
- Version 4.1.x is for eslint 9 with @typescript-eslint/eslint-plugin 5 - 8
- Version 4.0.x is for eslint 9 with @typescript-eslint/eslint-plugin 8
- 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.
## Typescript
If running typescript with [@typescript-eslint](https://github.com/typescript-eslint/typescript-eslint) make sure to use both `@typescript-eslint/eslint-plugin` and `@typescript-eslint/parser`.
## React
If writing react code you need to install `eslint-plugin-react` and enable the two rules `react/jsx-uses-react` and `react/jsx-uses-vars`. Otherwise all imports for components will be reported unused.
## Installation
You'll first need to install [ESLint](http://eslint.org) (and [@typescript-eslint](https://github.com/typescript-eslint/typescript-eslint) if using typescript):
```bash
npm i eslint --save-dev
```
Next, install `eslint-plugin-unused-imports`:
```bash
npm install eslint-plugin-unused-imports --save-dev
```
**Note:** If you installed ESLint globally (using the `-g` flag) then you must also install `eslint-plugin-unused-imports` globally.
## Usage
Add `unused-imports` to the plugins section of your `eslint.config.js` configuration file.
```js
import unusedImports from "eslint-plugin-unused-imports";
export default [{
plugins: {
"unused-imports": unusedImports,
},
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": "^_",
},
]
}
}];
```
## Supported Rules
- `no-unused-imports`
- `no-unused-vars`
+5
View File
@@ -0,0 +1,5 @@
import { ESLint } from 'eslint';
declare const plugin: ESLint.Plugin;
export { plugin as default };
+5
View File
@@ -0,0 +1,5 @@
import { ESLint } from 'eslint';
declare const plugin: ESLint.Plugin;
export = plugin;
+167
View File
@@ -0,0 +1,167 @@
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }// node_modules/.pnpm/tsup@8.5.0_jiti@2.5.1_postcss@8.4.40_typescript@5.9.3_yaml@2.8.1/node_modules/tsup/assets/cjs_shims.js
var getImportMetaUrl = () => typeof document === "undefined" ? new URL(`file:${__filename}`).href : document.currentScript && document.currentScript.src || new URL("main.js", document.baseURI).href;
var importMetaUrl = /* @__PURE__ */ getImportMetaUrl();
// src/rules/predicates.ts
var commaFilter = { filter: (token) => token.value === "," };
var includeCommentsFilter = { includeComments: true };
function isUsedInJSDoc(identifierName, sourceCode) {
const comments = sourceCode.getAllComments();
const jsdocPattern = new RegExp(
// {@link Name} or @see Name
`(?:@(?:link|linkcode|linkplain|see)\\s+${identifierName}\\b)|(?:\\{@(?:link|linkcode|linkplain)\\s+${identifierName}\\b\\})|(?:[@{](?:type|typedef|param|returns?|template|augments|extends|implements)\\s+[^}]*\\b${identifierName}\\b)`
);
return comments.some((comment) => {
if (comment.type !== "Block") {
return false;
}
return jsdocPattern.test(comment.value);
});
}
function makePredicate(isImport, addFixer) {
return (problem, context) => {
const sourceCode = context.sourceCode || context.getSourceCode();
const node = _nullishCoalesce(problem.node, () => ( // typescript-eslint >= 7.8 sets a range instead of a node
sourceCode.getNodeByRangeIndex(sourceCode.getIndexFromLoc(problem.loc.start))));
const { parent } = node;
if (parent && /^Import(|Default|Namespace)Specifier$/.test(parent.type) && isImport) {
const identifierName = node.name;
if (identifierName && isUsedInJSDoc(identifierName, sourceCode)) {
return false;
}
}
return parent ? /^Import(|Default|Namespace)Specifier$/.test(parent.type) == isImport ? Object.assign(problem, _optionalChain([addFixer, 'optionalCall', _2 => _2(parent, sourceCode)])) : false : isImport ? false : problem;
};
}
var unusedVarsPredicate = makePredicate(false);
var unusedImportsPredicate = makePredicate(true, (parent, sourceCode) => ({
fix(fixer) {
const grandParent = parent.parent;
if (!grandParent) {
return null;
}
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)
)
];
}
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)
];
}
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([
sourceCode.getTokenBefore(parent, commaFilter).range[0],
parent.range[1]
]);
}
}));
function createRuleWithPredicate(name, baseRule, predicate) {
return {
...baseRule,
meta: {
...baseRule.meta,
fixable: "code",
docs: {
..._optionalChain([baseRule, 'access', _3 => _3.meta, 'optionalAccess', _4 => _4.docs]),
url: `https://github.com/sweepline/eslint-plugin-unused-imports/blob/master/docs/rules/${name}.md`
}
},
create(context) {
return baseRule.create(
Object.create(context, {
report: {
enumerable: true,
value(problem) {
const result = predicate(problem, context);
if (result) {
context.report(result);
}
}
}
})
);
}
};
}
// src/rules/load-rule.ts
var _module = require('module');
var rule;
var require2 = _module.createRequire.call(void 0, importMetaUrl);
function getBaseRule() {
if (!rule) {
rule = _nullishCoalesce(_nullishCoalesce(getRuleFromTSLintPlugin(), () => ( getRuleFromTSLint())), () => ( getESLintBaseRule()));
}
return rule;
}
function getRuleFromTSLintPlugin() {
try {
const tslintPlugin = require2("@typescript-eslint/eslint-plugin");
return tslintPlugin.rules["no-unused-vars"];
} catch (_) {
return null;
}
}
function getRuleFromTSLint() {
try {
const tslint = require2("typescript-eslint");
return tslint.plugin.rules["no-unused-vars"];
} catch (_) {
return null;
}
}
function getESLintBaseRule() {
try {
const eslint = require2("eslint");
return new eslint.Linter({ configType: "eslintrc" }).getRules().get("no-unused-vars");
} catch (e) {
const eslint_USE_AT_YOUR_OWN_RISK = require2("eslint/use-at-your-own-risk");
if ("builtinRules" in eslint_USE_AT_YOUR_OWN_RISK && eslint_USE_AT_YOUR_OWN_RISK.builtinRules instanceof Map) {
return eslint_USE_AT_YOUR_OWN_RISK.builtinRules.get("no-unused-vars");
}
throw new TypeError("[eslint-plugin-unused-imports] Cannot load 'no-unused-vars' rule from ESLint. This is most likely due to a breaking change in ESLint's internal API. Please report this issue to 'eslint-plugin-unused-imports'.");
}
}
// src/rules/no-unused-vars.ts
var no_unused_vars_default = createRuleWithPredicate("no-unused-vars", getBaseRule(), unusedVarsPredicate);
// src/rules/no-unused-imports.ts
var no_unused_imports_default = createRuleWithPredicate("no-unused-imports", getBaseRule(), unusedImportsPredicate);
// src/index.ts
var plugin = {
meta: {
name: "unused-imports"
},
rules: {
"no-unused-vars": no_unused_vars_default,
"no-unused-imports": no_unused_imports_default
}
};
var index_default = plugin;
exports.default = index_default;
module.exports = exports.default;
+161
View File
@@ -0,0 +1,161 @@
// src/rules/predicates.ts
var commaFilter = { filter: (token) => token.value === "," };
var includeCommentsFilter = { includeComments: true };
function isUsedInJSDoc(identifierName, sourceCode) {
const comments = sourceCode.getAllComments();
const jsdocPattern = new RegExp(
// {@link Name} or @see Name
`(?:@(?:link|linkcode|linkplain|see)\\s+${identifierName}\\b)|(?:\\{@(?:link|linkcode|linkplain)\\s+${identifierName}\\b\\})|(?:[@{](?:type|typedef|param|returns?|template|augments|extends|implements)\\s+[^}]*\\b${identifierName}\\b)`
);
return comments.some((comment) => {
if (comment.type !== "Block") {
return false;
}
return jsdocPattern.test(comment.value);
});
}
function makePredicate(isImport, addFixer) {
return (problem, context) => {
const sourceCode = context.sourceCode || context.getSourceCode();
const node = problem.node ?? // typescript-eslint >= 7.8 sets a range instead of a node
sourceCode.getNodeByRangeIndex(sourceCode.getIndexFromLoc(problem.loc.start));
const { parent } = node;
if (parent && /^Import(|Default|Namespace)Specifier$/.test(parent.type) && isImport) {
const identifierName = node.name;
if (identifierName && isUsedInJSDoc(identifierName, sourceCode)) {
return false;
}
}
return parent ? /^Import(|Default|Namespace)Specifier$/.test(parent.type) == isImport ? Object.assign(problem, addFixer?.(parent, sourceCode)) : false : isImport ? false : problem;
};
}
var unusedVarsPredicate = makePredicate(false);
var unusedImportsPredicate = makePredicate(true, (parent, sourceCode) => ({
fix(fixer) {
const grandParent = parent.parent;
if (!grandParent) {
return null;
}
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)
)
];
}
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)
];
}
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([
sourceCode.getTokenBefore(parent, commaFilter).range[0],
parent.range[1]
]);
}
}));
function createRuleWithPredicate(name, baseRule, predicate) {
return {
...baseRule,
meta: {
...baseRule.meta,
fixable: "code",
docs: {
...baseRule.meta?.docs,
url: `https://github.com/sweepline/eslint-plugin-unused-imports/blob/master/docs/rules/${name}.md`
}
},
create(context) {
return baseRule.create(
Object.create(context, {
report: {
enumerable: true,
value(problem) {
const result = predicate(problem, context);
if (result) {
context.report(result);
}
}
}
})
);
}
};
}
// src/rules/load-rule.ts
import { createRequire } from "module";
var rule;
var require2 = createRequire(import.meta.url);
function getBaseRule() {
if (!rule) {
rule = getRuleFromTSLintPlugin() ?? getRuleFromTSLint() ?? getESLintBaseRule();
}
return rule;
}
function getRuleFromTSLintPlugin() {
try {
const tslintPlugin = require2("@typescript-eslint/eslint-plugin");
return tslintPlugin.rules["no-unused-vars"];
} catch (_) {
return null;
}
}
function getRuleFromTSLint() {
try {
const tslint = require2("typescript-eslint");
return tslint.plugin.rules["no-unused-vars"];
} catch (_) {
return null;
}
}
function getESLintBaseRule() {
try {
const eslint = require2("eslint");
return new eslint.Linter({ configType: "eslintrc" }).getRules().get("no-unused-vars");
} catch {
const eslint_USE_AT_YOUR_OWN_RISK = require2("eslint/use-at-your-own-risk");
if ("builtinRules" in eslint_USE_AT_YOUR_OWN_RISK && eslint_USE_AT_YOUR_OWN_RISK.builtinRules instanceof Map) {
return eslint_USE_AT_YOUR_OWN_RISK.builtinRules.get("no-unused-vars");
}
throw new TypeError("[eslint-plugin-unused-imports] Cannot load 'no-unused-vars' rule from ESLint. This is most likely due to a breaking change in ESLint's internal API. Please report this issue to 'eslint-plugin-unused-imports'.");
}
}
// src/rules/no-unused-vars.ts
var no_unused_vars_default = createRuleWithPredicate("no-unused-vars", getBaseRule(), unusedVarsPredicate);
// src/rules/no-unused-imports.ts
var no_unused_imports_default = createRuleWithPredicate("no-unused-imports", getBaseRule(), unusedImportsPredicate);
// src/index.ts
var plugin = {
meta: {
name: "unused-imports"
},
rules: {
"no-unused-vars": no_unused_vars_default,
"no-unused-imports": no_unused_imports_default
}
};
var index_default = plugin;
export {
index_default as default
};
+68
View File
@@ -0,0 +1,68 @@
{
"name": "eslint-plugin-unused-imports",
"version": "4.4.1",
"type": "commonjs",
"description": "Report and remove unused es6 modules",
"keywords": [
"eslint",
"eslintplugin",
"eslint-plugin",
"import",
"unused",
"modules",
"autofix"
],
"author": "Mikkel Holmer Pedersen",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
".": {
"import": "./dist/index.mjs",
"require": "./dist/index.js"
}
},
"files": [
"dist"
],
"peerDependencies": {
"@typescript-eslint/eslint-plugin": "^8.0.0-0 || ^7.0.0 || ^6.0.0 || ^5.0.0",
"eslint": "^10.0.0 || ^9.0.0 || ^8.0.0"
},
"peerDependenciesMeta": {
"@typescript-eslint/eslint-plugin": {
"optional": true
}
},
"devDependencies": {
"@types/eslint": "^9.6.1",
"@typescript-eslint-v5/eslint-plugin": "npm:@typescript-eslint/eslint-plugin@^5.62.0",
"@typescript-eslint-v5/parser": "npm:@typescript-eslint/parser@^5.62.0",
"@typescript-eslint-v6/eslint-plugin": "npm:@typescript-eslint/eslint-plugin@^6.21.0",
"@typescript-eslint-v6/parser": "npm:@typescript-eslint/parser@^6.21.0",
"@typescript-eslint-v7/eslint-plugin": "npm:@typescript-eslint/eslint-plugin@^7.18.0",
"@typescript-eslint-v7/parser": "npm:@typescript-eslint/parser@^7.18.0",
"@typescript-eslint/eslint-plugin": "^8.46.1",
"@typescript-eslint/parser": "^8.46.1",
"@typescript-eslint/utils": "^8.46.1",
"bumpp": "^10.3.1",
"eslint": "^9.37.0",
"prettier": "^3.6.2",
"tsup": "^8.5.0",
"typescript": "^5.9.3",
"typescript-eslint": "^8.46.1",
"vitest": "^3.2.4"
},
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://github.com/sweepline/eslint-plugin-unused-imports.git"
},
"homepage": "https://github.com/sweepline/eslint-plugin-unused-imports",
"bugs": "https://github.com/sweepline/eslint-plugin-unused-imports/issues",
"scripts": {
"build": "tsup",
"test": "vitest",
"format": "prettier --write '**/*.[jt]s'",
"release": "bumpp"
}
}