From 12d4a2aa149dcafb5071ad0309f1e5fdde917a75 Mon Sep 17 00:00:00 2001 From: Victorien Elvinger Date: Thu, 9 May 2024 23:44:29 +0200 Subject: [PATCH] refactor: remove unneeded index access --- src/ast/bare-configure.ts | 13 +++++++------ src/ast/bare-semantic-checker.ts | 9 ++++----- src/parser/bare-lexer.ts | 2 +- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/ast/bare-configure.ts b/src/ast/bare-configure.ts index 01b96901..23932a27 100644 --- a/src/ast/bare-configure.ts +++ b/src/ast/bare-configure.ts @@ -10,13 +10,14 @@ export function configure(schema: ast.Ast, config: Config): ast.Ast { aliasesInFlatUnion: new Set(), symbols: ast.symbols(schema), } - const defs = schema.defs.slice() - for (let i = 0; i < defs.length; i++) { - const type = configureType(c, defs[i].type, true) - if (defs[i].type !== type) { - const { alias, internal, comment, offset } = defs[i] - defs[i] = { alias, internal, type, comment, offset } + const defs: ast.AliasedType[] = [] + for (let def of schema.defs) { + const type = configureType(c, def.type, true) + if (def.type !== type) { + const { alias, internal, comment, offset } = def + def = { alias, internal, type, comment, offset } } + defs.push(def) } for (let i = 0; i < defs.length; i++) { let type = defs[i].type diff --git a/src/ast/bare-semantic-checker.ts b/src/ast/bare-semantic-checker.ts index 0d490252..958e3ef6 100644 --- a/src/ast/bare-semantic-checker.ts +++ b/src/ast/bare-semantic-checker.ts @@ -46,7 +46,7 @@ type Checker = { function checkTypeName(aliased: ast.AliasedType): void { const { alias, internal } = aliased - if (alias.length !== 0 && /^\d/.test(alias[0])) { + if (/^\d/.test(alias)) { if (!internal) { throw new CompilerError( `the type name '${alias}' must not start with a figure or must be internal.`, @@ -235,17 +235,16 @@ function checkNonVoid(c: Checker, type: ast.Type): void { } function checkUnionInvariants(c: Checker, type: ast.UnionType): void { - const tags = type.data // check type uniqueness const stringifiedTypes = new Set() - for (let i = 0; i < tags.length; i++) { - const stringifiedType = JSON.stringify(ast.withoutExtra(type.types[i])) + for (const ty of type.types) { + const stringifiedType = JSON.stringify(ast.withoutExtra(ty)) // NOTE: this dirty check is ok because we initialize // every object in the same way (properties are in the same order) if (stringifiedTypes.has(stringifiedType)) { throw new CompilerError( "a type cannot be repeated in an union.", - type.types[i].offset, + ty.offset, ) } stringifiedTypes.add(stringifiedType) diff --git a/src/parser/bare-lexer.ts b/src/parser/bare-lexer.ts index 06d15d64..864fc6ba 100644 --- a/src/parser/bare-lexer.ts +++ b/src/parser/bare-lexer.ts @@ -56,7 +56,7 @@ export function nextToken(lex: Lexer): void { offset += token.length token = "" while (offset < content.length) { - const c = content[offset] + const c = content[offset] as string if (c === "#") { // comment let index = content.indexOf("\n", offset + 1)