Skip to content

Commit eb2dcc7

Browse files
committed
Just simply check via includes
1 parent 4ee2758 commit eb2dcc7

File tree

2 files changed

+7
-14
lines changed

2 files changed

+7
-14
lines changed

src/compiler/scanner.ts

+7-11
Original file line numberDiff line numberDiff line change
@@ -1799,17 +1799,13 @@ export function createScanner(languageVersion: ScriptTarget, skipTrivia: boolean
17991799
// in the first few characters because UTF-8 decode will fail and produce U+FFFD.
18001800
// If that happens, just issue one error and refuse to try to scan further;
18011801
// this is likely a binary file that cannot be parsed.
1802-
let i = 0;
1803-
const stop = Math.min(text.length, 256);
1804-
while (i < stop) {
1805-
const ch = codePointAt(text, i);
1806-
// Jump to the end of the file and fail.
1807-
if (ch === CharacterCodes.replacementCharacter) {
1808-
error(Diagnostics.File_appears_to_be_binary);
1809-
pos = end;
1810-
return token = SyntaxKind.NonTextFileMarkerTrivia;
1811-
}
1812-
i += charSize(ch);
1802+
//
1803+
// It's safe to slice the text; U+FFFD can only be produced by an invalid decode,
1804+
// so even if we cut a surrogate pair in half, they wouldn't be U+FFFD.
1805+
if (text.slice(0, 256).includes("\uFFFD")) {
1806+
error(Diagnostics.File_appears_to_be_binary);
1807+
pos = end;
1808+
return token = SyntaxKind.NonTextFileMarkerTrivia;
18131809
}
18141810
// Special handling for shebang
18151811
if (ch === CharacterCodes.hash && isShebangTrivia(text, pos)) {

src/compiler/types.ts

-3
Original file line numberDiff line numberDiff line change
@@ -7531,9 +7531,6 @@ export const enum CharacterCodes {
75317531
mathematicalSpace = 0x205F,
75327532
ogham = 0x1680,
75337533

7534-
// Unicode replacement character produced when a byte sequence is invalid
7535-
replacementCharacter = 0xFFFD,
7536-
75377534
_ = 0x5F,
75387535
$ = 0x24,
75397536

0 commit comments

Comments
 (0)