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

Remove a few deopts already #4311

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/language/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export class Token {
* Note: is undefined for punctuation tokens, but typed as string for
* convenience in the parser.
*/
readonly value: string;
readonly value: string | undefined;

/**
* Tokens exist as nodes in a double-linked-list amongst all tokens
Expand All @@ -101,15 +101,14 @@ export class Token {
end: number,
line: number,
column: number,
value?: string,
value: string | undefined,
) {
this.kind = kind;
this.start = start;
this.end = end;
this.line = line;
this.column = column;
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
this.value = value!;
this.value = value;
this.prev = null;
this.next = null;
}
Expand Down
13 changes: 10 additions & 3 deletions src/language/blockString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,19 @@ export function dedentBlockStringLines(
}
}

if (lastNonEmptyLine === -1) {
return [];
}

firstNonEmptyLine = firstNonEmptyLine ?? 0;

const removeCommonIndentation = (line: string, i: number) =>
i === 0 ? line : commonIndent && line ? line.substring(commonIndent) : line;
return (
lines
// Remove common indentation from all lines but first.
.map((line, i) => (i === 0 ? line : line.slice(commonIndent)))
// Remove leading and trailing blank lines.
.slice(firstNonEmptyLine ?? 0, lastNonEmptyLine + 1)
.map(removeCommonIndentation)
.slice(firstNonEmptyLine, lastNonEmptyLine + 1)
);
}

Expand Down
148 changes: 117 additions & 31 deletions src/language/lexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class Lexer {
lineStart: number;

constructor(source: Source) {
const startOfFileToken = new Token(TokenKind.SOF, 0, 0, 0, 0);
const startOfFileToken = new Token(TokenKind.SOF, 0, 0, 0, 0, undefined);

this.source = source;
this.lastToken = startOfFileToken;
Expand Down Expand Up @@ -174,11 +174,10 @@ function createToken(
kind: TokenKind,
start: number,
end: number,
value?: string,
value: string | undefined,
): Token {
const line = lexer.line;
const col = 1 + start - lexer.lineStart;
return new Token(kind, start, end, line, col, value);
const col = start - (lexer.lineStart - 1);
return new Token(kind, start, end, lexer.line, col, value);
}

/**
Expand Down Expand Up @@ -248,39 +247,123 @@ function readNextToken(lexer: Lexer, start: number): Token {
//
// Punctuator :: one of ! $ & ( ) ... : = @ [ ] { | }
case 0x0021: // !
return createToken(lexer, TokenKind.BANG, position, position + 1);
return createToken(
lexer,
TokenKind.BANG,
position,
position + 1,
undefined,
);
case 0x0024: // $
return createToken(lexer, TokenKind.DOLLAR, position, position + 1);
return createToken(
lexer,
TokenKind.DOLLAR,
position,
position + 1,
undefined,
);
case 0x0026: // &
return createToken(lexer, TokenKind.AMP, position, position + 1);
return createToken(
lexer,
TokenKind.AMP,
position,
position + 1,
undefined,
);
case 0x0028: // (
return createToken(lexer, TokenKind.PAREN_L, position, position + 1);
return createToken(
lexer,
TokenKind.PAREN_L,
position,
position + 1,
undefined,
);
case 0x0029: // )
return createToken(lexer, TokenKind.PAREN_R, position, position + 1);
return createToken(
lexer,
TokenKind.PAREN_R,
position,
position + 1,
undefined,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consistently calling createToken with 5 arguments helps V8 inline the cache

);
case 0x002e: // .
if (
body.charCodeAt(position + 1) === 0x002e &&
body.charCodeAt(position + 2) === 0x002e
) {
return createToken(lexer, TokenKind.SPREAD, position, position + 3);
return createToken(
lexer,
TokenKind.SPREAD,
position,
position + 3,
undefined,
);
}
break;
case 0x003a: // :
return createToken(lexer, TokenKind.COLON, position, position + 1);
return createToken(
lexer,
TokenKind.COLON,
position,
position + 1,
undefined,
);
case 0x003d: // =
return createToken(lexer, TokenKind.EQUALS, position, position + 1);
return createToken(
lexer,
TokenKind.EQUALS,
position,
position + 1,
undefined,
);
case 0x0040: // @
return createToken(lexer, TokenKind.AT, position, position + 1);
return createToken(
lexer,
TokenKind.AT,
position,
position + 1,
undefined,
);
case 0x005b: // [
return createToken(lexer, TokenKind.BRACKET_L, position, position + 1);
return createToken(
lexer,
TokenKind.BRACKET_L,
position,
position + 1,
undefined,
);
case 0x005d: // ]
return createToken(lexer, TokenKind.BRACKET_R, position, position + 1);
return createToken(
lexer,
TokenKind.BRACKET_R,
position,
position + 1,
undefined,
);
case 0x007b: // {
return createToken(lexer, TokenKind.BRACE_L, position, position + 1);
return createToken(
lexer,
TokenKind.BRACE_L,
position,
position + 1,
undefined,
);
case 0x007c: // |
return createToken(lexer, TokenKind.PIPE, position, position + 1);
return createToken(
lexer,
TokenKind.PIPE,
position,
position + 1,
undefined,
);
case 0x007d: // }
return createToken(lexer, TokenKind.BRACE_R, position, position + 1);
return createToken(
lexer,
TokenKind.BRACE_R,
position,
position + 1,
undefined,
);
// StringValue
case 0x0022: // "
if (
Expand Down Expand Up @@ -313,7 +396,7 @@ function readNextToken(lexer: Lexer, start: number): Token {
);
}

return createToken(lexer, TokenKind.EOF, bodyLength, bodyLength);
return createToken(lexer, TokenKind.EOF, bodyLength, bodyLength, undefined);
}

/**
Expand Down Expand Up @@ -353,7 +436,7 @@ function readComment(lexer: Lexer, start: number): Token {
TokenKind.COMMENT,
start,
position,
body.slice(start + 1, position),
body.substring(start + 1, position),
);
}

Expand Down Expand Up @@ -454,7 +537,7 @@ function readNumber(lexer: Lexer, start: number, firstCode: number): Token {
isFloat ? TokenKind.FLOAT : TokenKind.INT,
start,
position,
body.slice(start, position),
body.substring(start, position),
);
}

Expand Down Expand Up @@ -515,13 +598,13 @@ function readString(lexer: Lexer, start: number): Token {

// Closing Quote (")
if (code === 0x0022) {
value += body.slice(chunkStart, position);
value += body.substring(chunkStart, position);
return createToken(lexer, TokenKind.STRING, start, position + 1, value);
}

// Escape Sequence (\)
if (code === 0x005c) {
value += body.slice(chunkStart, position);
value += body.substring(chunkStart, position);
const escape =
body.charCodeAt(position + 1) === 0x0075 // u
? body.charCodeAt(position + 2) === 0x007b // {
Expand Down Expand Up @@ -593,7 +676,7 @@ function readEscapedUnicodeVariableWidth(
throw syntaxError(
lexer.source,
position,
`Invalid Unicode escape sequence: "${body.slice(
`Invalid Unicode escape sequence: "${body.substring(
position,
position + size,
)}".`,
Expand Down Expand Up @@ -635,7 +718,10 @@ function readEscapedUnicodeFixedWidth(
throw syntaxError(
lexer.source,
position,
`Invalid Unicode escape sequence: "${body.slice(position, position + 6)}".`,
`Invalid Unicode escape sequence: "${body.substring(
position,
position + 6,
)}".`,
);
}

Expand Down Expand Up @@ -717,7 +803,7 @@ function readEscapedCharacter(lexer: Lexer, position: number): EscapeSequence {
throw syntaxError(
lexer.source,
position,
`Invalid character escape sequence: "${body.slice(
`Invalid character escape sequence: "${body.substring(
position,
position + 2,
)}".`,
Expand Down Expand Up @@ -755,7 +841,7 @@ function readBlockString(lexer: Lexer, start: number): Token {
body.charCodeAt(position + 1) === 0x0022 &&
body.charCodeAt(position + 2) === 0x0022
) {
currentLine += body.slice(chunkStart, position);
currentLine += body.substring(chunkStart, position);
blockLines.push(currentLine);

const token = createToken(
Expand All @@ -779,15 +865,15 @@ function readBlockString(lexer: Lexer, start: number): Token {
body.charCodeAt(position + 2) === 0x0022 &&
body.charCodeAt(position + 3) === 0x0022
) {
currentLine += body.slice(chunkStart, position);
currentLine += body.substring(chunkStart, position);
chunkStart = position + 1; // skip only slash
position += 4;
continue;
}

// LineTerminator
if (code === 0x000a || code === 0x000d) {
currentLine += body.slice(chunkStart, position);
currentLine += body.substring(chunkStart, position);
blockLines.push(currentLine);

if (code === 0x000d && body.charCodeAt(position + 1) === 0x000a) {
Expand Down Expand Up @@ -849,6 +935,6 @@ function readName(lexer: Lexer, start: number): Token {
TokenKind.NAME,
start,
position,
body.slice(start, position),
body.substring(start, position),
);
}
2 changes: 2 additions & 0 deletions src/language/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@
: this._lexer.token;

if (keywordToken.kind === TokenKind.NAME) {
// eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check
switch (keywordToken.value) {
case 'schema':
return this.parseSchemaDefinition();
Expand Down Expand Up @@ -296,6 +297,7 @@
);
}

// eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check
switch (keywordToken.value) {
case 'query':
case 'mutation':
Expand Down Expand Up @@ -350,7 +352,7 @@
*/
parseOperationType(): OperationTypeNode {
const operationToken = this.expectToken(TokenKind.NAME);
switch (operationToken.value) {

Check failure on line 355 in src/language/parser.ts

View workflow job for this annotation

GitHub Actions / ci / Lint source files

Switch is not exhaustive. Cases not matched: undefined | string

Check failure on line 355 in src/language/parser.ts

View workflow job for this annotation

GitHub Actions / ci / Lint source files

Switch is not exhaustive. Cases not matched: undefined | string
case 'query':
return OperationTypeNode.QUERY;
case 'mutation':
Expand Down Expand Up @@ -1131,7 +1133,7 @@
const keywordToken = this._lexer.lookahead();

if (keywordToken.kind === TokenKind.NAME) {
switch (keywordToken.value) {

Check failure on line 1136 in src/language/parser.ts

View workflow job for this annotation

GitHub Actions / ci / Lint source files

Switch is not exhaustive. Cases not matched: undefined | string

Check failure on line 1136 in src/language/parser.ts

View workflow job for this annotation

GitHub Actions / ci / Lint source files

Switch is not exhaustive. Cases not matched: undefined | string
case 'schema':
return this.parseSchemaExtension();
case 'scalar':
Expand Down
4 changes: 3 additions & 1 deletion src/utilities/stripIgnoredCharacters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ export function stripIgnoredCharacters(source: string | Source): string {

const tokenBody = body.slice(currentToken.start, currentToken.end);
if (tokenKind === TokenKind.BLOCK_STRING) {
strippedBody += printBlockString(currentToken.value, { minimize: true });
strippedBody += printBlockString(currentToken.value ?? '', {
minimize: true,
});
} else {
strippedBody += tokenBody;
}
Expand Down
Loading