Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: generate string constants from config #5451

Merged
merged 1 commit into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions rust/parse_ast/src/convert_ast/converter/string_constants.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// These need to correspond to the positions in convert-ast-strings.ts
// This file is generated by scripts/generate-string-constant.js.
// Do not edit this file directly.
pub const STRING_VAR: [u8; 4] = 0u32.to_ne_bytes(); // var
pub const STRING_LET: [u8; 4] = 1u32.to_ne_bytes(); // let
pub const STRING_CONST: [u8; 4] = 2u32.to_ne_bytes(); // const
Expand Down Expand Up @@ -49,7 +50,7 @@ pub const STRING_DIVASSIGN: [u8; 4] = 46u32.to_ne_bytes(); // /=
pub const STRING_MODASSIGN: [u8; 4] = 47u32.to_ne_bytes(); // %=
pub const STRING_LSHIFTASSIGN: [u8; 4] = 48u32.to_ne_bytes(); // <<=
pub const STRING_RSHIFTASSIGN: [u8; 4] = 49u32.to_ne_bytes(); // >>=
pub const STRING_ZEROFILLRSHIFTASSIGN: [u8; 4] = 50u32.to_ne_bytes(); // ">>>=
pub const STRING_ZEROFILLRSHIFTASSIGN: [u8; 4] = 50u32.to_ne_bytes(); // >>>=
pub const STRING_BITORASSIGN: [u8; 4] = 51u32.to_ne_bytes(); // |=
pub const STRING_BITXORASSIGN: [u8; 4] = 52u32.to_ne_bytes(); // ^=
pub const STRING_BITANDASSIGN: [u8; 4] = 53u32.to_ne_bytes(); // &=
Expand Down
1 change: 1 addition & 0 deletions scripts/generate-ast-converters.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ import './generate-rust-constants.js';
import './generate-buffer-to-ast.js';
import './generate-child-node-keys.js';
import './generate-buffer-parsers.js';
import './generate-string-constants.js';
6 changes: 3 additions & 3 deletions scripts/generate-buffer-parsers.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { writeFile } from 'node:fs/promises';
import { AST_NODES, astNodeNamesWithFieldOrder } from './ast-types.js';
import { getNode } from './generate-buffer-to-ast.js';
import { firstLetterLowercase, lintFile } from './helpers.js';
import { firstLetterLowercase, lintTsFile } from './helpers.js';

const bufferParsersFile = new URL('../src/ast/bufferParsers.ts', import.meta.url);

