Skip to content

Commit

Permalink
Just simply check via includes
Browse files Browse the repository at this point in the history
  • Loading branch information
jakebailey committed Jan 16, 2024
1 parent 4ee2758 commit eb2dcc7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 14 deletions.
18 changes: 7 additions & 11 deletions src/compiler/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1799,17 +1799,13 @@ export function createScanner(languageVersion: ScriptTarget, skipTrivia: boolean
// in the first few characters because UTF-8 decode will fail and produce U+FFFD.
// If that happens, just issue one error and refuse to try to scan further;
// this is likely a binary file that cannot be parsed.
let i = 0;
const stop = Math.min(text.length, 256);
while (i < stop) {
const ch = codePointAt(text, i);
// Jump to the end of the file and fail.
if (ch === CharacterCodes.replacementCharacter) {
error(Diagnostics.File_appears_to_be_binary);
pos = end;
return token = SyntaxKind.NonTextFileMarkerTrivia;
}
i += charSize(ch);
//
// It's safe to slice the text; U+FFFD can only be produced by an invalid decode,
// so even if we cut a surrogate pair in half, they wouldn't be U+FFFD.
if (text.slice(0, 256).includes("\uFFFD")) {
error(Diagnostics.File_appears_to_be_binary);
pos = end;
return token = SyntaxKind.NonTextFileMarkerTrivia;
}
// Special handling for shebang
if (ch === CharacterCodes.hash && isShebangTrivia(text, pos)) {
Expand Down
3 changes: 0 additions & 3 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7531,9 +7531,6 @@ export const enum CharacterCodes {
mathematicalSpace = 0x205F,
ogham = 0x1680,

// Unicode replacement character produced when a byte sequence is invalid
replacementCharacter = 0xFFFD,

_ = 0x5F,
$ = 0x24,

Expand Down

0 comments on commit eb2dcc7

Please sign in to comment.