-
-
Notifications
You must be signed in to change notification settings - Fork 27
feat(chalk-to-util-styletext): add workflow #256
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
richiemccoll
wants to merge
25
commits into
nodejs:main
Choose a base branch
from
richiemccoll:feat/chalk-styletext-migration
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 1 commit
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
4fac653
feat(chalk-to-util-styletext): add workflow with basic test
richiemccoll 21781d6
feat(chalk-to-util-styletext): sync lockfile
richiemccoll 6944457
feat(chalk-to-util-styletext): add commonjs require case
richiemccoll e628155
Merge branch 'main' into feat/chalk-styletext-migration
richiemccoll eaf5469
feat(chalk-to-util-styletext): add bg color case
richiemccoll 8b29e26
feat(chalk-to-util-styletext): add chained case
richiemccoll 0c2b44a
feat(chalk-to-util-styletext): add advanced modifiers case
richiemccoll 54b3bb9
feat(chalk-to-util-styletext): add more modifiers to test case
richiemccoll 90d0648
feat(chalk-to-util-styletext): add modifiers test case
richiemccoll 1bf1736
feat(chalk-to-util-styletext): add mixed imports test case
richiemccoll 6a14179
feat(chalk-to-util-styletext): add dynamic imports test case
richiemccoll cefb46c
Merge branch 'main' into feat/chalk-styletext-migration
richiemccoll 324fb50
feat(chalk-to-util-styletext): refactoring
richiemccoll fa51707
feat(chalk-to-util-styletext): code review comments
richiemccoll 27a0ad2
Merge branch 'main' into feat/chalk-styletext-migration
richiemccoll 0482005
feat(chalk-to-util-styletext): code review comments
richiemccoll 8c762aa
Merge branch 'feat/chalk-styletext-migration' of github.com:richiemcc…
richiemccoll f2a5a8b
feat(chalk-to-util-styletext): handle different import cases
richiemccoll 09e8eaf
feat(chalk-to-util-styletext): add unsupported features test
richiemccoll 412f1ee
Merge branch 'main' into feat/chalk-styletext-migration
richiemccoll 34c97cd
feat(chalk-to-util-styletext): warn on unsupported method
richiemccoll 6d5433d
Merge branch 'feat/chalk-styletext-migration' of github.com:richiemcc…
richiemccoll 661902f
feat(chalk-to-util-styletext): handle method assignments
richiemccoll 3f41a08
feat(chalk-to-util-styletext): handle complex chaining
richiemccoll 0a88554
Merge branch 'main' into feat/chalk-styletext-migration
richiemccoll 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # chalk-to-util-styletext | ||
|
|
||
| TODO |
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/chalk-to-util-styletext" | ||
| version: 1.0.0 | ||
| description: Migrate from the chalk package to Node.js's built-in util.styleText API | ||
| author: Richie McColl | ||
| 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/chalk-to-util-styletext", | ||
| "version": "1.0.0", | ||
| "description": "Migrate from the chalk package to Node.js's built-in util.styleText API", | ||
| "type": "module", | ||
| "scripts": { | ||
| "test": "npx codemod jssg test -l typescript ./src/workflow.ts ./" | ||
| }, | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "git+https://github.com/nodejs/userland-migrations.git", | ||
| "directory": "recipes/chalk-to-util-styletext", | ||
| "bugs": "https://github.com/nodejs/userland-migrations/issues" | ||
| }, | ||
| "author": "Richie McColl", | ||
| "license": "MIT", | ||
| "homepage": "https://github.com/nodejs/userland-migrations/blob/main/recipes/chalk-to-util-styletext/README.md", | ||
| "devDependencies": { | ||
| "@codemod.com/jssg-types": "^1.0.9" | ||
| }, | ||
| "dependencies": { | ||
| "@nodejs/codemod-utils": "*" | ||
| } | ||
| } |
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,123 @@ | ||
| import { getNodeRequireCalls } from "@nodejs/codemod-utils/ast-grep/require-call"; | ||
| import { getNodeImportStatements } from "@nodejs/codemod-utils/ast-grep/import-statement"; | ||
| import { resolveBindingPath } from "@nodejs/codemod-utils/ast-grep/resolve-binding-path"; | ||
| import { removeLines } from "@nodejs/codemod-utils/ast-grep/remove-lines"; | ||
| import type { Edit, Range, SgNode, SgRoot } from "@codemod.com/jssg-types/main"; | ||
| import type Js from "@codemod.com/jssg-types/langs/javascript"; | ||
|
|
||
| /** | ||
| * Transform function that converts chalk method calls to Node.js util.styleText calls. | ||
| * | ||
| * Examples: | ||
| * - chalk.red("text") -> styleText("red", "text") | ||
| */ | ||
| export default function transform(root: SgRoot<Js>): string | null { | ||
| const rootNode = root.root(); | ||
| const edits: Edit[] = []; | ||
| const linesToRemove: Range[] = []; | ||
| const chalkBinding = "chalk"; | ||
|
|
||
| const statements = [ | ||
| ...getNodeImportStatements(root, chalkBinding), | ||
| ...getNodeRequireCalls(root, chalkBinding), | ||
| ]; | ||
|
|
||
| if (!statements.length) return null; | ||
AugustinMauroy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| for (const statement of statements) { | ||
| const binding = resolveBindingPath(statement, "$"); | ||
|
|
||
| if (!binding) continue; | ||
|
|
||
| const calls = rootNode.findAll({ | ||
| rule: { | ||
| kind: "call_expression", | ||
| has: { | ||
| field: "function", | ||
| kind: "member_expression", | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| const transformedCalls: SgNode<Js>[] = []; | ||
|
|
||
| for (const call of calls) { | ||
| const functionCall = call.field("function"); | ||
|
|
||
| if (!functionCall) { | ||
| continue; | ||
| } | ||
|
|
||
| const styles = extractChalkStyles(functionCall, chalkBinding); | ||
|
|
||
| if (styles.length === 0) { | ||
| continue; | ||
| } | ||
AugustinMauroy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // Get the first argument (the text passed to style) | ||
| const args = call.field("arguments"); | ||
| if (!args) continue; | ||
AugustinMauroy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // Find all argument nodes | ||
| const argsList = args.children().filter((c) => { | ||
| const excluded = [",", "(", ")"]; | ||
| return !excluded.includes(c.kind()); | ||
| }); | ||
|
|
||
| if (argsList.length === 0) continue; | ||
|
|
||
| const textArg = argsList[0].text(); | ||
|
|
||
| // Create the styleText replacement | ||
| let replacement: string; | ||
|
|
||
| if (styles.length === 1) { | ||
| replacement = `styleText("${styles[0]}", ${textArg})`; | ||
| } else { | ||
| const stylesArray = `[${styles.map((s) => `"${s}"`).join(", ")}]`; | ||
| replacement = `styleText(${stylesArray}, ${textArg})`; | ||
| } | ||
|
|
||
| edits.push(call.replace(replacement)); | ||
| transformedCalls.push(call); | ||
| } | ||
|
|
||
| if (edits.length > 0) { | ||
| // Update the import statement if any calls were transformed | ||
| if (statement.kind() === "import_statement") { | ||
| // Replace entire import statement | ||
| edits.push(statement.replace(`import { styleText } from "node:util";`)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (!edits.length) return null; | ||
|
|
||
| const sourceCode = rootNode.commitEdits(edits); | ||
| return removeLines(sourceCode, linesToRemove); | ||
AugustinMauroy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
AugustinMauroy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // Helper function to extract chalk style methods from a member expression | ||
| function extractChalkStyles(node: SgNode<Js>, chalkBinding: string): string[] { | ||
AugustinMauroy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const styles: string[] = []; | ||
|
|
||
| function traverse(n: SgNode<Js>): boolean { | ||
| const obj = n.field("object"); | ||
| const prop = n.field("property"); | ||
|
|
||
| if (obj && prop && prop.kind() === "property_identifier") { | ||
| const propName = prop.text(); | ||
|
|
||
| if (obj.kind() === "identifier" && obj.text() === chalkBinding) { | ||
| // Base case: chalk.method | ||
| styles.push(propName); | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| traverse(node); | ||
| return styles; | ||
AugustinMauroy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
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,5 @@ | ||
| import { styleText } from "node:util"; | ||
|
|
||
| console.log(styleText("red", "Error message")); | ||
| console.log(styleText("green", "Success message")); | ||
| console.log(styleText("blue", "Info message")); |
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,5 @@ | ||
| import chalk from "chalk"; | ||
|
|
||
| console.log(chalk.red("Error message")); | ||
| console.log(chalk.green("Success message")); | ||
| console.log(chalk.blue("Info message")); |
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 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "allowImportingTsExtensions": true, | ||
| "allowJs": true, | ||
| "alwaysStrict": true, | ||
| "baseUrl": "./", | ||
| "declaration": true, | ||
| "declarationMap": true, | ||
| "emitDeclarationOnly": true, | ||
| "lib": ["ESNext", "DOM"], | ||
| "module": "NodeNext", | ||
| "moduleResolution": "NodeNext", | ||
| "noImplicitThis": true, | ||
| "removeComments": true, | ||
| "strict": true, | ||
| "stripInternal": true, | ||
| "target": "esnext" | ||
| }, | ||
| "include": ["./"], | ||
| "exclude": ["tests/**"] | ||
| } |
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,25 @@ | ||
| # yaml-language-server: $schema=https://raw.githubusercontent.com/codemod-com/codemod/refs/heads/main/schemas/workflow.json | ||
|
|
||
| version: "1" | ||
|
|
||
| nodes: | ||
| - id: apply-transforms | ||
| name: Apply AST Transformations | ||
| type: automatic | ||
| steps: | ||
| - name: Migrate from the chalk package to Node.js's built-in util.styleText API | ||
| js-ast-grep: | ||
| js_file: src/workflow.ts | ||
| base_path: . | ||
| include: | ||
| - "**/*.cjs" | ||
| - "**/*.cts" | ||
| - "**/*.js" | ||
| - "**/*.jsx" | ||
| - "**/*.mjs" | ||
| - "**/*.mts" | ||
| - "**/*.ts" | ||
| - "**/*.tsx" | ||
| exclude: | ||
| - "**/node_modules/**" | ||
| language: typescript |
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.