-
-
Notifications
You must be signed in to change notification settings - Fork 17
Feat(node url to whatwg url
)
#172
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
Open
AugustinMauroy
wants to merge
55
commits into
main
Choose a base branch
from
feat(`node-url-to-whatwg-url`)
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 14 commits
Commits
Show all changes
55 commits
Select commit
Hold shift + click to select a range
cdcdffe
fea(`node-url-to-whatwg-url`): scaffold codemod
AugustinMauroy 3addfb3
url-parse(`node-url-to-whatwg-url`): setup `url-parse`
AugustinMauroy 04174b8
WIP
AugustinMauroy 4249d8e
url-parse(`node-url-to-whatwg-url`): introduce
AugustinMauroy 873775a
clean
AugustinMauroy 4cf1c36
WIP
AugustinMauroy ab18017
fix: ts
AugustinMauroy 4b60d03
fix: windows
AugustinMauroy b289b2d
use resolve utility
AugustinMauroy 50193e0
add more supported case for `url-format`
AugustinMauroy 264a1f2
test: add more case
AugustinMauroy c60c073
add support of semicolon
AugustinMauroy c5ce549
Update package.json
AugustinMauroy cedf9bc
Revert "Update package.json"
AugustinMauroy 0adc023
update
AugustinMauroy 599fff4
fea(`node-url-to-whatwg-url`): scaffold codemod
AugustinMauroy a4ff1db
url-parse(`node-url-to-whatwg-url`): setup `url-parse`
AugustinMauroy 4aeffdd
WIP
AugustinMauroy 0b1f00a
url-parse(`node-url-to-whatwg-url`): introduce
AugustinMauroy 729f5f8
clean
AugustinMauroy 0914a53
WIP
AugustinMauroy d8debdf
fix: ts
AugustinMauroy b431aa4
fix: windows
AugustinMauroy 25f7df2
use resolve utility
AugustinMauroy 36f3b79
add more supported case for `url-format`
AugustinMauroy 821e3ee
test: add more case
AugustinMauroy 0713b1e
add support of semicolon
AugustinMauroy 4b06973
Update package.json
AugustinMauroy 9eb2c0d
Revert "Update package.json"
AugustinMauroy b893428
update
AugustinMauroy 7c45240
Merge branch 'feat(`node-url-to-whatwg-url`)' of https://github.com/n…
AugustinMauroy 3694442
update logic
AugustinMauroy e571ce7
Update url-format.ts
AugustinMauroy d240d5a
Update url-parse.ts
AugustinMauroy c128063
chore: remove `@next` and clean (#176)
AugustinMauroy 9467f6c
fea(`node-url-to-whatwg-url`): scaffold codemod
AugustinMauroy 4430f7f
url-parse(`node-url-to-whatwg-url`): setup `url-parse`
AugustinMauroy 01247a7
WIP
AugustinMauroy e1b3415
url-parse(`node-url-to-whatwg-url`): introduce
AugustinMauroy 68fe603
clean
AugustinMauroy 1c6a884
WIP
AugustinMauroy ac7b540
use resolve utility
AugustinMauroy 4bccf85
update
AugustinMauroy 0e4799e
update logic
AugustinMauroy 587e97d
Update url-format.ts
AugustinMauroy 02fb501
Update url-parse.ts
AugustinMauroy 245ca03
chore: remove `@next` and clean (#176)
AugustinMauroy 6fdb711
Merge branch 'feat(`node-url-to-whatwg-url`)' of https://github.com/n…
AugustinMauroy 81f421f
Update package-lock.json
AugustinMauroy 841d576
Update url-format.ts
AugustinMauroy 44ca92d
Merge branch 'main' into feat(`node-url-to-whatwg-url`)
AugustinMauroy c8ff01a
improve
AugustinMauroy ca7dadf
update from feedback
AugustinMauroy 6601add
Update url-format.ts
AugustinMauroy 328fadd
simplify
AugustinMauroy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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,58 @@ | ||
# Node.js URL to WHATWG URL | ||
|
||
This recipe converts Node.js `url` module usage to the WHATWG URL API. It modifies code that uses the legacy `url` module to use the modern `URL` class instead. | ||
|
||
See [DEP0116](https://nodejs.org/api/deprecations.html#DEP0116). | ||
|
||
## Example | ||
|
||
|
||
**`url.parse` to `new URL()`** | ||
```js | ||
// Before | ||
const url = require('node:url'); | ||
|
||
const myUrl = new url.URL('https://example.com'); | ||
const urlAuth = legacyURL.auth; | ||
// After | ||
const myUrl = new URL('https://example.com'); | ||
const urlAuth = `${myUrl.username}:${myUrl.password}`; | ||
``` | ||
|
||
**`url.format` to `myUrl.toString()`** | ||
```js | ||
// Before | ||
const url = require('node:url'); | ||
|
||
url.format({ | ||
protocol: 'https', | ||
hostname: 'example.com', | ||
pathname: '/some/path', | ||
query: { | ||
page: 1, | ||
format: 'json', | ||
}, | ||
}); | ||
// After | ||
const myUrl = new URL('https://example.com/some/path?page=1&format=json').toString(); | ||
``` | ||
AugustinMauroy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## Caveats | ||
|
||
The [`url.resolve`](https://nodejs.org/api/url.html#urlresolvefrom-to) method is not directly translatable to the WHATWG URL API. You may need to implement custom logic to handle URL resolution in your application. | ||
|
||
```js | ||
function resolve(from, to) { | ||
const resolvedUrl = new URL(to, new URL(from, 'resolve://')); | ||
if (resolvedUrl.protocol === 'resolve:') { | ||
// `from` is a relative URL. | ||
const { pathname, search, hash } = resolvedUrl; | ||
return pathname + search + hash; | ||
} | ||
return resolvedUrl.toString(); | ||
} | ||
|
||
resolve('/one/two/three', 'four'); // '/one/two/four' | ||
resolve('http://example.com/', '/one'); // 'http://example.com/one' | ||
resolve('http://example.com/one', '/two'); // 'http://example.com/two' | ||
``` |
This file contains hidden or 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 @@ | ||
schema_version: "1.0" | ||
name: "@nodejs/node-url-to-whatwg-url" | ||
version: 1.0.0 | ||
description: Handle DEPDEP0116 via transforming `url.parse` to `new URL()` | ||
AugustinMauroy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
author: Augustin Mauroy | ||
license: MIT | ||
workflow: workflow.yaml | ||
category: migration | ||
|
||
targets: | ||
languages: | ||
- javascript | ||
- typescript | ||
|
||
keywords: | ||
- transformation | ||
- migration | ||
|
||
registry: | ||
access: public | ||
visibility: public |
This file contains hidden or 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,24 @@ | ||
{ | ||
"name": "@nodejs/node-url-to-whatwg-url", | ||
"version": "1.0.0", | ||
"description": "Handle DEPDEP0116 via transforming `url.parse` to `new URL()`", | ||
"type": "module", | ||
"scripts": { | ||
"test": "node --run test:import-process && node --run test:url-format && node --run test:url-parse", | ||
"test:import-process": "npx codemod@next jssg test -l typescript ./src/import-process.ts ./tests/ --filter import-process", | ||
"test:url-format": "npx codemod@next jssg test -l typescript ./src/url-format.ts ./tests/ --filter url-format", | ||
"test:url-parse": "npx codemod@next jssg test -l typescript ./src/url-parse.ts ./tests/ --filter url-parse" | ||
AugustinMauroy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/nodejs/userland-migrations.git", | ||
"directory": "recipes/node-url-to-whatwg-url", | ||
"bugs": "https://github.com/nodejs/userland-migrations/issues" | ||
}, | ||
"author": "Augustin Mauroy", | ||
"license": "MIT", | ||
"homepage": "https://github.com/nodejs/userland-migrations/tree/main/node-url-to-whatwg-url#readme", | ||
"devDependencies": { | ||
"@codemod.com/jssg-types": "^1.0.3" | ||
} | ||
} |
This file contains hidden or 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,114 @@ | ||
import type { SgRoot, Edit, Range } from "@codemod.com/jssg-types/main"; | ||
import type JS from "@codemod.com/jssg-types/langs/javascript"; | ||
import { getNodeImportStatements } from "@nodejs/codemod-utils/ast-grep/import-statement"; | ||
import { getNodeRequireCalls } from "@nodejs/codemod-utils/ast-grep/require-call"; | ||
import { removeLines } from "@nodejs/codemod-utils/ast-grep/remove-lines"; | ||
|
||
/** | ||
* Clean up unused imports/requires from 'node:url' after transforms using shared utils | ||
*/ | ||
export default function transform(root: SgRoot<JS>): string | null { | ||
const rootNode = root.root(); | ||
const edits: Edit[] = []; | ||
|
||
const isBindingUsed = (name: string): boolean => { | ||
const refs = rootNode.findAll({ rule: { pattern: name } }); | ||
// Heuristic: declaration counts as one; any other usage yields > 1 | ||
return refs.length > 1; | ||
}; | ||
|
||
const linesToRemove: Range[] = []; | ||
AugustinMauroy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// 1) ES Module imports: import ... from 'node:url' | ||
// @ts-ignore - ast-grep types vs jssg types | ||
const esmImports = getNodeImportStatements(root, "url"); | ||
|
||
for (const imp of esmImports) { | ||
const clause = imp.find({ rule: { kind: "import_clause" } }); | ||
let removed = false; | ||
if (clause) { | ||
const nsId = clause.find({ rule: { kind: "namespace_import" } })?.find({ rule: { kind: "identifier" } }); | ||
if (nsId && !isBindingUsed(nsId.text())) { | ||
linesToRemove.push(imp.range()); | ||
removed = true; | ||
} | ||
if (removed) continue; | ||
|
||
const specs = clause.findAll({ rule: { kind: "import_specifier" } }); | ||
|
||
if (specs.length === 0 && !nsId) { | ||
const defaultId = clause.find({ rule: { kind: "identifier" } }); | ||
if (defaultId && !isBindingUsed(defaultId.text())) { | ||
linesToRemove.push(imp.range()); | ||
removed = true; | ||
} | ||
if (removed) continue; | ||
} | ||
|
||
if (specs.length > 0) { | ||
const keepTexts: string[] = []; | ||
for (const spec of specs) { | ||
const text = spec.text().trim(); | ||
const bindingName = text.includes(" as ") ? text.split(/\s+as\s+/)[1] : text; | ||
if (bindingName && isBindingUsed(bindingName)) keepTexts.push(text); | ||
} | ||
if (keepTexts.length === 0) { | ||
linesToRemove.push(imp.range()); | ||
} else if (keepTexts.length !== specs.length) { | ||
const namedImportsNode = clause.find({ rule: { kind: "named_imports" } }); | ||
if (namedImportsNode) edits.push(namedImportsNode.replace(`{ ${keepTexts.join(", ")} }`)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
// 2) CommonJS requires: const ... = require('node:url') | ||
// @ts-ignore - ast-grep types vs jssg types | ||
const requireDecls = getNodeRequireCalls(root, "url"); | ||
|
||
for (const decl of requireDecls) { | ||
const id = decl.find({ rule: { kind: "identifier" } }); | ||
const hasObjectPattern = decl.find({ rule: { kind: "object_pattern" } }); | ||
|
||
if (id && !hasObjectPattern) { | ||
if (!isBindingUsed(id.text())) linesToRemove.push(decl.parent().range()); | ||
continue; | ||
} | ||
|
||
if (hasObjectPattern) { | ||
const names: string[] = []; | ||
const shorts = decl.findAll({ rule: { kind: "shorthand_property_identifier_pattern" } }); | ||
for (const s of shorts) names.push(s.text()); | ||
const pairs = decl.findAll({ rule: { kind: "pair_pattern" } }); | ||
for (const pair of pairs) { | ||
const aliasId = pair.find({ rule: { kind: "identifier" } }); | ||
if (aliasId) names.push(aliasId.text()); | ||
} | ||
|
||
const usedTexts: string[] = []; | ||
for (const s of shorts) if (isBindingUsed(s.text())) usedTexts.push(s.text()); | ||
for (const pair of pairs) { | ||
const aliasId = pair.find({ rule: { kind: "identifier" } }); | ||
if (aliasId && isBindingUsed(aliasId.text())) usedTexts.push(pair.text()); | ||
} | ||
|
||
if (usedTexts.length === 0) { | ||
linesToRemove.push(decl.parent().range()); | ||
} else if (usedTexts.length !== names.length) { | ||
const objPat = decl.find({ rule: { kind: "object_pattern" } }); | ||
if (objPat) edits.push(objPat.replace(`{ ${usedTexts.join(", ")} }`)); | ||
} | ||
} | ||
} | ||
|
||
if (edits.length === 0 && linesToRemove.length === 0) return null; | ||
|
||
let source = rootNode.commitEdits(edits); | ||
|
||
source = removeLines(source, linesToRemove.map(range => ({ | ||
start: { line: range.start.line, column: 0, index: 0 }, | ||
end: { line: range.end.line + 1, column: 0, index: 0 } | ||
AugustinMauroy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}))); | ||
|
||
return source; | ||
}; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.