oxc/crates/oxc_traverse/scripts/lib/utils.mjs
DonIsaac d5cf1f823b chore(traverse): add JSDoc type hints to JS codegen scripts (#6375)
Use JSDoc comments in `build.mjs` and related scripts to provide type hints and better intellisense. I was having a hard time knowing what fields are available in different methods, and I found this quite helpful. I'm sure other newcomers to this part of our codegen infrastructure will find it helpful as well.
2024-10-08 21:00:34 +00:00

43 lines
1,007 B
JavaScript

/**
* @param {string} name
*/
export function typeAndWrappers(name) {
const wrappers = [];
while (true) {
const match = name.match(/^(.+?)<(.+)>$/);
if (!match) break;
wrappers.push(match[1]);
name = match[2];
}
return { name, wrappers };
}
/**
* @param {string} name
*/
export function camelToSnake(name) {
let prefixLen = 1;
for (const prefix of ['TS', 'JSX', 'JS']) {
if (name.startsWith(prefix)) {
prefixLen = prefix.length;
break;
}
}
return name.slice(0, prefixLen).toLowerCase() +
name.slice(prefixLen).replace(/[A-Z]/g, c => `_${c.toLowerCase()}`);
}
/**
* @param {string} name
*/
export function snakeToCamel(name) {
let prefixLen = 0;
for (const prefix of ['TS', 'JSX', 'JS']) {
if (name.startsWith(`${prefix.toLowerCase()}_`)) {
prefixLen = prefix.length + 1;
break;
}
}
return name.slice(0, prefixLen + 1).toUpperCase() +
name.slice(prefixLen + 1).replace(/_([a-z])/g, (_, c) => c.toUpperCase());
}