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

TypeScript incorrectly handles naughty strings in json #53519

Closed
Gabriel-Alves-Cunha opened this issue Mar 27, 2023 · 5 comments · Fixed by #53869
Closed

TypeScript incorrectly handles naughty strings in json #53519

Gabriel-Alves-Cunha opened this issue Mar 27, 2023 · 5 comments · Fixed by #53869
Labels
Bug A bug in TypeScript Help Wanted You can do this
Milestone

Comments

@Gabriel-Alves-Cunha
Copy link

Bug Report

🔎 Search Terms

bad naughty json string

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about bad strings in json

⏯ Playground Link

Playground link with relevant code

When you enter the playground, an alert message will appear:
image

Click at 'Cancel', otherwise, you'll not see the errors at the errors tab.

💻 Code

Code here will tell you nothing, you should see the playground above. Don't worry, it's not complicated at all.

🙁 Actual behavior

There are several errors reported by the compiler, you can see them in the image below:
image

🙂 Expected behavior

I believe the compiler should be able to correctly handle these potencial problematic strings.

@fatcerberus
Copy link

I mean it seems to me like the purpose of the warning is precisely because the compiler won’t be able to handle the file correctly otherwise…

@ferrybig
Copy link

Typescript expects JavaScript or TypeScript code, you are providing it json which is not a valid JavaScript or TypeScript input, as JSON allows raw characters that are not allowed in JavaScript and TypeScript.

If you do not want to encode your JSOn input into a valid JavaScript/TypeScript input, place it in a seperate .json file and import it

@Josh-Cena
Copy link
Contributor

Josh-Cena commented Mar 27, 2023

JSON allows raw characters that are not allowed in JavaScript and TypeScript.

Not the case anymore after https://github.com/tc39/proposal-json-superset—all JSON are now valid ES expressions, including unescaped LS and PS in string literals.

@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript Help Wanted You can do this labels Mar 27, 2023
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Mar 27, 2023
@RyanCavanaugh
Copy link
Member

Can we use more technically-precise terminology than "bad" or "naughty" ? What is the nature of the strings that aren't handled correctly?

@Josh-Cena
Copy link
Contributor

Josh-Cena commented Mar 27, 2023

@RyanCavanaugh The buggy code reported here has exactly one problem:

const myjson = [
  "
test
",
] as const;

This string has a U+2029 Paragraph Separator character on both ends. After JSON-superset, this should be valid ES code, but seems the TS lexer is not updated yet. I assume the same error would occur with U+2028 Line Separator.

liviu-gheorghe pushed a commit to liviu-gheorghe/TypeScript that referenced this issue Apr 17, 2023
liviu-gheorghe pushed a commit to liviu-gheorghe/TypeScript that referenced this issue Apr 27, 2023
…ph and line separators in string literal
Gudahtt added a commit to MetaMask/metamask-mobile that referenced this issue Sep 1, 2023
A new `lint:tsc` command has been added to validate TypeScript types in
the project. This lint command uses a separate configuration,
`tsconfig.lint.ts`, which covers just the files that have no type
errors. We can expand the type check coverage over time as we fix more
type errors.

One additional change was made to fix a TypeScript error that hade an
impact on many files. The problem was caused by a TypeScript bug that
causes the compiler to fail when it encounters certain unicode
characters. Those characters were in a JSON file in the
`unicode-confusables` package. They have been temporarily removed until
this TypeScript error can be fixed.

For more details, see microsoft/TypeScript#53519
Gudahtt added a commit to MetaMask/metamask-mobile that referenced this issue Sep 1, 2023
A new `lint:tsc` command has been added to validate TypeScript types in
the project. This lint command uses a separate configuration,
`tsconfig.lint.ts`, which covers just the files that have no type
errors. We can expand the type check coverage over time as we fix more
type errors.

One additional change was made to fix a TypeScript error that hade an
impact on many files. The problem was caused by a TypeScript bug that
causes the compiler to fail when it encounters certain unicode
characters. Those characters were in a JSON file in the
`unicode-confusables` package. They have been temporarily removed until
this TypeScript error can be fixed.

For more details, see microsoft/TypeScript#53519
Gudahtt added a commit to MetaMask/metamask-mobile that referenced this issue Sep 1, 2023
A new `lint:tsc` command has been added to validate TypeScript types in
the project. This lint command uses a separate configuration,
`tsconfig.lint.ts`, which covers just the files that have no type
errors. We can expand the type check coverage over time as we fix more
type errors.

One additional change was made to fix a TypeScript error that hade an
impact on many files. The problem was caused by a TypeScript bug that
causes the compiler to fail when it encounters certain unicode
characters. Those characters were in a JSON file in the
`unicode-confusables` package. They have been temporarily removed until
this TypeScript error can be fixed.

For more details, see microsoft/TypeScript#53519
Gudahtt added a commit to MetaMask/metamask-mobile that referenced this issue Sep 1, 2023
A new `lint:tsc` command has been added to validate TypeScript types in
the project. This lint command uses a separate configuration,
`tsconfig.lint.ts`, which covers just the files that have no type
errors. We can expand the type check coverage over time as we fix more
type errors.

One additional change was made to fix a TypeScript error that hade an
impact on many files. The problem was caused by a TypeScript bug that
causes the compiler to fail when it encounters certain unicode
characters. Those characters were in a JSON file in the
`unicode-confusables` package. They have been temporarily removed until
this TypeScript error can be fixed.

For more details, see microsoft/TypeScript#53519
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Help Wanted You can do this
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants