Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
feat(eslint-plugin-internal): add internal lint rule no-relative-paths-to-internal-packages#8596
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| import path from 'path'; | ||
| import { createRule } from '../util'; | ||
| export const REPO_ROOT = path.resolve(__dirname, '../../../..'); | ||
MemberAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. tried to import this from | ||
| export const PACKAGES_DIR = path.join(REPO_ROOT, 'packages'); | ||
| export default createRule({ | ||
| name: __filename, | ||
| meta: { | ||
| type: 'problem', | ||
| docs: { | ||
| recommended: 'recommended', | ||
| description: 'Disallow relative paths to internal packages', | ||
| }, | ||
| messages: { | ||
| noRelativePathsToInternalPackages: | ||
| 'Use absolute paths instead of relative paths to import modules in other internal packages.', | ||
| }, | ||
| schema: [], | ||
| fixable: 'code', | ||
| }, | ||
| defaultOptions: [], | ||
| create(context) { | ||
| return { | ||
| ImportDeclaration(node): void { | ||
| const importSource = node.source; | ||
| if ( | ||
| importSource.value.startsWith('@typescript-eslint') || | ||
| !importSource.value.startsWith('.') | ||
| ) { | ||
| return; | ||
| } | ||
| // The idea here is to check if the import source resolves to a different | ||
| // package than the package the file is currently in. This lets us not flag | ||
| // relative paths to modules inside a package called 'utils' but still flag | ||
| // if importing the '@typescript-eslint/utils' package with a relative path. | ||
| const pathOfFileFromPackagesDir = path.relative( | ||
| PACKAGES_DIR, | ||
| context.physicalFilename, | ||
| ); | ||
| const packageOfFile = pathOfFileFromPackagesDir.split(path.sep)[0]; | ||
| const absolutePathOfImport = path.resolve( | ||
| path.dirname(context.physicalFilename), | ||
| importSource.value, | ||
| ); | ||
| const pathOfImportFromPackagesDir = path.relative( | ||
| PACKAGES_DIR, | ||
| absolutePathOfImport, | ||
| ); | ||
| const packageOfImport = pathOfImportFromPackagesDir.split(path.sep)[0]; | ||
| if (packageOfImport !== packageOfFile) { | ||
| context.report({ | ||
| node: importSource, | ||
| messageId: 'noRelativePathsToInternalPackages', | ||
| fix: fixer => { | ||
| // Force the output path to be separated with '/' to get consistent | ||
| // results on windows. | ||
| const platformIndependentRelativePathOfImportFromPackagesDir = | ||
| pathOfImportFromPackagesDir.split(path.sep).join('/'); | ||
| return fixer.replaceText( | ||
| importSource, | ||
| `'@typescript-eslint/${platformIndependentRelativePathOfImportFromPackagesDir}'`, | ||
| ); | ||
| }, | ||
| }); | ||
| } | ||
| }, | ||
| }; | ||
| }, | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| import { RuleTester } from '@typescript-eslint/rule-tester'; | ||
| import path from 'path'; | ||
| import rule, { | ||
| PACKAGES_DIR, | ||
| } from '../../src/rules/no-relative-paths-to-internal-packages'; | ||
| const ruleTester = new RuleTester({ | ||
| parser: '@typescript-eslint/parser', | ||
| }); | ||
| ruleTester.run('no-relative-paths-to-internal-packages', rule, { | ||
| valid: [ | ||
| "import { parse } from '@typescript-eslint/typescript-estree';", | ||
| "import { something } from 'not/a/relative/path';", | ||
| { | ||
| filename: path.resolve( | ||
| PACKAGES_DIR, | ||
| 'eslint-plugin/src/rules/my-awesome-rule.ts', | ||
| ), | ||
| code: "import { something } from './utils';", | ||
| }, | ||
| { | ||
| code: "import type { ValueOf } from './utils';", | ||
| filename: path.resolve( | ||
| PACKAGES_DIR, | ||
| 'ast-spec/src/expression/AssignmentExpression/spec.ts', | ||
| ), | ||
| }, | ||
| { | ||
| code: "import type { ValueOf } from '../../utils';", | ||
| filename: path.resolve( | ||
| PACKAGES_DIR, | ||
| 'ast-spec/src/expression/AssignmentExpression/spec.ts', | ||
| ), | ||
| }, | ||
| { | ||
| code: "import type { ValueOf } from '../../../utils';", | ||
| filename: path.resolve( | ||
| PACKAGES_DIR, | ||
| 'ast-spec/src/expression/AssignmentExpression/spec.ts', | ||
| ), | ||
| }, | ||
| ], | ||
| invalid: [ | ||
| { | ||
| code: "import { parse } from '../../../typescript-estree';", | ||
| filename: path.resolve( | ||
| PACKAGES_DIR, | ||
| 'eslint-plugin/src/rules/my-awesome-rule.ts', | ||
| ), | ||
| output: `import { parse } from '@typescript-eslint/typescript-estree';`, | ||
| errors: [ | ||
| { | ||
| messageId: 'noRelativePathsToInternalPackages', | ||
| line: 1, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: "import { parse } from '../../../typescript-estree/inner-module';", | ||
| filename: path.resolve( | ||
| PACKAGES_DIR, | ||
| 'eslint-plugin/src/rules/my-awesome-rule.ts', | ||
| ), | ||
| output: `import { parse } from '@typescript-eslint/typescript-estree/inner-module';`, | ||
| errors: [ | ||
| { | ||
| messageId: 'noRelativePathsToInternalPackages', | ||
| line: 1, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: "import type { ValueOf } from '../../../../utils';", | ||
| output: "import type { ValueOf } from '@typescript-eslint/utils';", | ||
| filename: path.resolve( | ||
| PACKAGES_DIR, | ||
| 'ast-spec/src/expression/AssignmentExpression/spec.ts', | ||
| ), | ||
| errors: [ | ||
| { | ||
| messageId: 'noRelativePathsToInternalPackages', | ||
| line: 1, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: ` | ||
| import type { | ||
| MemberExpressionComputedName, | ||
| MemberExpressionNonComputedName, | ||
| } from '../../../types/src/generated/ast-spec'; | ||
| `, | ||
| output: ` | ||
| import type { | ||
| MemberExpressionComputedName, | ||
| MemberExpressionNonComputedName, | ||
| } from '@typescript-eslint/types/src/generated/ast-spec'; | ||
| `, | ||
| filename: path.resolve( | ||
| PACKAGES_DIR, | ||
| 'eslint-plugin/src/rules/prefer-find.ts', | ||
| ), | ||
| errors: [ | ||
| { | ||
| messageId: 'noRelativePathsToInternalPackages', | ||
| line: 5, | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }); |