@codemod-utils/ast-template-tag
v1.1.0
Published
Utilities for handling *.{gjs,gts} files
Downloads
1,047
Readme
@codemod-utils/ast-template-tag
Utilities for handling *.{gjs,gts} files
What is it?
@codemod-utils/ast-template-tag uses content-tag to help you parse and transform *.{gjs,gts} files.
import { updateJavaScript } from '@codemod-utils/ast-template-tag';
// Reuse a method that can update `*.{js,ts}` files
function transform(file: string): string {
// ...
}
const newFile = updateJavaScript(oldFile, transform);import { updateTemplates } from '@codemod-utils/ast-template-tag';
// Reuse a method that can update `*.hbs` files
function transform(file: string): string {
// ...
}
const newFile = updateTemplates(oldFile, transform);API
findTemplateTags
Finds <template> tags in a file.
Count the number of lines of code (LOC) in <template> tags.
function getLOC(code: string): number {
const matches = file.match(/\r?\n/g);
return (matches ?? []).length;
}
const templateTags = findTemplateTags(file);
let loc = 0;
templateTags.forEach(({ contents }) => {
loc += getLOC(contents.trim());
});replaceTemplateTag
Replaces a particular <template> tag.
⚠️ Likely, you won't need this method but updateTemplates instead.
Update all template tags in a file.
const templateTags = findTemplateTags(file);
templateTags.reverse().forEach(({ contents, range }) => {
// Some method that can update `*.hbs` files
const template = transform(contents);
file = replaceTemplateTag(file, {
code: `<template>${template}</template>`,
range,
});
});toEcma
Converts a file with <template> tags to ECMAScript (JavaScript).
⚠️ Likely, you won't need this method but updateJavaScript instead.
Analyze the JavaScript part of the file.
const ecma = toEcma(file);
// Some method that checks `*.{js,ts}` files
analyze(ecma);toTemplateTag
Converts an ECMA file to show <template> tags.
⚠️ Likely, you won't need this method but updateJavaScript instead.
Update *.{gjs,gts} files.
// Some method that updates `*.{js,ts}` files
function transform(file: string): string {
// ...
}
file = toTemplateTag(transform(toEcma(file)));updateJavaScript
Updates the JavaScript part of a file. Leaves the <template> tags alone.
Reuse a method that can update *.{js,ts} files.
function transform(file: string): string {
// ...
}
const newFile = updateJavaScript(oldFile, transform);Provide data when updating file.
type Data = {
isTypeScript: boolean;
};
function transform(file: string, data: Data): string {
// ...
}
const data = {
isTypeScript: filePath.endsWith('.gts'),
};
const newFile = updateJavaScript(oldFile, (code) => {
return transform(code, data);
});updateTemplates
Updates the <template> tags in a file. Leaves the JavaScript part alone.
Reuse a method that can update *.hbs files.
function transform(file: string): string {
// ...
}
const newFile = updateTemplates(oldFile, transform);Provide data when updating file.
type Data = {
isTypeScript: boolean;
};
function transform(file: string, data: Data): string {
// ...
}
const data = {
isTypeScript: filePath.endsWith('.gts'),
};
const newFile = updateTemplates(oldFile, (code) => {
return transform(code, data);
});Compatibility
- Node.js v20 or above
Contributing
See the Contributing guide for details.
License
This project is licensed under the MIT License.
