From 1b0580a9ec31d72097bde3d057abd6ca3b82559d Mon Sep 17 00:00:00 2001 From: Pete Bacon Darwin Date: Fri, 8 Feb 2019 22:10:19 +0000 Subject: [PATCH] refactor(compiler): remove unnecessary `!` operators from lexer (#28055) When we added the strict null checks, the lexer had some `!` operators added to prevent the compilation from failing. This commit resolves this problem correctly and removes the hacks. Also the comment ``` // Note: this is always lowercase! ``` has been removed as it is no longer true. See #24571 PR Close #28055 --- packages/compiler/src/ml_parser/lexer.ts | 26 ++++++++++++++++-------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/packages/compiler/src/ml_parser/lexer.ts b/packages/compiler/src/ml_parser/lexer.ts index 3df7c5e986..5f8b479232 100644 --- a/packages/compiler/src/ml_parser/lexer.ts +++ b/packages/compiler/src/ml_parser/lexer.ts @@ -36,11 +36,12 @@ export enum TokenType { } export class Token { - constructor(public type: TokenType, public parts: string[], public sourceSpan: ParseSourceSpan) {} + constructor( + public type: TokenType|null, public parts: string[], public sourceSpan: ParseSourceSpan) {} } export class TokenError extends ParseError { - constructor(errorMsg: string, public tokenType: TokenType, span: ParseSourceSpan) { + constructor(errorMsg: string, public tokenType: TokenType|null, span: ParseSourceSpan) { super(span, errorMsg); } } @@ -86,16 +87,13 @@ class _Tokenizer { private _length: number; private _tokenizeIcu: boolean; private _interpolationConfig: InterpolationConfig; - // Note: this is always lowercase! private _peek: number = -1; private _nextPeek: number = -1; private _index: number = -1; private _line: number = 0; private _column: number = -1; - // TODO(issue/24571): remove '!'. - private _currentTokenStart !: ParseLocation; - // TODO(issue/24571): remove '!'. - private _currentTokenType !: TokenType; + private _currentTokenStart: ParseLocation|null = null; + private _currentTokenType: TokenType|null = null; private _expansionCaseStack: TokenType[] = []; private _inInterpolation: boolean = false; @@ -206,11 +204,21 @@ class _Tokenizer { } private _endToken(parts: string[], end: ParseLocation = this._getLocation()): Token { + if (this._currentTokenStart === null) { + throw new TokenError( + 'Programming error - attempted to end a token when there was no start to the token', + this._currentTokenType, this._getSpan(end, end)); + } + if (this._currentTokenType === null) { + throw new TokenError( + 'Programming error - attempted to end a token which has no token type', null, + this._getSpan(this._currentTokenStart, end)); + } const token = new Token(this._currentTokenType, parts, new ParseSourceSpan(this._currentTokenStart, end)); this.tokens.push(token); - this._currentTokenStart = null !; - this._currentTokenType = null !; + this._currentTokenStart = null; + this._currentTokenType = null; return token; }