mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-12-12 03:20:56 -06:00
Detect binary files by checking first 256 code units for 0xFFFD (#57008)
This commit is contained in:
parent
59b6f78c29
commit
ad2f465a38
@ -1795,12 +1795,14 @@ export function createScanner(languageVersion: ScriptTarget, skipTrivia: boolean
|
||||
|
||||
const ch = codePointAt(text, pos);
|
||||
if (pos === 0) {
|
||||
// If a file wasn't valid text at all, it will usually be apparent at
|
||||
// position 0 because UTF-8 decode will fail and produce U+FFFD.
|
||||
// If a file isn't valid text at all, it will usually be apparent
|
||||
// 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
|
||||
if (ch === CharacterCodes.replacementCharacter) {
|
||||
// Jump to the end of the file and fail.
|
||||
// this is likely a binary file that cannot be parsed.
|
||||
//
|
||||
// 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;
|
||||
|
||||
@ -7532,9 +7532,6 @@ export const enum CharacterCodes {
|
||||
mathematicalSpace = 0x205F,
|
||||
ogham = 0x1680,
|
||||
|
||||
// Unicode replacement character produced when a byte sequence is invalid
|
||||
replacementCharacter = 0xFFFD,
|
||||
|
||||
_ = 0x5F,
|
||||
$ = 0x24,
|
||||
|
||||
|
||||
7
tests/baselines/reference/TransportStream.errors.txt
Normal file
7
tests/baselines/reference/TransportStream.errors.txt
Normal file
@ -0,0 +1,7 @@
|
||||
TransportStream.ts(1,1): error TS1490: File appears to be binary.
|
||||
|
||||
|
||||
==== TransportStream.ts (1 errors) ====
|
||||
G@<04>G@<05>G@<06>
|
||||
|
||||
!!! error TS1490: File appears to be binary.
|
||||
6
tests/baselines/reference/TransportStream.js
Normal file
6
tests/baselines/reference/TransportStream.js
Normal file
@ -0,0 +1,6 @@
|
||||
//// [tests/cases/compiler/TransportStream.ts] ////
|
||||
|
||||
//// [TransportStream.ts]
|
||||
G@<EFBFBD>G@<EFBFBD>G@<EFBFBD>
|
||||
|
||||
//// [TransportStream.js]
|
||||
5
tests/baselines/reference/TransportStream.symbols
Normal file
5
tests/baselines/reference/TransportStream.symbols
Normal file
@ -0,0 +1,5 @@
|
||||
//// [tests/cases/compiler/TransportStream.ts] ////
|
||||
|
||||
=== TransportStream.ts ===
|
||||
|
||||
G@<04>G@<05>G@<06>
|
||||
5
tests/baselines/reference/TransportStream.types
Normal file
5
tests/baselines/reference/TransportStream.types
Normal file
@ -0,0 +1,5 @@
|
||||
//// [tests/cases/compiler/TransportStream.ts] ////
|
||||
|
||||
=== TransportStream.ts ===
|
||||
|
||||
G@<04>G@<05>G@<06>
|
||||
1
tests/cases/compiler/TransportStream.ts
Normal file
1
tests/cases/compiler/TransportStream.ts
Normal file
@ -0,0 +1 @@
|
||||
G@<EFBFBD>G@<EFBFBD>G@<EFBFBD>
|
||||
Loading…
x
Reference in New Issue
Block a user