Skip to content

Commit a737332

Browse files
refactor(compiler): remove unnecessary ! operators from lexer (angular#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 angular#24571 PR Close angular#28055
1 parent 963dc06 commit a737332

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

packages/compiler/src/ml_parser/lexer.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,12 @@ export enum TokenType {
3636
}
3737

3838
export class Token {
39-
constructor(public type: TokenType, public parts: string[], public sourceSpan: ParseSourceSpan) {}
39+
constructor(
40+
public type: TokenType|null, public parts: string[], public sourceSpan: ParseSourceSpan) {}
4041
}
4142

4243
export class TokenError extends ParseError {
43-
constructor(errorMsg: string, public tokenType: TokenType, span: ParseSourceSpan) {
44+
constructor(errorMsg: string, public tokenType: TokenType|null, span: ParseSourceSpan) {
4445
super(span, errorMsg);
4546
}
4647
}
@@ -86,16 +87,13 @@ class _Tokenizer {
8687
private _length: number;
8788
private _tokenizeIcu: boolean;
8889
private _interpolationConfig: InterpolationConfig;
89-
// Note: this is always lowercase!
9090
private _peek: number = -1;
9191
private _nextPeek: number = -1;
9292
private _index: number = -1;
9393
private _line: number = 0;
9494
private _column: number = -1;
95-
// TODO(issue/24571): remove '!'.
96-
private _currentTokenStart !: ParseLocation;
97-
// TODO(issue/24571): remove '!'.
98-
private _currentTokenType !: TokenType;
95+
private _currentTokenStart: ParseLocation|null = null;
96+
private _currentTokenType: TokenType|null = null;
9997
private _expansionCaseStack: TokenType[] = [];
10098
private _inInterpolation: boolean = false;
10199

@@ -206,11 +204,21 @@ class _Tokenizer {
206204
}
207205

208206
private _endToken(parts: string[], end: ParseLocation = this._getLocation()): Token {
207+
if (this._currentTokenStart === null) {
208+
throw new TokenError(
209+
'Programming error - attempted to end a token when there was no start to the token',
210+
this._currentTokenType, this._getSpan(end, end));
211+
}
212+
if (this._currentTokenType === null) {
213+
throw new TokenError(
214+
'Programming error - attempted to end a token which has no token type', null,
215+
this._getSpan(this._currentTokenStart, end));
216+
}
209217
const token =
210218
new Token(this._currentTokenType, parts, new ParseSourceSpan(this._currentTokenStart, end));
211219
this.tokens.push(token);
212-
this._currentTokenStart = null !;
213-
this._currentTokenType = null !;
220+
this._currentTokenStart = null;
221+
this._currentTokenType = null;
214222
return token;
215223
}
216224

0 commit comments

Comments
 (0)