From 5afae11193f724f2baac674f2bdef2458942d7cb Mon Sep 17 00:00:00 2001 From: networkException Date: Sun, 24 Oct 2021 23:06:35 +0200 Subject: [PATCH] Everywhere: Replace calls to console.assert and throws with assertions This patch replaces console.assert calls and random throw new Error calls to crash on undefined behavior with predefined assertion functions from utils. --- html/highlighter.ts | 25 ++++++++++--------------- html/tokenizer.ts | 30 ++++++++++-------------------- 2 files changed, 20 insertions(+), 35 deletions(-) diff --git a/html/highlighter.ts b/html/highlighter.ts index 67ae3ba..218981e 100644 --- a/html/highlighter.ts +++ b/html/highlighter.ts @@ -2,6 +2,7 @@ import { Palette } from "./highlighter/palette"; import { Node, Position } from "./highlighter/node"; import { State } from "./highlighter/state"; import { Token, Type } from "./tokenizer/token"; +import { TODO, VERIFY, VERIFY_NOT_REACHED } from "../util/assertions.js"; export class Highlighter { private state: State = State.Undefined; @@ -28,7 +29,7 @@ export class Highlighter { case Type.DOCTYPE: this.reconsumeIn(State.DOCTYPE); break; case Type.Comment: this.reconsumeIn(State.Comment); break; case Type.EndOfFile: this.finished = true; break; - default: throw new Error(`FIXME (Highlighter#spin, Unimplemented token type '${this.currentToken.type}')`); + default: TODO(`Highlighter#spin, Unimplemented token type '${this.currentToken.type}'`); } break; @@ -39,7 +40,7 @@ export class Highlighter { this.createNode({ position: { line: 0, character: 0 }, color: Palette.Plain, content: '' }); this.reconsumeIn(State.Plain); break; - default: throw new Error('BeforePlain') + default: VERIFY_NOT_REACHED(); } break; @@ -159,7 +160,7 @@ export class Highlighter { this.state = State.Undefined; break; - default: throw new Error(`FIXME (Highlighter#iterate, Unimplemented state '${this.state}')`); + default: TODO(`Highlighter#iterate, Unimplemented state '${this.state}'`); } } @@ -178,9 +179,7 @@ export class Highlighter { private consumeNextTokenOfType(type: T): Token & { type: T } { this.currentToken = this.tokens[this.pointer]; - console.assert(this.currentToken.type === type, { - message: `Highlighter#consumeNextOfType: Expected '${type}', got '${this.currentToken.type}' instead` - }); + VERIFY(this.currentToken.type === type, `Highlighter#consumeNextOfType: Expected '${type}', got '${this.currentToken.type}' instead`); this.pointer++; @@ -190,9 +189,8 @@ export class Highlighter { private consumeNextTokenOfEitherType(a: T, b: U): Token & { type: T | U } { this.currentToken = this.tokens[this.pointer]; - console.assert(this.currentToken.type === a || this.currentToken.type === b, { - message: `Highlighter#consumeNextTokenOfEitherType: Expected '${a}' or '${b}', got '${this.currentToken.type}' instead` - }); + VERIFY(this.currentToken.type === a || this.currentToken.type === b, + `Highlighter#consumeNextTokenOfEitherType: Expected '${a}' or '${b}', got '${this.currentToken.type}' instead`); this.pointer++; @@ -214,17 +212,14 @@ export class Highlighter { } private currentTokenOfType(type: T): Token & { type: T } { - console.assert(this.currentToken.type === type, { - message: `Highlighter#currentTokenOfType: Expected '${type}', got '${this.currentToken.type}' instead` - }); + VERIFY(this.currentToken.type === type, `Highlighter#currentTokenOfType: Expected '${type}', got '${this.currentToken.type}' instead`); return this.currentToken as Token & { type: T }; } private currentTokenOfEitherType(a: T, b: U): Token & { type: T | U } { - console.assert(this.currentToken.type === a || this.currentToken.type === b, { - message: `Highlighter#currentTokenOfEitherType: Expected '${a}' or '${b}', got '${this.currentToken.type}' instead` - }); + VERIFY(this.currentToken.type === a || this.currentToken.type === b, + `Highlighter#currentTokenOfEitherType: Expected '${a}' or '${b}', got '${this.currentToken.type}' instead`); return this.currentToken as Token & { type: T }; } diff --git a/html/tokenizer.ts b/html/tokenizer.ts index aec4c6d..0af5441 100644 --- a/html/tokenizer.ts +++ b/html/tokenizer.ts @@ -1,9 +1,9 @@ +import { TODO, VERIFY } from "../util/assertions.js"; import { ParseError } from "./errors.js"; import { entities } from "./tokenizer/entities.js"; import { State } from "./tokenizer/state.js"; import { AttributeList, Token, Type } from "./tokenizer/token.js"; -// FIXME: Replace console.assert calls will throwing errors export class Tokenizer { private state: State = State.Data; private returnState!: State; @@ -510,7 +510,7 @@ export class Tokenizer { break; } - default: throw new Error(`FIXME (Tokenizer#iterate, Unimplemented state '${this.state}')`); + default: TODO(`Tokenizer#iterate, Unimplemented state '${this.state}'`); } } @@ -581,9 +581,7 @@ export class Tokenizer { for (let i = 0; i < input.length; i++) { const consumed = this.consumeNext(); - console.assert(consumed === input[i], { - message: `Tokenizer#consumeNextFew: Expected '${input[i]}' (${input} at ${i}), got ${consumed} instead` - }); + VERIFY(consumed === input[i], `Tokenizer#consumeNextFew: Expected '${input[i]}' (${input} at ${i}), got ${consumed} instead`); } } @@ -591,9 +589,8 @@ export class Tokenizer { for (let i = 0; i < input.length; i++) { const consumed = this.consumeNext()?.toLowerCase(); - console.assert(consumed === input[i].toLowerCase(), { - message: `Tokenizer#consumeNextFewCaseInsensitive: Expected '${input[i].toLowerCase()}' (${input.toLowerCase()} at ${i}), got ${consumed} instead` - }); + VERIFY(consumed === input[i].toLowerCase(), + `Tokenizer#consumeNextFewCaseInsensitive: Expected '${input[i].toLowerCase()}' (${input.toLowerCase()} at ${i}), got ${consumed} instead`); } } @@ -602,33 +599,26 @@ export class Tokenizer { } private emitCurrentOfType(type: Type): void { - console.assert(this.currentToken.type === type, { - message: `Tokenizer#emitCurrentOfType: Expected '${type}', got '${this.currentToken.type}' instead` - }); + VERIFY(this.currentToken.type === type, `Tokenizer#emitCurrentOfType: Expected '${type}', got '${this.currentToken.type}' instead`); this.tokens.push(this.currentToken); } private emitCurrentOfEitherType(a: Type, b: Type): void { - console.assert(this.currentToken.type === a || this.currentToken.type === b, { - message: `Tokenizer#emitCurrentOfEitherType: Expected '${a}' or '${b}', got '${this.currentToken.type}' instead` - }); + VERIFY(this.currentToken.type === a || this.currentToken.type === b, `Tokenizer#emitCurrentOfEitherType: Expected '${a}' or '${b}', got '${this.currentToken.type}' instead`); this.tokens.push(this.currentToken); } private currentOfType(type: T): Token & { type: T } { - console.assert(this.currentToken.type === type, { - message: `Tokenizer#currentOfType: Expected '${type}', got '${this.currentToken.type}' instead` - }); + VERIFY(this.currentToken.type === type, `Tokenizer#currentOfType: Expected '${type}', got '${this.currentToken.type}' instead`); return this.currentToken as Token & { type: T }; } private currentOfEitherType(a: T, b: U): Token & { type: T | U } { - console.assert(this.currentToken.type === a || this.currentToken.type === b, { - message: `Tokenizer#currentOfEitherType: Expected '${a}' or '${b}', got '${this.currentToken.type}' instead` - }); + VERIFY(this.currentToken.type === a || this.currentToken.type === b, + `Tokenizer#currentOfEitherType: Expected '${a}' or '${b}', got '${this.currentToken.type}' instead`); return this.currentToken as Token & { type: T }; }