-
-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for constructor call specifiers (first: new URL)
- Loading branch information
Showing
9 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
Empty file.
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,15 @@ | ||
// import.meta.url: URL in browsers, file: protocol in Node.js etc. | ||
|
||
// new URL('./exists.js'); | ||
new URL('https://example.org/url1.js'); | ||
new URL('file1.js', import.meta.url); | ||
new URL('./file2.js', import.meta.url); | ||
new URL('file3.js', 'https://example.org'); | ||
const baseUrl = 'https://example.org'; | ||
new URL('/', baseUrl); | ||
new URL(baseUrl); | ||
|
||
// TODO Implement? | ||
new Worker('worker1.js'); | ||
new Worker(new URL('worker2.js', import.meta.url)); | ||
new Worker('./worker3.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,3 @@ | ||
{ | ||
"name": "imports-constructor-call" | ||
} |
Empty file.
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,21 @@ | ||
import ts from 'typescript'; | ||
import { isConstructorCall } from '../../ast-helpers.js'; | ||
import { importVisitor as visit } from '../index.js'; | ||
|
||
export default visit( | ||
() => true, | ||
node => { | ||
// Pattern: new URL('specifier', import.meta.url) | ||
if (isConstructorCall(node, 'URL')) { | ||
if ( | ||
node.arguments.length === 2 && | ||
ts.isStringLiteralLike(node.arguments[0]) && | ||
ts.isPropertyAccessExpression(node.arguments[1]) && | ||
node.arguments[1].getText() === 'import.meta.url' | ||
) { | ||
const specifier = node.arguments[0].text; | ||
if (specifier) return { specifier }; | ||
} | ||
} | ||
} | ||
); |
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,21 @@ | ||
import assert from 'node:assert/strict'; | ||
import test from 'node:test'; | ||
import { main } from '../src/index.js'; | ||
import { resolve } from '../src/util/path.js'; | ||
import baseArguments from './helpers/baseArguments.js'; | ||
import baseCounters from './helpers/baseCounters.js'; | ||
|
||
const cwd = resolve('fixtures/imports-constructor-call'); | ||
|
||
test('Support various constructor calls', async () => { | ||
const { counters } = await main({ | ||
...baseArguments, | ||
cwd, | ||
}); | ||
|
||
assert.deepEqual(counters, { | ||
...baseCounters, | ||
processed: 4, | ||
total: 4, | ||
}); | ||
}); |