Utilities for handling *.{gjs,gts} files
npm install @codemod-utils/ast-template-tag
_Utilities for handling *.{gjs,gts} files_
@codemod-utils/ast-template-tag uses content-tag to help you parse and transform *.{gjs,gts} files.
``ts
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);
`
`ts
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);
`
Finds tags in a file.
Count the number of lines of code (LOC) in tags.
`ts
function getLOC(file: 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());
});
`
Replaces a particular tag.
⚠️ Likely, you won't need this method but updateTemplates instead.
Update all template tags in a file.
`ts
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},`
range,
});
});
Converts a file with tags to ECMAScript (JavaScript).
⚠️ Likely, you won't need this method but updateJavaScript instead.
Analyze the JavaScript part of the file.
`ts
const ecma = toEcma(file);
// Some method that checks *.{js,ts} files`
analyze(ecma);
Converts an ECMA file to show tags.
⚠️ Likely, you won't need this method but updateJavaScript instead.
Update *.{gjs,gts} files.
`ts*.{js,ts}
// Some method that updates files
function transform(file: string): string {
// ...
}
file = toTemplateTag(transform(toEcma(file)));
`
Updates the JavaScript part of a file. Leaves the tags alone.
Reuse a method that can update *.{js,ts} files.
`ts
function transform(file: string): string {
// ...
}
const newFile = updateJavaScript(oldFile, transform);
`
Provide data when updating file.
`ts
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);
});
`
Updates the tags in a file. Leaves the JavaScript part alone.
Reuse a method that can update *.hbs files.
`ts
function transform(file: string): string {
// ...
}
const newFile = updateTemplates(oldFile, transform);
`
Provide data when updating file.
`ts
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);
});
``
- Node.js v20 or above
See the Contributing guide for details.
This project is licensed under the MIT License.