fix: reformated ast-create-node for better readability
MODIFY
CONTRIBUTING.md
CONTRIBUTING.md
@@ -134,7 +134,7 @@ The created files will be listed in the console output. See those files for inli
This is used to generate new ast nodes and boilerplate.
```bash
-./rome run scripts/ast-create-node [language] [nodeType] [category]
+./rome run scripts/ast-create-node [language]/[category]/[nodeType]
```
The `language` is one of the language folders defined in [`https://github.com/romefrontend/rome/tree/main/internal/ast/`]
@@ -142,7 +142,7 @@ The `language` is one of the language folders defined in [`https://github.com/ro
The `category` is one of the category folders inside the `language` folders.
```bash
-./rome run scripts/ast-create-node js JSArrayType typescript
+./rome run scripts/ast-create-node js/typescript/JSArrayType
```
The created files will be listed in the console output.

MODIFY
scripts/ast-create-node.ts
scripts/ast-create-node.ts
@@ -10,25 +10,27 @@ import {
import {exists} from "@internal/fs";
import {dedent, toCamelCase} from "@internal/string-utils";
import {markup} from "@internal/markup";
+import {createUnknownPath} from "@internal/path";
import {main as generateAST} from "./generated-files/ast";
-export async function main(
- [language, nodeType, category]: Array<string>,
-): Promise<number> {
- if (language === undefined || nodeType === undefined || category === undefined) {
+export async function main([filename]: Array<string>): Promise<number> {
+ const whitelistedPrefix = ["TS"];
+
+ if (filename === undefined) {
reporter.error(
- markup`./rome run scripts/ast-create-node [language] [node-type] [category]`,
+ markup`./rome run ast-create-node [language]/[category]/[nodeType]`,
);
return 1;
}
- if (!nodeType.toLowerCase().startsWith(language)) {
- reporter.error(
- markup`Node type argument "${nodeType}" must have the language prefix "${language}"`,
- );
+ const segments = createUnknownPath(filename).getSegments();
+ if (segments.length !== 3) {
+ reporter.error(markup`Expected three segments in filename argument`);
return 1;
}
+ const [language, category, nodeType] = segments;
+
if (!(await languageExists(language))) {
const languages = await getLanguages();
@@ -55,15 +57,23 @@ export async function main(
return 1;
}
- const joined = `${language}/${category}/${nodeType}`;
+ if (
+ !whitelistedPrefix.includes(nodeType.slice(0, 2)) &&
+ !nodeType.toLowerCase().startsWith(language)
+ ) {
+ reporter.error(
+ markup`Node type argument "${nodeType}" must have the language prefix "${language}"`,
+ );
+ return 1;
+ }
// This will convert the pascal case to camel
const builderName = toCamelCase(nodeType);
// Write AST def
- const astDefPath = INTERNAL.append("ast", `${joined}.ts`);
+ const astDefPath = INTERNAL.append("ast", `${filename}.ts`);
if (await exists(astDefPath)) {
- reporter.error(markup`AST node ${joined} already exists`);
+ reporter.error(markup`AST node ${filename} already exists`);
return 1;
}
await writeFile(
@@ -85,7 +95,7 @@ export async function main(
// Write builder
await writeFile(
- INTERNAL.append("formatter", "builders", `${joined}.ts`),
+ INTERNAL.append("formatter", "builders", `${filename}.ts`),
dedent`
import {${nodeType}} from "@internal/ast";
import {Builder, Token} from "@internal/formatter";

fix: reformated ast-create-node for better readability (#1179)