-
-
Notifications
You must be signed in to change notification settings - Fork 35
feat(correct-ts-specifiers): use JSSG
#415
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| schema_version: "1.0" | ||
| name: "@nodejs/correct-ts-specifiers" | ||
| version: "1.0.0" | ||
| description: "Replace erroneous 'js' or omitted file extensions of import specifiers in TypeScript files." | ||
| author: "Jacob Smith" | ||
| license: "MIT" | ||
| workflow: "workflow.yaml" | ||
| category: "migration" | ||
|
|
||
| targets: | ||
| languages: | ||
| - "javascript" | ||
| - "typescript" | ||
|
|
||
| keywords: | ||
| - "transformation" | ||
| - "migration" | ||
| - "esm" | ||
| - "typescript" | ||
|
|
||
| registry: | ||
| access: "public" | ||
| visibility: "public" |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -4,13 +4,16 @@ import { fileURLToPath } from 'node:url'; | |||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| import type { FSAbsolutePath } from './index.d.ts'; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| type FSAccess = typeof import('node:fs/promises').access; | ||||||||||||||||||||||||||||||||||||||||||||
| type FSAccess = typeof import('fs').promises.access; | ||||||||||||||||||||||||||||||||||||||||||||
| type FExists = typeof import('./fexists.ts').fexists; | ||||||||||||||||||||||||||||||||||||||||||||
| type ResolveSpecifier = typeof import('./resolve-specifier.ts').resolveSpecifier; | ||||||||||||||||||||||||||||||||||||||||||||
| type ResolveSpecifier = | ||||||||||||||||||||||||||||||||||||||||||||
| typeof import('./resolve-specifier.ts').resolveSpecifier; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| const RESOLVED_SPECIFIER_ERR = 'Resolved specifier did not match expected'; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| describe('fexists', { concurrency: false /* concurrency clobbers `before`s */ }, () => { | ||||||||||||||||||||||||||||||||||||||||||||
| describe('fexists', { | ||||||||||||||||||||||||||||||||||||||||||||
| concurrency: false /* concurrency clobbers `before`s */, | ||||||||||||||||||||||||||||||||||||||||||||
| }, () => { | ||||||||||||||||||||||||||||||||||||||||||||
| const parentPath = '/tmp/test.ts'; | ||||||||||||||||||||||||||||||||||||||||||||
| const constants = { F_OK: null }; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -20,10 +23,10 @@ describe('fexists', { concurrency: false /* concurrency clobbers `before`s */ }, | |||||||||||||||||||||||||||||||||||||||||||
| before(() => { | ||||||||||||||||||||||||||||||||||||||||||||
| const access = mock.fn<FSAccess>(); | ||||||||||||||||||||||||||||||||||||||||||||
| ({ mock: mock__access } = access); | ||||||||||||||||||||||||||||||||||||||||||||
| mock.module('node:fs/promises', { | ||||||||||||||||||||||||||||||||||||||||||||
| mock.module('fs', { | ||||||||||||||||||||||||||||||||||||||||||||
| namedExports: { | ||||||||||||||||||||||||||||||||||||||||||||
| access, | ||||||||||||||||||||||||||||||||||||||||||||
| constants, | ||||||||||||||||||||||||||||||||||||||||||||
| promises: { access }, | ||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
23
to
31
Member
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. Heads up: I subsequently realised
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -34,16 +37,18 @@ describe('fexists', { concurrency: false /* concurrency clobbers `before`s */ }, | |||||||||||||||||||||||||||||||||||||||||||
| resolveSpecifier, | ||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||
| mock__resolveSpecifier.mockImplementation(function MOCK__resolveSpecifier(_pp, specifier) { | ||||||||||||||||||||||||||||||||||||||||||||
| return specifier; | ||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||
| mock__resolveSpecifier.mockImplementation( | ||||||||||||||||||||||||||||||||||||||||||||
| function MOCK__resolveSpecifier(_pp, specifier) { | ||||||||||||||||||||||||||||||||||||||||||||
| return specifier; | ||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| describe('when the file exists', () => { | ||||||||||||||||||||||||||||||||||||||||||||
| let fexists: FExists; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| before(async () => { | ||||||||||||||||||||||||||||||||||||||||||||
| mock__access.mockImplementation(async function MOCK_access() { }); | ||||||||||||||||||||||||||||||||||||||||||||
| mock__access.mockImplementation(async function MOCK_access() {}); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| ({ fexists } = await import('./fexists.ts')); | ||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -59,21 +64,33 @@ describe('fexists', { concurrency: false /* concurrency clobbers `before`s */ }, | |||||||||||||||||||||||||||||||||||||||||||
| ) as FSAbsolutePath; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| assert.equal(await fexists(parentUrl, specifier), true); | ||||||||||||||||||||||||||||||||||||||||||||
| assert.equal(mock__access.calls[0].arguments[0], specifier, RESOLVED_SPECIFIER_ERR); | ||||||||||||||||||||||||||||||||||||||||||||
| assert.equal( | ||||||||||||||||||||||||||||||||||||||||||||
| mock__access.calls[0].arguments[0], | ||||||||||||||||||||||||||||||||||||||||||||
| specifier, | ||||||||||||||||||||||||||||||||||||||||||||
| RESOLVED_SPECIFIER_ERR, | ||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| it('should return `true` for a relative specifier', async () => { | ||||||||||||||||||||||||||||||||||||||||||||
| const specifier = 'exists.js'; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| assert.equal(await fexists(parentPath, specifier), true); | ||||||||||||||||||||||||||||||||||||||||||||
| assert.equal(mock__access.calls[0].arguments[0], specifier, RESOLVED_SPECIFIER_ERR); | ||||||||||||||||||||||||||||||||||||||||||||
| assert.equal( | ||||||||||||||||||||||||||||||||||||||||||||
| mock__access.calls[0].arguments[0], | ||||||||||||||||||||||||||||||||||||||||||||
| specifier, | ||||||||||||||||||||||||||||||||||||||||||||
| RESOLVED_SPECIFIER_ERR, | ||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| it('should return `true` for specifier with a query parameter', async () => { | ||||||||||||||||||||||||||||||||||||||||||||
| const specifier = 'exists.js?v=1'; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| assert.equal(await fexists(parentPath, specifier), true); | ||||||||||||||||||||||||||||||||||||||||||||
| assert.equal(mock__access.calls[0].arguments[0], specifier, RESOLVED_SPECIFIER_ERR); | ||||||||||||||||||||||||||||||||||||||||||||
| assert.equal( | ||||||||||||||||||||||||||||||||||||||||||||
| mock__access.calls[0].arguments[0], | ||||||||||||||||||||||||||||||||||||||||||||
| specifier, | ||||||||||||||||||||||||||||||||||||||||||||
| RESOLVED_SPECIFIER_ERR, | ||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| it('should return `true` for an absolute specifier', async () => { | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -86,7 +103,10 @@ describe('fexists', { concurrency: false /* concurrency clobbers `before`s */ }, | |||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| it('should return `true` for a URL', async () => { | ||||||||||||||||||||||||||||||||||||||||||||
| assert.equal(await fexists(parentPath, 'file://localhost/foo/exists.js'), true); | ||||||||||||||||||||||||||||||||||||||||||||
| assert.equal( | ||||||||||||||||||||||||||||||||||||||||||||
| await fexists(parentPath, 'file://localhost/foo/exists.js'), | ||||||||||||||||||||||||||||||||||||||||||||
| true, | ||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||
| assert.equal( | ||||||||||||||||||||||||||||||||||||||||||||
| mock__access.calls[0].arguments[0], | ||||||||||||||||||||||||||||||||||||||||||||
| 'file://localhost/foo/exists.js', | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -114,34 +134,54 @@ describe('fexists', { concurrency: false /* concurrency clobbers `before`s */ }, | |||||||||||||||||||||||||||||||||||||||||||
| const specifier = 'noexists.js'; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| assert.equal(await fexists(parentPath, specifier), false); | ||||||||||||||||||||||||||||||||||||||||||||
| assert.equal(mock__access.calls[0].arguments[0], specifier, RESOLVED_SPECIFIER_ERR); | ||||||||||||||||||||||||||||||||||||||||||||
| assert.equal( | ||||||||||||||||||||||||||||||||||||||||||||
| mock__access.calls[0].arguments[0], | ||||||||||||||||||||||||||||||||||||||||||||
| specifier, | ||||||||||||||||||||||||||||||||||||||||||||
| RESOLVED_SPECIFIER_ERR, | ||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| it('should return `false` for a relative specifier', async () => { | ||||||||||||||||||||||||||||||||||||||||||||
| const specifier = 'noexists.js?v=1'; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| assert.equal(await fexists(parentPath, specifier), false); | ||||||||||||||||||||||||||||||||||||||||||||
| assert.equal(mock__access.calls[0].arguments[0], specifier, RESOLVED_SPECIFIER_ERR); | ||||||||||||||||||||||||||||||||||||||||||||
| assert.equal( | ||||||||||||||||||||||||||||||||||||||||||||
| mock__access.calls[0].arguments[0], | ||||||||||||||||||||||||||||||||||||||||||||
| specifier, | ||||||||||||||||||||||||||||||||||||||||||||
| RESOLVED_SPECIFIER_ERR, | ||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| it('should return `false` for an absolute specifier', async () => { | ||||||||||||||||||||||||||||||||||||||||||||
| const specifier = '/tmp/foo/noexists.js'; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| assert.equal(await fexists(parentPath, specifier), false); | ||||||||||||||||||||||||||||||||||||||||||||
| assert.equal(mock__access.calls[0].arguments[0], specifier, RESOLVED_SPECIFIER_ERR); | ||||||||||||||||||||||||||||||||||||||||||||
| assert.equal( | ||||||||||||||||||||||||||||||||||||||||||||
| mock__access.calls[0].arguments[0], | ||||||||||||||||||||||||||||||||||||||||||||
| specifier, | ||||||||||||||||||||||||||||||||||||||||||||
| RESOLVED_SPECIFIER_ERR, | ||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| it('should return `false` for a URL specifier', async () => { | ||||||||||||||||||||||||||||||||||||||||||||
| const specifier = 'file://localhost/foo/noexists.js'; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| assert.equal(await fexists(parentPath, specifier), false); | ||||||||||||||||||||||||||||||||||||||||||||
| assert.equal(mock__access.calls[0].arguments[0], specifier, RESOLVED_SPECIFIER_ERR); | ||||||||||||||||||||||||||||||||||||||||||||
| assert.equal( | ||||||||||||||||||||||||||||||||||||||||||||
| mock__access.calls[0].arguments[0], | ||||||||||||||||||||||||||||||||||||||||||||
| specifier, | ||||||||||||||||||||||||||||||||||||||||||||
| RESOLVED_SPECIFIER_ERR, | ||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| it('should return `false` when the specifier can’t be resolved', async () => { | ||||||||||||||||||||||||||||||||||||||||||||
| mock__resolveSpecifier.mockImplementationOnce(function MOCK__resolveSpecifier(_pp, _specifier) { | ||||||||||||||||||||||||||||||||||||||||||||
| throw Object.assign(new Error('ERR_MODULE_NOT_FOUND'), { code: 'ERR_MODULE_NOT_FOUND' }); | ||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||
| mock__resolveSpecifier.mockImplementationOnce( | ||||||||||||||||||||||||||||||||||||||||||||
| function MOCK__resolveSpecifier(_pp, _specifier) { | ||||||||||||||||||||||||||||||||||||||||||||
| throw Object.assign(new Error('ERR_MODULE_NOT_FOUND'), { | ||||||||||||||||||||||||||||||||||||||||||||
| code: 'ERR_MODULE_NOT_FOUND', | ||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| assert.equal(await fexists(parentPath, 'noexists'), false); | ||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,9 @@ | ||
| import { pathToFileURL } from 'node:url'; | ||
|
Check failure on line 1 in recipes/correct-ts-specifiers/src/get-not-found-url.ts
|
||
|
|
||
| import type { FSAbsolutePath, ResolvedSpecifier } from './index.d.ts'; | ||
|
|
||
| export const getNotFoundUrl = (err: NodeJS.ErrnoException & { url?: FSAbsolutePath }) => | ||
| pathToFileURL(err?.url ?? err.message.split("'")[1])?.href as ResolvedSpecifier; | ||
| export const getNotFoundUrl = ( | ||
| err: NodeJS.ErrnoException & { url?: FSAbsolutePath }, | ||
|
Check failure on line 6 in recipes/correct-ts-specifiers/src/get-not-found-url.ts
|
||
| ) => | ||
| pathToFileURL(err?.url ?? err.message.split("'")[1]) | ||
| ?.href as ResolvedSpecifier; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you want
Node ignores type imports, so that doesn't cause the module to be adversely loaded into the ModuleCache.