Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
@codemod-utils/ast
Advanced tools
Utilities for handling abstract syntax tree
@codemod-utils/ast
wraps the methods from ember-template-recast
and recast
, libraries that help you parse and transform *.hbs
and *.{js,ts}
files.
The wrappers help you read and write files of different types in the same way. This way, you can focus on learning the builders and visit methods, the building blocks for transforming code (library-dependent).
*.hbs
filesimport { ASTHandlebars as AST } from '@codemod-utils/ast';
function transformCode(file) {
const traverse = AST.traverse();
const ast = traverse(file, {
/* Use AST.builders to transform the tree */
});
return AST.print(ast);
}
*.{js,ts}
filesimport { ASTJavaScript as AST } from '@codemod-utils/ast';
function transformCode(file, isTypeScript) {
const traverse = AST.traverse(isTypeScript);
const ast = traverse(file, {
/* Use AST.builders to transform the tree */
});
return AST.print(ast);
}
Currently, ember-template-recast
and recast
lack documentation and tutorials. This is unfortunate, given the large amount of builders and visit methods that they provide to help you transform code.
I recommend using AST Explorer to test a small piece of code and familiarize with the API. The error messages from TypeScript, which you can find in your browser's console, can sometimes help. AST Workshop provides a good starting point for Handlebars.
If you intend to publish your codemod, I recommend using @codemod-utils/tests
(create and test file fixtures) to check the output and prevent regressions.
Select the following options to create a 4-tab window:
Handlebars
ember-template-recast
ember-template-recast
Copy-paste the visit methods from your file to AST explorer, then rename AST.builders
to b
.
/* Your file */
import { ASTHandlebars as AST } from '@codemod-utils/ast';
function transformCode(file) {
const traverse = AST.traverse();
const ast = traverse(file, {
AttrNode(node) {
if (node.name !== 'local-class') {
return;
}
node.name = 'class';
const attributeValue = node.value.chars.trim();
node.value = AST.builders.mustache(
AST.builders.path(`this.styles.${attributeValue}`),
);
},
});
return AST.print(ast);
}
/* AST Explorer */
module.exports = function(env) {
const b = env.syntax.builders;
return {
AttrNode(node) {
if (node.name !== 'local-class') {
return;
}
node.name = 'class';
const attributeValue = node.value.chars.trim();
node.value = b.mustache(
b.path(`this.styles.${attributeValue}`),
);
},
};
};
Select the following options to create a 4-tab window:
JavaScript
recast
recast
Copy-paste the visit methods from your file to AST explorer, then rename AST.builders
to b
.
/* Your file */
import { ASTJavaScript as AST } from '@codemod-utils/ast';
export function transformCode(file) {
const traverse = AST.traverse(true);
const ast = traverse(file, {
visitClassDeclaration(path) {
const { body } = path.node.body;
const nodesToAdd = [
AST.builders.classProperty(
AST.builders.identifier('styles'),
AST.builders.identifier('styles'),
),
];
body.unshift(...nodesToAdd);
return false;
},
});
return AST.print(ast);
}
/* AST Explorer */
export default function transformer(code, { recast, parsers }) {
const ast = recast.parse(code, { parser: parsers.typescript });
const b = recast.types.builders;
recast.visit(ast, {
visitClassDeclaration(path) {
const { body } = path.node.body;
const nodesToAdd = [
b.classProperty(
b.identifier('styles'),
b.identifier('styles')
)
];
body.unshift(...nodesToAdd);
return false;
}
});
return recast.print(ast).code;
}
See the Contributing guide for details.
This project is licensed under the MIT License.
FAQs
Utilities for handling abstract syntax tree
The npm package @codemod-utils/ast receives a total of 0 weekly downloads. As such, @codemod-utils/ast popularity was classified as not popular.
We found that @codemod-utils/ast demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.