-
-
Notifications
You must be signed in to change notification settings - Fork 60
dx: Lint rule for imports #2345
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
Open
aleksanderkatan
wants to merge
13
commits into
main
Choose a base branch
from
dx/lint-rule-for-imports
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
c9f0366
Duplicate eslint plugin
aleksanderkatan 3c52c10
Strip the plugin of unused code, enable plugin in config
aleksanderkatan 2916f67
Implement noUselessPathSegments
aleksanderkatan f2b4ca9
Implement noLongImports rule
aleksanderkatan 64dc8ad
nr fix
aleksanderkatan 93d0dda
Merge remote-tracking branch 'origin/main' into dx/lint-rule-for-imports
aleksanderkatan d4f5c1c
Fix lockfile
aleksanderkatan 9b70f14
Update readme
aleksanderkatan 3d5d420
Make tests more portable
aleksanderkatan c6f1c57
Add windows compat
aleksanderkatan f375895
Remove the fix tag
aleksanderkatan 4c4a915
Simplify package.json
aleksanderkatan 8baf753
Readd version to package.json
aleksanderkatan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| <div align="center"> | ||
|
|
||
| # eslint-plugin-internal | ||
|
|
||
| Internal ESLint rules used by this repository. | ||
|
|
||
| </div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| { | ||
| "name": "eslint-plugin-internal", | ||
| "version": "0.10.0", | ||
| "private": true, | ||
| "license": "MIT", | ||
| "type": "module", | ||
| "main": "./src/index.ts", | ||
| "scripts": { | ||
| "test:types": "pnpm tsc --p ./tsconfig.json --noEmit", | ||
| "test": "vitest" | ||
| }, | ||
| "dependencies": { | ||
| "@typescript-eslint/utils": "^8.57.2" | ||
| }, | ||
| "devDependencies": { | ||
| "@types/node": "catalog:types", | ||
| "@typescript-eslint/rule-tester": "^8.57.2", | ||
| "eslint": "^9.39.2", | ||
| "typescript": "^5.9.3", | ||
| "vitest": "^4.0.17" | ||
| }, | ||
| "peerDependencies": { | ||
| "eslint": "^9.0.0" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import pkg from '../package.json' with { type: 'json' }; | ||
| import type { TSESLint } from '@typescript-eslint/utils'; | ||
| import { noUselessPathSegments } from './rules/noUselessPathSegments.ts'; | ||
| import { noLongImports } from './rules/noLongImports.ts'; | ||
|
|
||
| const plugin = { | ||
| meta: { | ||
| name: pkg.name, | ||
| version: pkg.version, | ||
| }, | ||
| rules: { | ||
| 'no-useless-path-segments': noUselessPathSegments, | ||
| 'no-long-imports': noLongImports, | ||
| }, | ||
| } satisfies TSESLint.FlatConfig.Plugin; | ||
|
|
||
| export default plugin; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import { ESLintUtils } from '@typescript-eslint/utils'; | ||
|
|
||
| export const createRule = ESLintUtils.RuleCreator( | ||
| () => `https://docs.swmansion.com/TypeGPU/getting-started/`, | ||
| ); |
32 changes: 32 additions & 0 deletions
32
packages/eslint-plugin-internal/src/rules/noLongImports.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import { createRule } from '../ruleCreator.ts'; | ||
|
|
||
| export const noLongImports = createRule({ | ||
| name: 'no-long-imports', | ||
| meta: { | ||
| type: 'suggestion', | ||
| docs: { | ||
| description: 'Disallow long import paths (to be used in TypeGPU examples), except common.', | ||
| }, | ||
| messages: { | ||
| unexpected: | ||
| "Import path '{{path}}' probably won't work on StackBlitz, use imports from packages instead", | ||
| }, | ||
| schema: [], | ||
| }, | ||
| defaultOptions: [], | ||
|
|
||
| create(context) { | ||
| return { | ||
| ImportDeclaration(node) { | ||
| const importPath = node.source.value; | ||
| if (importPath.startsWith('../../') && !importPath.startsWith('../../common/')) { | ||
| context.report({ | ||
| node, | ||
| messageId: 'unexpected', | ||
| data: { path: importPath }, | ||
| }); | ||
| } | ||
| }, | ||
| }; | ||
| }, | ||
| }); | ||
52 changes: 52 additions & 0 deletions
52
packages/eslint-plugin-internal/src/rules/noUselessPathSegments.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import { createRule } from '../ruleCreator.ts'; | ||
| import * as path from 'node:path'; | ||
|
|
||
| export const noUselessPathSegments = createRule({ | ||
| name: 'no-useless-path-segments', | ||
| meta: { | ||
| type: 'suggestion', | ||
| fixable: 'code', | ||
| docs: { | ||
| description: 'Disallow redundant parent folder lookups in relative import paths', | ||
| }, | ||
| messages: { | ||
| redundant: "Import path '{{path}}' can be simplified to '{{simplified}}'", | ||
| }, | ||
| schema: [], | ||
| }, | ||
| defaultOptions: [], | ||
|
|
||
| create(context) { | ||
| return { | ||
| ImportDeclaration(node) { | ||
| const importPath = node.source.value; | ||
| if (!importPath.startsWith('.')) { | ||
| return; | ||
| } | ||
|
|
||
| const filename = context.filename; // e.g. `/Users/me/typegpu-monorepo/packages/typegpu/tests/buffer.test.ts` | ||
| const dir = path.dirname(filename); // e.g. `/Users/me/typegpu-monorepo/packages/typegpu/tests` | ||
| const resolved = path.resolve(dir, importPath); // e.g. `/Users/me/typegpu-monorepo/packages/typegpu/src/data/index.ts` | ||
| let simplified = path | ||
| .relative(dir, resolved) // e.g. `../src/data/index.ts`, or `subfolder/helper.ts` | ||
| .replaceAll('\\', '/'); // Windows compatibility | ||
|
|
||
| if (!simplified.startsWith('..')) { | ||
| simplified = `./${simplified}`; | ||
| } | ||
aleksanderkatan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if (importPath !== simplified) { | ||
| context.report({ | ||
| node, | ||
| messageId: 'redundant', | ||
| data: { path: importPath, simplified }, | ||
| fix(fixer) { | ||
| const quote = context.sourceCode.getText(node.source)[0]; | ||
| return fixer.replaceText(node.source, `${quote}${simplified}${quote}`); | ||
| }, | ||
| }); | ||
| } | ||
| }, | ||
| }; | ||
| }, | ||
| }); | ||
24 changes: 24 additions & 0 deletions
24
packages/eslint-plugin-internal/tests/rules/noLongImports.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import { describe } from 'vitest'; | ||
| import { ruleTester } from '../utils/ruleTester.ts'; | ||
| import { noLongImports } from '../../src/rules/noLongImports.ts'; | ||
|
|
||
| describe('noLongImports', () => { | ||
| ruleTester.run('noLongImports', noLongImports, { | ||
| valid: [ | ||
| { code: "import item from './file.ts';" }, | ||
| { code: "import item from '../file.ts';" }, | ||
| { code: "import item from '../../common/file.ts';" }, | ||
| ], | ||
| invalid: [ | ||
| { | ||
| code: "import item from '../../file.ts';", | ||
| errors: [ | ||
| { | ||
| messageId: 'unexpected', | ||
| data: { path: '../../file.ts' }, | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }); | ||
| }); |
66 changes: 66 additions & 0 deletions
66
packages/eslint-plugin-internal/tests/rules/noUselessPathSegments.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import { describe } from 'vitest'; | ||
| import { ruleTester } from '../utils/ruleTester.ts'; | ||
| import { noUselessPathSegments } from '../../src/rules/noUselessPathSegments.ts'; | ||
| import path from 'path'; | ||
|
|
||
| const filename = path.join(process.cwd(), 'packages', 'typegpu', 'tests', 'buffer.test.ts'); | ||
aleksanderkatan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| describe('noUselessPathSegments', () => { | ||
| ruleTester.run('noUselessPathSegments', noUselessPathSegments, { | ||
| valid: [ | ||
| { code: "import item from './file.ts';", filename }, | ||
| { code: "import item from '../file.ts';", filename }, | ||
| { code: "import item from '../../file.ts';", filename }, | ||
| { code: "import item from '../folder/file.ts';", filename }, | ||
|
|
||
| { code: "import item from 'eslint-plugin-typegpu';", filename }, | ||
| { code: "import item from '@eslint-plugin/typegpu';", filename }, | ||
| ], | ||
| invalid: [ | ||
| { | ||
| code: "import item from '../tests/file.ts';", | ||
| filename, | ||
| errors: [ | ||
| { | ||
| messageId: 'redundant', | ||
| data: { path: '../tests/file.ts', simplified: './file.ts' }, | ||
| }, | ||
| ], | ||
| output: "import item from './file.ts';", | ||
| }, | ||
| { | ||
| code: 'import item from "../tests/file.ts";', | ||
| filename, | ||
| errors: [ | ||
| { | ||
| messageId: 'redundant', | ||
| data: { path: '../tests/file.ts', simplified: './file.ts' }, | ||
| }, | ||
| ], | ||
| output: 'import item from "./file.ts";', | ||
| }, | ||
| { | ||
| code: "import item from './../file.ts';", | ||
| filename, | ||
| errors: [ | ||
| { | ||
| messageId: 'redundant', | ||
| data: { path: './../file.ts', simplified: '../file.ts' }, | ||
| }, | ||
| ], | ||
| output: "import item from '../file.ts';", | ||
| }, | ||
| { | ||
| code: "import item from '../../typegpu/folder/file.ts';", | ||
| filename, | ||
| errors: [ | ||
| { | ||
| messageId: 'redundant', | ||
| data: { path: '../../typegpu/folder/file.ts', simplified: '../folder/file.ts' }, | ||
| }, | ||
| ], | ||
| output: "import item from '../folder/file.ts';", | ||
| }, | ||
| ], | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import { RuleTester } from '@typescript-eslint/rule-tester'; | ||
| import { afterAll, describe, it } from 'vitest'; | ||
|
|
||
| // RuleTester relies on global hooks for tests. | ||
| // Vitest doesn't make the hooks available globally, so we need to bind them. | ||
| RuleTester.describe = describe; | ||
| RuleTester.it = it; | ||
| RuleTester.afterAll = afterAll; | ||
|
|
||
| export const ruleTester = new RuleTester(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "extends": "../../tsconfig.base.json", | ||
| "compilerOptions": { | ||
| "types": ["node"] | ||
| }, | ||
| "include": ["src/**/*", "tests/**/*"], | ||
| "exclude": ["node_modules"] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import { defineConfig } from 'vitest/config'; | ||
|
|
||
| export default defineConfig({ | ||
| test: { | ||
| include: ['tests/**/*.test.ts'], | ||
| environment: 'node', | ||
| }, | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.