Expand Down Expand Up @@ -187,7 +187,7 @@ import type * as estree from 'estree';
import type { AstContext } from '../Module';
import { convertAnnotations, convertString } from '../utils/astConverterHelpers';
import { convertNode as convertJsonNode } from '../utils/bufferToAst';
import { FIXED_STRINGS } from '../utils/convert-ast-strings';
import FIXED_STRINGS from '../utils/convert-ast-strings';
import type { ReadString } from '../utils/getReadStringFunction';
import getReadStringFunction from '../utils/getReadStringFunction';
${nodeTypeImports.join('\n')}
Expand Down Expand Up @@ -253,4 +253,4 @@ function convertNodeList(parent: Node | { context: AstContext; type: string }, p
`;

await writeFile(bufferParsersFile, bufferParsers);
await lintFile(bufferParsersFile);
await lintTsFile(bufferParsersFile);
6 changes: 3 additions & 3 deletions scripts/generate-buffer-to-ast.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { writeFile } from 'node:fs/promises';
import { AST_NODES, astNodeNamesWithFieldOrder } from './ast-types.js';
import { firstLetterLowercase, lintFile } from './helpers.js';
import { firstLetterLowercase, lintTsFile } from './helpers.js';

const bufferToJsAstFile = new URL('../src/utils/bufferToAst.ts', import.meta.url);

Expand Down Expand Up @@ -190,7 +190,7 @@ import {
convertString,
INVALID_ANNOTATION_KEY
} from './astConverterHelpers';
import { FIXED_STRINGS } from './convert-ast-strings';
import FIXED_STRINGS from './convert-ast-strings';
import type { ReadString } from './getReadStringFunction';
import { error, getRollupError, logParseError } from './logs';

Expand Down Expand Up @@ -239,4 +239,4 @@ function convertNodeList(position: number, buffer: Uint32Array, readString: Read
`;

await writeFile(bufferToJsAstFile, bufferToJsAst);
await lintFile(bufferToJsAstFile);
await lintTsFile(bufferToJsAstFile);
4 changes: 2 additions & 2 deletions scripts/generate-child-node-keys.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { writeFile } from 'node:fs/promises';
import { AST_NODES } from './ast-types.js';
import { lintFile } from './helpers.js';
import { lintTsFile } from './helpers.js';

const childNodeKeysFile = new URL('../src/ast/childNodeKeys.ts', import.meta.url);

Expand Down Expand Up @@ -30,4 +30,4 @@ export const childNodeKeys: Record<string, string[]> = {
`;

await writeFile(childNodeKeysFile, childNodeKeys);
await lintFile(childNodeKeysFile);
await lintTsFile(childNodeKeysFile);
5 changes: 2 additions & 3 deletions scripts/generate-rust-constants.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { writeFile } from 'node:fs/promises';
import { fileURLToPath } from 'node:url';
import { AST_NODES, astNodeNamesWithFieldOrder } from './ast-types.js';
import { runWithEcho, toScreamingSnakeCase } from './helpers.js';
import { lintRustFile, toScreamingSnakeCase } from './helpers.js';

const BYTES_PER_U32 = 4;

Expand Down Expand Up @@ -79,4 +78,4 @@ ${reservedBytesAndOffsets}
`;

await writeFile(astConstantsFile, astConstants);
await runWithEcho('rustfmt', [fileURLToPath(astConstantsFile)]);
await lintRustFile(astConstantsFile);
101 changes: 101 additions & 0 deletions scripts/generate-string-constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { writeFile } from 'node:fs/promises';
import { lintRustFile, lintTsFile } from './helpers.js';

const targetRustFile = new URL(
'../rust/parse_ast/src/convert_ast/converter/string_constants.rs',
import.meta.url
);
const targetTsFile = new URL('../src/utils/convert-ast-strings.ts', import.meta.url);

const stringConstantsTemplate = [
['STRING_VAR', 'var'],
['STRING_LET', 'let'],
['STRING_CONST', 'const'],
['STRING_INIT', 'init'],
['STRING_GET', 'get'],
['STRING_SET', 'set'],
['STRING_CONSTRUCTOR', 'constructor'],
['STRING_METHOD', 'method'],
['STRING_MINUS', '-'],
['STRING_PLUS', '+'],
['STRING_BANG', '!'],
['STRING_TILDE', '~'],
['STRING_TYPEOF', 'typeof'],
['STRING_VOID', 'void'],
['STRING_DELETE', 'delete'],
['STRING_PLUSPLUS', '++'],
['STRING_MINUSMINUS', '--'],
['STRING_EQEQ', '=='],
['STRING_NOTEQ', '!='],
['STRING_EQEQEQ', '==='],
['STRING_NOTEQEQ', '!=='],
['STRING_LT', '<'],
['STRING_LTEQ', '<='],
['STRING_GT', '>'],
['STRING_GTEQ', '>='],
['STRING_LSHIFT', '<<'],
['STRING_RSHIFT', '>>'],
['STRING_ZEROFILLRSHIFT', '>>>'],
['STRING_ADD', '+'],
['STRING_SUB', '-'],
['STRING_MUL', '*'],
['STRING_DIV', '/'],
['STRING_MOD', '%'],
['STRING_BITOR', '|'],
['STRING_BITXOR', '^'],
['STRING_BITAND', '&'],
['STRING_LOGICALOR', '||'],
['STRING_LOGICALAND', '&&'],
['STRING_IN', 'in'],
['STRING_INSTANCEOF', 'instanceof'],
['STRING_EXP', '**'],
['STRING_NULLISHCOALESCING', '??'],
['STRING_ASSIGN', '='],
['STRING_ADDASSIGN', '+='],
['STRING_SUBASSIGN', '-='],
['STRING_MULASSIGN', '*='],
['STRING_DIVASSIGN', '/='],
['STRING_MODASSIGN', '%='],
['STRING_LSHIFTASSIGN', '<<='],
['STRING_RSHIFTASSIGN', '>>='],
['STRING_ZEROFILLRSHIFTASSIGN', '>>>='],
['STRING_BITORASSIGN', '|='],
['STRING_BITXORASSIGN', '^='],
['STRING_BITANDASSIGN', '&='],
['STRING_EXPASSIGN', '**='],
['STRING_ANDASSIGN', '&&='],
['STRING_ORASSIGN', '||='],
['STRING_NULLISHASSIGN', '??='],
['STRING_PURE', 'pure'],
['STRING_NOSIDEEFFECTS', 'noSideEffects'],
['STRING_SOURCEMAP', 'sourcemap'],
['STRING_USING', 'using'],
['STRING_AWAIT_USING', 'await using']
];

const tipComment = `// This file is generated by scripts/generate-string-constant.js.
// Do not edit this file directly.\n`;

const rustCode =
tipComment +
stringConstantsTemplate
.map(
([variableName, value], index) =>
`pub const ${variableName}: [u8; 4] = ${index}u32.to_ne_bytes(); // ${value}`
)
.join('\n');

const tsCode =
tipComment +
`export default ` +
JSON.stringify(
stringConstantsTemplate.map(([, value]) => value),
undefined,
2
) +
`;\n`;

await Promise.all([
writeFile(targetTsFile, tsCode).then(() => lintTsFile(targetTsFile)),
writeFile(targetRustFile, rustCode).then(() => lintRustFile(targetRustFile))
]);
10 changes: 9 additions & 1 deletion scripts/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export async function readJson(file) {
* @param {URL} file
* @return {Promise<void>}
*/
export async function lintFile(file) {
export async function lintTsFile(file) {
const eslint = new ESLint({ fix: true });
const results = await eslint.lintFiles([fileURLToPath(file)]);
await ESLint.outputFixes(results);
Expand All @@ -89,6 +89,14 @@ export async function lintFile(file) {
console.log(resultText);
}

/**
* @param {URL} file
* @return {Promise<void>}
*/
export function lintRustFile(file) {
return runWithEcho('rustfmt', [fileURLToPath(file)]);
}

/**
* @param {string} string
* @returns {string}
Expand Down
2 changes: 1 addition & 1 deletion src/ast/bufferParsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type * as estree from 'estree';
import type { AstContext } from '../Module';
import { convertAnnotations, convertString } from '../utils/astConverterHelpers';
import { convertNode as convertJsonNode } from '../utils/bufferToAst';
import { FIXED_STRINGS } from '../utils/convert-ast-strings';
import FIXED_STRINGS from '../utils/convert-ast-strings';
import type { ReadString } from '../utils/getReadStringFunction';
import getReadStringFunction from '../utils/getReadStringFunction';
import ArrayExpression from './nodes/ArrayExpression';
Expand Down
2 changes: 1 addition & 1 deletion src/utils/astConverterHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FIXED_STRINGS } from './convert-ast-strings';
import FIXED_STRINGS from './convert-ast-strings';
import type { ReadString } from './getReadStringFunction';

export const ANNOTATION_KEY = '_rollupAnnotations';
Expand Down
2 changes: 1 addition & 1 deletion src/utils/bufferToAst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
convertString,
INVALID_ANNOTATION_KEY
} from './astConverterHelpers';
import { FIXED_STRINGS } from './convert-ast-strings';
import FIXED_STRINGS from './convert-ast-strings';
import type { ReadString } from './getReadStringFunction';
import { error, getRollupError, logParseError } from './logs';

Expand Down
4 changes: 3 additions & 1 deletion src/utils/convert-ast-strings.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export const FIXED_STRINGS = [
// This file is generated by scripts/generate-string-constant.js.
// Do not edit this file directly.
export default [
'var',
'let',
'const',
Expand Down