Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add eslint-plugin-example-typed-linting #13

Merged
merged 4 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,519 changes: 2,037 additions & 482 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions packages/eslint-plugin-example-typed-linting/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lib
70 changes: 70 additions & 0 deletions packages/eslint-plugin-example-typed-linting/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# `eslint-plugin-example-typed-linting`

An example ESLint plugin showing typed linting with `@typescript-eslint/utils`.

For documentation on custom ESLint plugins with typescript-eslint, see: <https://typescript-eslint.io/developers/custom-rules>.

```js
// eslint.config.js
import eslint from '@eslint/js';
import exampleTypedLinting from 'eslint-plugin-example-typed-linting'
import tseslint from 'typescript-eslint';

export default tseslint.config(
{ ignores: ["lib"] },
eslint.configs.recommended,
...tseslint.configs.recommendedTypeChecked,
exampleTypedLinting.configs.recommended // 👈
{
languageOptions: {
parserOptions: {
projectService:true,
tsconfigRootDir: import.meta.dirname,
},
},
},
);
```

## Rules

<!-- begin auto-generated rules list -->

💭 Requires [type information](https://typescript-eslint.io/linting/typed-linting).

| Name | Description | 💭 |
| :----------------------------------------------------- | :------------------------ | :- |
| [no-loop-over-enums](docs/rules/no-loop-over-enums.md) | Avoid looping over enums. | 💭 |

<!-- end auto-generated rules list -->

## Development

To set up this individual package, `cd` to the path to it, then install dependencies:

```shell
cd path/to/eslint-plugin-example-typed-linting
npm i
```

Then build files into the `lib` directory with TypeScript:

```shell
npm run tsc
```

You'll then be able to run standard package scripts:

- `npm run docs`: Regenerates documentation using [`eslint-doc-generator`](https://github.com/bmish/eslint-doc-generator)
- `npm run docs --check`: Validates that documentation is generated and up-to-date.
- `npm run lint`: Linting this plugin itself with ESLint

### Testing

This example uses [Vitest](https://vitest.dev):

```shell
npm run test
```

Note that files don't need to have been built to the `lib` directory to run tests.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Avoid looping over enums (`example-typed-linting/no-loop-over-enums`)

💭 This rule requires [type information](https://typescript-eslint.io/linting/typed-linting).

<!-- end auto-generated rule header -->

Example rule that demonstrates banning `for-in` looping over `enum`s.

## Valid

```ts
const values = {};
for (const a in values) {
}
```

```ts
const values = [];
for (const a of values) {
}
```

## Invalid

```ts
enum Values {}
for (const a of values) {
}
```
21 changes: 21 additions & 0 deletions packages/eslint-plugin-example-typed-linting/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import eslint from '@eslint/js';
import eslintPlugin from 'eslint-plugin-eslint-plugin'
import tseslint from 'typescript-eslint';

export default tseslint.config(
{ ignores: ["lib"] },
eslint.configs.recommended,
...tseslint.configs.recommendedTypeChecked,
eslintPlugin.configs['flat/recommended'],
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{
languageOptions: {
parserOptions: {
projectService: {
allowDefaultProject: ["*.config.*"],
defaultProject: "tsconfig.json"
},
tsconfigRootDir: import.meta.dirname,
},
},
},
);
34 changes: 34 additions & 0 deletions packages/eslint-plugin-example-typed-linting/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"dependencies": {
"@typescript-eslint/utils": "8.0.0-alpha.59"
},
"devDependencies": {
"@eslint/js": "^9.8.0",
"@types/eslint": "^9.6.0",
"@types/eslint__js": "^8.42.3",
"@types/node": "^22.0.2",
"@typescript-eslint/rule-tester": "^8.0.0-alpha.59",
"eslint": "^9.8.0",
"eslint-doc-generator": "^1.7.1",
"eslint-plugin-eslint-plugin": "^6.2.0",
"typescript": "^5.5.4",
"typescript-eslint": "^8.0.0-alpha.59",
"vitest": "^2.0.4"
},
"exports": {
".": {
"types": "./lib/index.d.ts",
"default": "./lib/index.js"
}
},
"main": "lib/index.js",
"scripts": {
"docs": "eslint-doc-generator",
"lint": "eslint",
"tsc": "tsc",
"test": "vitest"
},
"name": "eslint-plugin-example-typed-linting",
"type": "commonjs",
"version": "0.0.0"
}
25 changes: 25 additions & 0 deletions packages/eslint-plugin-example-typed-linting/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { rules } from "./rules/index.js";

const { name, version } =
// `import`ing here would bypass the TSConfig's `"rootDir": "src"`
// eslint-disable-next-line @typescript-eslint/no-require-imports
require("../package.json") as typeof import("../package.json");

const plugin = {
configs: {
get recommended() {
return recommended;
},
},
meta: { name, version },
rules,
};

const recommended = {
plugins: {
"example-typed-linting": plugin,
},
rules,
};

export = plugin;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { rule as noLoopOverEnums } from "./no-loop-over-enum.js";

export const rules = {
"no-loop-over-enums": noLoopOverEnums,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import path from "node:path";
import tseslint from "typescript-eslint";
import { RuleTester } from "@typescript-eslint/rule-tester";
import * as vitest from "vitest";

import { rule } from "./no-loop-over-enum.js";

RuleTester.afterAll = vitest.afterAll;
RuleTester.it = vitest.it;
RuleTester.itOnly = vitest.it.only;
RuleTester.describe = vitest.describe;

const ruleTester = new RuleTester({
languageOptions: {
parser: tseslint.parser,
parserOptions: {
projectService: {
allowDefaultProject: ["*.ts*"],
defaultProject: "tsconfig.json",
},
tsconfigRootDir: path.join(__dirname, "../.."),
},
},
});

ruleTester.run("no-loop-over-enum", rule, {
valid: [
`enum Values {}`,
`for (const a in []) {}`,
`for (const a of []) {}`,
`
const values = {};
for (const a in values) {}
`,
`
const values = [];
for (const a of values) {}
`,
],
invalid: [
{
code: `
enum Values {}
for (const a in Values) {}
`,
errors: [
{
column: 27,
endColumn: 33,
line: 3,
endLine: 3,
messageId: "loopOverEnum",
},
],
},
],
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { ESLintUtils } from "@typescript-eslint/utils";
import * as ts from "typescript";

import { createRule } from "../utils.js";

export const rule = createRule({
create(context) {
const services = ESLintUtils.getParserServices(context);

return {
ForInStatement(node) {
const type = services.getTypeAtLocation(node.right);

if (type.symbol.flags & ts.SymbolFlags.Enum) {
context.report({
messageId: "loopOverEnum",
node: node.right,
});
}
},
};
},
meta: {
docs: {
description: "Avoid looping over enums.",
recommended: true,
requiresTypeChecking: true,
},
messages: {
loopOverEnum: "Do not loop over enums.",
},
type: "suggestion",
schema: [],
},
name: "no-loop-over-enum",
defaultOptions: [],
});
12 changes: 12 additions & 0 deletions packages/eslint-plugin-example-typed-linting/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { ESLintUtils } from "@typescript-eslint/utils";

export interface ExampleTypedLintingRuleDocs {
description: string;
recommended?: boolean;
requiresTypeChecking?: boolean;
}

export const createRule = ESLintUtils.RuleCreator<ExampleTypedLintingRuleDocs>(
(name) =>
`https://github.com/typescript-eslint/examples/tree/main/eslint-plugin-example-typed-linting/docs/${name}.md`
);
15 changes: 15 additions & 0 deletions packages/eslint-plugin-example-typed-linting/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"compilerOptions": {
"declaration": true,
"esModuleInterop": true,
"module": "NodeNext",
"outDir": "lib",
"resolveJsonModule": true,
"rootDir": "src",
"skipLibCheck": true,
"sourceMap": true,
"strict": true,
"target": "ES2022"
},
"include": ["src"]
}
7 changes: 7 additions & 0 deletions packages/eslint-plugin-example-typed-linting/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from "vitest/config";

export default defineConfig({
test: {
exclude: ["lib"],
},
});