feat: migrate date codemod to ast-grep#192
Open
43081j wants to merge 2 commits into
Open
Conversation
gameroman
suggested changes
May 21, 2026
Co-authored-by: Roman <dev@rman.dev>
43081j
commented
May 21, 2026
gameroman
reviewed
May 21, 2026
Comment on lines
+23
to
+49
| const ast = ts.parse(file.source); | ||
| const root = ast.root(); | ||
|
|
||
| const { edits } = removeImport(root, `${MODULE_NAME}/auto`); | ||
|
|
||
| const { imports, identifierName } = findDefaultImportIdentifier( | ||
| root, | ||
| MODULE_NAME, | ||
| ); | ||
|
|
||
| if (identifierName) { | ||
| const identifiers = root.findAll({ | ||
| rule: { | ||
| kind: 'identifier', | ||
| regex: `^${identifierName}$`, | ||
| }, | ||
| }); | ||
| for (const id of identifiers) { | ||
| edits.push(id.replace('Date')); | ||
| } | ||
|
|
||
| for (const imp of imports) { | ||
| edits.push(imp.replace('')); | ||
| } | ||
| } | ||
|
|
||
| return dirtyFlag ? root.toSource(options) : file.source; | ||
| return edits.length > 0 ? root.commitEdits(edits) : file.source; |
Contributor
There was a problem hiding this comment.
Can we use the replacePolyfillUsage util here?
Like this for example:
Suggested change
| const ast = ts.parse(file.source); | |
| const root = ast.root(); | |
| const { edits } = removeImport(root, `${MODULE_NAME}/auto`); | |
| const { imports, identifierName } = findDefaultImportIdentifier( | |
| root, | |
| MODULE_NAME, | |
| ); | |
| if (identifierName) { | |
| const identifiers = root.findAll({ | |
| rule: { | |
| kind: 'identifier', | |
| regex: `^${identifierName}$`, | |
| }, | |
| }); | |
| for (const id of identifiers) { | |
| edits.push(id.replace('Date')); | |
| } | |
| for (const imp of imports) { | |
| edits.push(imp.replace('')); | |
| } | |
| } | |
| return dirtyFlag ? root.toSource(options) : file.source; | |
| return edits.length > 0 ? root.commitEdits(edits) : file.source; | |
| const ast = ts.parse(file.source); | |
| const root = ast.root(); | |
| const { edits } = replacePolyfillUsage( | |
| root, | |
| `${MODULE_NAME}/auto`, | |
| 'Date', | |
| ); | |
| return edits.length > 0 ? root.commitEdits(edits) : file.source; |
Looks similar to
module-replacements-codemods/codemods/math.cbrt/index.js
Lines 20 to 27 in ba7db71
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
🔗 Linked issue
📚 Description