-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: more complete support for imports with extensions
- Loading branch information
Showing
18 changed files
with
272 additions
and
80 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
...s/cli/src/services/check-parser/__tests__/check-parser-fixtures/import-js-from-ts/dep1.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 @@ | ||
export const value = 'hello' |
1 change: 1 addition & 0 deletions
1
...s/cli/src/services/check-parser/__tests__/check-parser-fixtures/import-js-from-ts/dep2.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 @@ | ||
export const value = 'world' |
1 change: 1 addition & 0 deletions
1
...s/cli/src/services/check-parser/__tests__/check-parser-fixtures/import-js-from-ts/dep3.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 value = 3 |
3 changes: 3 additions & 0 deletions
3
...src/services/check-parser/__tests__/check-parser-fixtures/import-js-from-ts/entrypoint.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,3 @@ | ||
import { } from './dep1' | ||
import { } from './dep2.js' | ||
import { } from './dep3.js' |
1 change: 1 addition & 0 deletions
1
...li/src/services/check-parser/__tests__/check-parser-fixtures/no-import-ts-from-js/dep1.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 value = 'hello' |
1 change: 1 addition & 0 deletions
1
...li/src/services/check-parser/__tests__/check-parser-fixtures/no-import-ts-from-js/dep2.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 @@ | ||
export const value = 'world' |
4 changes: 4 additions & 0 deletions
4
.../services/check-parser/__tests__/check-parser-fixtures/no-import-ts-from-js/entrypoint.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,4 @@ | ||
import { } from './dep1' | ||
import { } from './dep1.ts' | ||
import { } from './dep1.js' | ||
import { } from './dep2' |
1 change: 1 addition & 0 deletions
1
...parser/__tests__/check-parser-fixtures/tsconfig-allow-importing-ts-extensions/src/dep1.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 value1 = 'value1' |
1 change: 1 addition & 0 deletions
1
...parser/__tests__/check-parser-fixtures/tsconfig-allow-importing-ts-extensions/src/dep2.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 value2 = 'value2' |
1 change: 1 addition & 0 deletions
1
...parser/__tests__/check-parser-fixtures/tsconfig-allow-importing-ts-extensions/src/dep3.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 value3 = 'value3' |
3 changes: 3 additions & 0 deletions
3
.../__tests__/check-parser-fixtures/tsconfig-allow-importing-ts-extensions/src/entrypoint.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,3 @@ | ||
export { value1 } from './dep1.ts' | ||
export { value2 } from './dep2.js' | ||
export { value3 } from './dep3' |
11 changes: 11 additions & 0 deletions
11
...rser/__tests__/check-parser-fixtures/tsconfig-allow-importing-ts-extensions/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,11 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "ES2022", | ||
"module": "NodeNext", | ||
"moduleResolution": "nodenext", | ||
"esModuleInterop": true, | ||
"baseUrl": ".", | ||
"noEmit": true, | ||
"allowImportingTsExtensions": true | ||
} | ||
} |
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
76 changes: 76 additions & 0 deletions
76
packages/cli/src/services/check-parser/package-files/extension.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,76 @@ | ||
import path from 'node:path' | ||
|
||
export type CoreExtension = '.js' | '.mjs' | '.cjs' | '.json' | ||
|
||
export const CoreExtensions: CoreExtension[] = ['.js', '.mjs', '.cjs', '.json'] | ||
|
||
type CoreExtensionMapping = { | ||
[key in CoreExtension]: string[] | ||
} | ||
|
||
/** | ||
* @see https://www.typescriptlang.org/docs/handbook/modules/reference.html#file-extension-substitution | ||
*/ | ||
export const tsCoreExtensionLookupOrder: CoreExtensionMapping = { | ||
'.js': ['.ts', '.tsx', '.js', '.jsx'], | ||
'.mjs': ['.mts', '.mjs'], | ||
'.cjs': ['.cts', '.cjs'], | ||
'.json': ['.json'], | ||
} | ||
|
||
export type TSExtension = '.ts' | '.mts' | '.tsx' | ||
|
||
export const TSExtensions: TSExtension[] = ['.ts', '.mts', '.tsx'] | ||
|
||
type TSExtensionMapping = { | ||
[key in TSExtension]: string[] | ||
} | ||
|
||
export const tsExtensionLookupOrder: TSExtensionMapping = { | ||
'.ts': ['.ts'], | ||
'.mts': ['.mts'], | ||
'.tsx': ['.tsx'], | ||
} | ||
|
||
function stripKnownSuffix (value: string, suffix: string): string { | ||
return value.substring(0, value.length - suffix.length) | ||
} | ||
|
||
export class FileExtPath { | ||
filePath: string | ||
ext: string | ||
|
||
private constructor (filePath: string, ext: string) { | ||
this.filePath = filePath | ||
this.ext = ext | ||
} | ||
|
||
static fromFilePath (filePath: string) { | ||
const ext = path.extname(filePath) | ||
return new FileExtPath(filePath, ext) | ||
} | ||
|
||
hasCoreExtension () { | ||
return this.ext in tsCoreExtensionLookupOrder | ||
} | ||
|
||
hasTypeScriptExtension () { | ||
return this.ext in tsExtensionLookupOrder | ||
} | ||
|
||
replaceExt (ext: string) { | ||
return stripKnownSuffix(this.filePath, this.ext) + ext | ||
} | ||
|
||
appendExt (ext: string) { | ||
return this.filePath + ext | ||
} | ||
|
||
resolve (...paths: string[]) { | ||
return path.resolve(this.filePath, ...paths) | ||
} | ||
|
||
self () { | ||
return this.filePath | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
packages/cli/src/services/check-parser/package-files/lookup.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,78 @@ | ||
import { PackageJsonFile } from './package-json-file' | ||
import { CoreExtension, FileExtPath, tsCoreExtensionLookupOrder, TSExtension, tsExtensionLookupOrder } from './extension' | ||
|
||
type Options = { | ||
plainJs?: boolean | ||
allowImportingTsExtensions?: boolean | ||
} | ||
|
||
export class LookupContext { | ||
#plainJs: boolean | ||
#allowImportingTsExtensions: boolean | ||
|
||
constructor (options: Options) { | ||
this.#plainJs = options.plainJs ?? false | ||
this.#allowImportingTsExtensions = options.allowImportingTsExtensions ?? false | ||
} | ||
|
||
static forFilePath (filePath: string, options?: Options) { | ||
return new LookupContext({ | ||
plainJs: FileExtPath.fromFilePath(filePath).hasCoreExtension(), | ||
allowImportingTsExtensions: options?.allowImportingTsExtensions, | ||
}) | ||
} | ||
|
||
switch (options: Options) { | ||
return new LookupContext({ | ||
plainJs: options.plainJs ?? this.#plainJs, | ||
allowImportingTsExtensions: options.allowImportingTsExtensions ?? this.#allowImportingTsExtensions, | ||
}) | ||
} | ||
|
||
collectLookupPaths (filePath: string): string[] { | ||
const extPath = FileExtPath.fromFilePath(filePath) | ||
|
||
if (this.#plainJs) { | ||
if (extPath.hasCoreExtension()) { | ||
return [extPath.self()] | ||
} | ||
|
||
return this.extlessCoreLookupPaths(extPath) | ||
} | ||
|
||
if (extPath.hasCoreExtension()) { | ||
const extensions = tsCoreExtensionLookupOrder[extPath.ext as CoreExtension] | ||
return extensions.map(ext => extPath.replaceExt(ext)) | ||
} | ||
|
||
if (this.#allowImportingTsExtensions) { | ||
if (extPath.hasTypeScriptExtension()) { | ||
const extensions = tsExtensionLookupOrder[extPath.ext as TSExtension] | ||
return extensions.map(ext => extPath.replaceExt(ext)) | ||
} | ||
} | ||
|
||
return this.extlessTSLookupPaths(extPath) | ||
} | ||
|
||
private extlessCoreLookupPaths (extPath: FileExtPath): string[] { | ||
return [ | ||
extPath.appendExt('.js'), | ||
extPath.appendExt('.mjs'), | ||
extPath.appendExt('.cjs'), | ||
extPath.appendExt('.json'), | ||
extPath.resolve(PackageJsonFile.FILENAME), | ||
extPath.resolve('index.js'), | ||
extPath.resolve('index.mjs'), | ||
extPath.resolve('index.cjs'), | ||
] | ||
} | ||
|
||
private extlessTSLookupPaths (extPath: FileExtPath): string[] { | ||
return this.extlessCoreLookupPaths(extPath).flatMap(filePath => { | ||
const extPath = FileExtPath.fromFilePath(filePath) | ||
const extensions = tsCoreExtensionLookupOrder[extPath.ext as CoreExtension] | ||
return extensions.map(ext => extPath.replaceExt(ext)) | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.