Skip to content

feat: migrate date codemod to ast-grep#192

Open
43081j wants to merge 2 commits into
mainfrom
jg/date-sg
Open

feat: migrate date codemod to ast-grep#192
43081j wants to merge 2 commits into
mainfrom
jg/date-sg

Conversation

@43081j
Copy link
Copy Markdown
Contributor

@43081j 43081j commented May 21, 2026

🔗 Linked issue

📚 Description

Comment thread codemods/date/index.js Outdated
Co-authored-by: Roman <dev@rman.dev>
Comment thread codemods/shared-ast-grep.js
Comment thread codemods/date/index.js
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;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

const ast = ts.parse(file.source);
const root = ast.root();
const { edits } = replacePolyfillUsage(
root,
'math.cbrt/polyfill',
'Math.cbrt',
);
return edits.length > 0 ? root.commitEdits(edits) : file.source;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants