-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from gajus/gajus/add-require-extension
feat: add require-extension rule
- Loading branch information
Showing
27 changed files
with
488 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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,54 @@ | ||
### `require-extension` | ||
|
||
Adds `.js` extension to all imports and exports. | ||
|
||
It resolves the following cases: | ||
|
||
#### Relative imports | ||
|
||
Relative imports that resolve to a file of the same name: | ||
|
||
```js | ||
import './foo'; // => import './foo.js'; | ||
``` | ||
|
||
Relative imports that resolve to an index file: | ||
|
||
```js | ||
import './foo'; // => import './foo/index.js'; | ||
``` | ||
|
||
The above examples would also work if the file extension was `.ts` or `.tsx`, i.e. | ||
|
||
```js | ||
import './foo'; // => import './foo.ts'; | ||
import './foo'; // => import './foo/index.tsx'; | ||
``` | ||
|
||
#### TypeScript paths | ||
|
||
For this to work, you have to [configure `import/resolver`](https://www.npmjs.com/package/eslint-import-resolver-typescript): | ||
|
||
```ts | ||
settings: { | ||
'import/resolver': { | ||
typescript: { | ||
project: path.resolve(__dirname, 'tsconfig.json'), | ||
}, | ||
}, | ||
}, | ||
``` | ||
|
||
Imports that resolve to a file of the same name: | ||
|
||
```js | ||
import { foo } from '@/foo'; // => import { foo } from '@/foo.js'; | ||
``` | ||
|
||
Imports that resolve to an index file: | ||
|
||
```js | ||
import { foo } from '@/foo'; // => import { foo } from '@/foo/index.js'; | ||
``` | ||
|
||
<!-- assertions requireExtension --> |
This file contains 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 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 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 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,201 @@ | ||
import { existsSync, lstatSync, readFileSync } from 'node:fs'; | ||
import { dirname, resolve } from 'node:path'; | ||
import { type TSESTree } from '@typescript-eslint/utils'; | ||
import { type RuleFixer } from '@typescript-eslint/utils/dist/ts-eslint'; | ||
import resolveImport from 'eslint-module-utils/resolve'; | ||
import { createRule } from '../utilities'; | ||
|
||
const extensions = ['.js', '.ts', '.tsx']; | ||
|
||
type Options = []; | ||
|
||
type MessageIds = 'extensionMissing'; | ||
|
||
const isExistingFile = (fileName: string) => { | ||
return existsSync(fileName) && lstatSync(fileName).isFile(); | ||
}; | ||
|
||
const fixRelativeImport = ( | ||
fixer: RuleFixer, | ||
node: TSESTree.ImportDeclaration, | ||
fileName: string, | ||
overrideExtension: boolean = true, | ||
) => { | ||
const importPath = resolve(dirname(fileName), node.source.value); | ||
|
||
for (const extension of extensions) { | ||
if (isExistingFile(importPath + extension)) { | ||
return fixer.replaceTextRange( | ||
node.source.range, | ||
`'${node.source.value + (overrideExtension ? '.js' : extension)}'`, | ||
); | ||
} | ||
} | ||
|
||
for (const extension of extensions) { | ||
if (isExistingFile(resolve(importPath, 'index') + extension)) { | ||
return fixer.replaceTextRange( | ||
node.source.range, | ||
`'${ | ||
node.source.value + '/index' + (overrideExtension ? '.js' : extension) | ||
}'`, | ||
); | ||
} | ||
} | ||
|
||
return null; | ||
}; | ||
|
||
const fixPathImport = ( | ||
fixer: RuleFixer, | ||
node: TSESTree.ImportDeclaration, | ||
fileName: string, | ||
aliasPath: string, | ||
resolvedImportPath: string, | ||
overrideExtension: boolean = true, | ||
) => { | ||
const importPath = node.source.value.replace(aliasPath, ''); | ||
|
||
for (const extension of extensions) { | ||
if (resolvedImportPath.endsWith(importPath + extension)) { | ||
return fixer.replaceTextRange( | ||
node.source.range, | ||
`'${node.source.value + (overrideExtension ? '.js' : extension)}'`, | ||
); | ||
} | ||
} | ||
|
||
for (const extension of extensions) { | ||
if (resolvedImportPath.endsWith(importPath + '/index' + extension)) { | ||
return fixer.replaceTextRange( | ||
node.source.range, | ||
`'${ | ||
node.source.value + '/index' + (overrideExtension ? '.js' : extension) | ||
}'`, | ||
); | ||
} | ||
} | ||
|
||
return null; | ||
}; | ||
|
||
type AliasPaths = { | ||
[key: string]: string[]; | ||
}; | ||
|
||
type TSConfig = { | ||
compilerOptions: { | ||
paths?: AliasPaths; | ||
}; | ||
}; | ||
|
||
const findAliasPath = (aliasPaths: AliasPaths, importPath: string) => { | ||
return Object.keys(aliasPaths).find((path) => { | ||
if (!path.endsWith('*')) { | ||
return false; | ||
} | ||
|
||
const pathWithoutWildcard = path.slice(0, -1); | ||
|
||
return importPath.startsWith(pathWithoutWildcard); | ||
}); | ||
}; | ||
|
||
const endsWith = (subject: string, needles: string[]) => { | ||
return needles.some((needle) => { | ||
return subject.endsWith(needle); | ||
}); | ||
}; | ||
|
||
export default createRule<Options, MessageIds>({ | ||
create: (context) => { | ||
return { | ||
ImportDeclaration: (node) => { | ||
const importPath = node.source.value; | ||
|
||
const importPathHasExtension = endsWith(importPath, extensions); | ||
|
||
if (importPathHasExtension) { | ||
return; | ||
} | ||
|
||
if (importPath.startsWith('.')) { | ||
context.report({ | ||
fix(fixer) { | ||
return fixRelativeImport(fixer, node, context.getFilename()); | ||
}, | ||
messageId: 'extensionMissing', | ||
node, | ||
}); | ||
|
||
return; | ||
} | ||
|
||
// @ts-expect-error we know this setting exists | ||
const project = (context.settings['import/resolver']?.typescript | ||
?.project ?? null) as string | null; | ||
|
||
if (typeof project !== 'string') { | ||
return; | ||
} | ||
|
||
const tsconfig: TSConfig = JSON.parse(readFileSync(project, 'utf8')); | ||
|
||
const paths = tsconfig?.compilerOptions?.paths; | ||
|
||
if (!paths) { | ||
return; | ||
} | ||
|
||
const aliasPath = findAliasPath(paths, importPath); | ||
|
||
if (!aliasPath) { | ||
return; | ||
} | ||
|
||
const aliasPathWithoutWildcard = aliasPath.slice(0, -1); | ||
|
||
if (!aliasPathWithoutWildcard) { | ||
throw new Error('Path without wildcard is empty'); | ||
} | ||
|
||
const resolvedImportPath: string | null = resolveImport( | ||
importPath, | ||
context, | ||
); | ||
|
||
if (!resolvedImportPath) { | ||
return; | ||
} | ||
|
||
context.report({ | ||
fix(fixer) { | ||
return fixPathImport( | ||
fixer, | ||
node, | ||
context.getFilename(), | ||
aliasPathWithoutWildcard, | ||
resolvedImportPath, | ||
); | ||
}, | ||
messageId: 'extensionMissing', | ||
node, | ||
}); | ||
}, | ||
}; | ||
}, | ||
defaultOptions: [], | ||
meta: { | ||
docs: { | ||
description: '', | ||
recommended: 'error', | ||
}, | ||
fixable: 'code', | ||
messages: { | ||
extensionMissing: 'Must include file extension "{{extension}}"', | ||
}, | ||
schema: [], | ||
type: 'layout', | ||
}, | ||
name: 'require-extension', | ||
}); |
This file contains 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 @@ | ||
{ | ||
"rules": { | ||
"@typescript-eslint/no-unused-vars": 0, | ||
"import/extensions": 0, | ||
"no-console": 0 | ||
} | ||
} |
This file contains 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 @@ | ||
export const foo = 'FOO'; |
This file contains 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 @@ | ||
import { foo } from '@/foo.js'; |
This file contains 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 @@ | ||
import { foo } from '@/foo'; |
This file contains 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,13 @@ | ||
{ | ||
"compilerOptions": { | ||
"baseUrl": ".", | ||
"paths": { | ||
"@/*": [ | ||
"*" | ||
] | ||
} | ||
}, | ||
"include": [ | ||
"." | ||
] | ||
} |
1 change: 1 addition & 0 deletions
1
tests/fixtures/requireExtension/pathsImportWithExtension/foo.ts
This file contains 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 @@ | ||
export const foo = 'FOO'; |
1 change: 1 addition & 0 deletions
1
tests/fixtures/requireExtension/pathsImportWithExtension/subject.ts
This file contains 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 @@ | ||
import { foo } from '@/foo.js'; |
13 changes: 13 additions & 0 deletions
13
tests/fixtures/requireExtension/pathsImportWithExtension/tsconfig.json
This file contains 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,13 @@ | ||
{ | ||
"compilerOptions": { | ||
"baseUrl": ".", | ||
"paths": { | ||
"@/*": [ | ||
"*" | ||
] | ||
} | ||
}, | ||
"include": [ | ||
"." | ||
] | ||
} |
1 change: 1 addition & 0 deletions
1
tests/fixtures/requireExtension/pathsImportWithIndex/foo/index.ts
This file contains 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 @@ | ||
export const foo = 'FOO'; |
1 change: 1 addition & 0 deletions
1
tests/fixtures/requireExtension/pathsImportWithIndex/subject-fixed.ts
This file contains 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 @@ | ||
import { foo } from '@/foo/index.js'; |
1 change: 1 addition & 0 deletions
1
tests/fixtures/requireExtension/pathsImportWithIndex/subject.ts
This file contains 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 @@ | ||
import { foo } from '@/foo'; |
13 changes: 13 additions & 0 deletions
13
tests/fixtures/requireExtension/pathsImportWithIndex/tsconfig.json
This file contains 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,13 @@ | ||
{ | ||
"compilerOptions": { | ||
"baseUrl": ".", | ||
"paths": { | ||
"@/*": [ | ||
"*" | ||
] | ||
} | ||
}, | ||
"include": [ | ||
"." | ||
] | ||
} |
This file contains 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 @@ | ||
export const foo = 'FOO'; |
1 change: 1 addition & 0 deletions
1
tests/fixtures/requireExtension/relativeImport/subject-fixed.ts
This file contains 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 @@ | ||
import { foo } from './foo.js'; |
This file contains 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 @@ | ||
import { foo } from './foo'; |
1 change: 1 addition & 0 deletions
1
tests/fixtures/requireExtension/relativeImportWithExtension/foo.ts
This file contains 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 @@ | ||
export const foo = 'FOO'; |
1 change: 1 addition & 0 deletions
1
tests/fixtures/requireExtension/relativeImportWithExtension/subject.ts
This file contains 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 @@ | ||
import { foo } from './foo.js'; |
1 change: 1 addition & 0 deletions
1
tests/fixtures/requireExtension/relativeImportWithIndex/foo/index.ts
This file contains 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 @@ | ||
export const foo = 'FOO'; |
1 change: 1 addition & 0 deletions
1
tests/fixtures/requireExtension/relativeImportWithIndex/subject-fixed.ts
This file contains 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 @@ | ||
import { foo } from './foo/index.js'; |
1 change: 1 addition & 0 deletions
1
tests/fixtures/requireExtension/relativeImportWithIndex/subject.ts
This file contains 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 @@ | ||
import { foo } from './foo'; |
Oops, something went wrong.