Skip to content

Commit

Permalink
Run tests using both regular eslint/no-unused-vars and @typescript-es…
Browse files Browse the repository at this point in the history
…lint/no-unused-vars

I also setup a prettier config here and ran it because i didnt think.
  • Loading branch information
sweepline committed Feb 13, 2024
1 parent 9489fb3 commit b0d02fd
Show file tree
Hide file tree
Showing 14 changed files with 5,193 additions and 5,499 deletions.
11 changes: 5 additions & 6 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
---
name: Bug report
about: Create a report to help us improve
title: ''
labels: ''
assignees: ''

title: ""
labels: ""
assignees: ""
---

# Please follow the general troubleshooting steps first:

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_.

Now if something is not marked an import and being removed by the autofixer, it is an issue I can do something about.

Expand Down
10 changes: 10 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {
tabWidth: 4,
semi: true, // Trailing semicolons
trailingComma: "all",
singleQuote: false,
quoteProps: "as-needed",
bracketSpacing: true,
printWidth: 100, // Line width (this fit my 1440p screen at a half-screen window)
arrowParens: "always",
};
35 changes: 17 additions & 18 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
{
// 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"
}
]
}

31 changes: 18 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ Find and remove unused es6 module imports. It works by splitting up the `no-unus

## _Versions_

* 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
- Version 2.x.x is for eslint 8 with @typescript-eslint/eslint-plugin 5
- Version 1.x.x is for eslint 6 and 7.

## _Important for version 1.1_

Expand Down Expand Up @@ -51,18 +51,23 @@ Then configure the rules you want to use under the rules section. I can recommen

```jsonc
{
"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": "^_",
},
],
},
}
```

## Supported Rules

- `no-unused-imports`
- `no-unused-vars`
- `no-unused-imports`
- `no-unused-vars`
92 changes: 92 additions & 0 deletions lib/__test__/cases.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
module.exports = {
valid: [
{
code: `
import x from "package";
import { a, b } from "./utils";
import y from "package";
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);
`,
},
],
};
53 changes: 53 additions & 0 deletions lib/__test__/no-unused-imports-ts.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
RuleTester = require("eslint").RuleTester;

const ruleTester = new RuleTester({
parser: require.resolve('@typescript-eslint/parser'),
parserOptions: {
ecmaVersion: 2015,
sourceType: "module",
},
});

const cases = require("./cases");

global.eslintUnusedImportsForceLoadJSLint = "false";
let rule;
jest.isolateModules(() => {
rule = require("../rules/no-unused-imports");
});
ruleTester.run("no-unused-imports-js", rule, cases);
ruleTester.run("no-unused-imports-ts", rule, {
valid: [
{
code: `
import x from "package";
import { a, b } from "./utils";
import y from "package";
import TType from "Package";
const c: TType = a() + b + x() + y();
`,
},
],

invalid: [
{
code: `
import x from "package";
import { a, b } from "./utils";
import y from "package";
import TType from "Package";
const c = a() + b + x() + y();
`,
errors: ["'TType' is defined but never used."],
output: `
import x from "package";
import { a, b } from "./utils";
import y from "package";
const c = a() + b + x() + y();
`,
},
],
});
103 changes: 10 additions & 93 deletions lib/__test__/no-unused-imports.test.js
Original file line number Diff line number Diff line change
@@ -1,97 +1,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);
8 changes: 4 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ const noUnusedImports = require("./rules/no-unused-imports");

// import all rules in lib/rules
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,
};
Loading

0 comments on commit b0d02fd

Please sign in to comment.