Fix bad JSON syntax in tests, use JSON.parse first for perf (#61901)

This commit is contained in:
Jake Bailey
2025-06-24 14:56:43 -07:00
parent d4a3126838
commit e4e9fe741f
57 changed files with 169 additions and 119 deletions
+12 -3
View File
@@ -7692,9 +7692,18 @@ export function base64decode(host: { base64decode?(input: string): string; } | u
export function readJsonOrUndefined(path: string, hostOrText: { readFile(fileName: string): string | undefined; } | string): object | undefined {
const jsonText = isString(hostOrText) ? hostOrText : hostOrText.readFile(path);
if (!jsonText) return undefined;
// gracefully handle if readFile fails or returns not JSON
const result = parseConfigFileTextToJson(path, jsonText);
return !result.error ? result.config : undefined;
// Try strictly parsing first, then fall back to our (slower)
// parser that is resilient to comments/trailing commas.
// package.json files should never have these, but we
// have no way to communicate these issues in the first place.
let result = tryParseJson(jsonText);
if (result === undefined) {
const looseResult = parseConfigFileTextToJson(path, jsonText);
if (!looseResult.error) {
result = looseResult.config;
}
}
return result;
}
/** @internal */
@@ -10,7 +10,7 @@ error TS5110: Option 'module' must be set to 'Node16' when option 'moduleResolut
".": {
"import": "./dist/index.mjs",
"require": "./dist/index.js",
"types": "./dist/index.d.ts",
"types": "./dist/index.d.ts"
}
}
}
@@ -26,4 +26,5 @@ error TS5110: Option 'module' must be set to 'Node16' when option 'moduleResolut
// Should be an untyped resolution to dep/dist/index.mjs,
// but the first search is only for TS files, and when
// there's no dist/index.d.mts, it continues looking for
// matching conditions and resolves via `types`.
// matching conditions and resolves via `types`.
@@ -10,7 +10,7 @@ error TS5110: Option 'module' must be set to 'NodeNext' when option 'moduleResol
".": {
"import": "./dist/index.mjs",
"require": "./dist/index.js",
"types": "./dist/index.d.ts",
"types": "./dist/index.d.ts"
}
}
}
@@ -26,4 +26,5 @@ error TS5110: Option 'module' must be set to 'NodeNext' when option 'moduleResol
// Should be an untyped resolution to dep/dist/index.mjs,
// but the first search is only for TS files, and when
// there's no dist/index.d.mts, it continues looking for
// matching conditions and resolves via `types`.
// matching conditions and resolves via `types`.
@@ -126,7 +126,7 @@ node_modules/inner/index.d.ts(2,13): error TS2303: Circular definition of import
"./types": {
"types": {
"import": "./index.d.mts",
"require": "./index.d.cts",
"require": "./index.d.cts"
},
"node": {
"import": "./index.mjs",
@@ -134,4 +134,5 @@ node_modules/inner/index.d.ts(2,13): error TS2303: Circular definition of import
}
}
}
}
}
@@ -112,7 +112,7 @@ export const cjsSource = true;
"./types": {
"types": {
"import": "./index.d.mts",
"require": "./index.d.cts",
"require": "./index.d.cts"
},
"node": {
"import": "./index.mjs",
@@ -120,7 +120,8 @@ export const cjsSource = true;
}
}
}
}
}
//// [index.mjs]
// esm format file
@@ -126,7 +126,7 @@ node_modules/inner/index.d.ts(2,13): error TS2303: Circular definition of import
"./types": {
"types": {
"import": "./index.d.mts",
"require": "./index.d.cts",
"require": "./index.d.cts"
},
"node": {
"import": "./index.mjs",
@@ -134,4 +134,5 @@ node_modules/inner/index.d.ts(2,13): error TS2303: Circular definition of import
}
}
}
}
}
@@ -112,7 +112,7 @@ export const cjsSource = true;
"./types": {
"types": {
"import": "./index.d.mts",
"require": "./index.d.cts",
"require": "./index.d.cts"
},
"node": {
"import": "./index.mjs",
@@ -120,7 +120,8 @@ export const cjsSource = true;
}
}
}
}
}
//// [index.mjs]
// esm format file
@@ -120,7 +120,7 @@ node_modules/inner/index.d.ts(2,13): error TS2303: Circular definition of import
"./types": {
"types": {
"import": "./index.d.mts",
"require": "./index.d.cts",
"require": "./index.d.cts"
},
"node": {
"import": "./index.mjs",
@@ -128,4 +128,5 @@ node_modules/inner/index.d.ts(2,13): error TS2303: Circular definition of import
}
}
}
}
}
@@ -112,7 +112,7 @@ export const cjsSource = true;
"./types": {
"types": {
"import": "./index.d.mts",
"require": "./index.d.cts",
"require": "./index.d.cts"
},
"node": {
"import": "./index.mjs",
@@ -120,7 +120,8 @@ export const cjsSource = true;
}
}
}
}
}
//// [index.mjs]
// esm format file
@@ -64,7 +64,7 @@ node_modules/inner/index.d.ts(3,22): error TS1479: The current file is a CommonJ
{
"name": "package",
"private": true,
"type": "module",
"type": "module"
}
==== node_modules/inner/package.json (0 errors) ====
{
@@ -75,4 +75,5 @@ node_modules/inner/index.d.ts(3,22): error TS1479: The current file is a CommonJ
"./mjs/*": "./*.mjs",
"./js/*": "./*.js"
}
}
}
@@ -52,7 +52,7 @@ export { type };
{
"name": "package",
"private": true,
"type": "module",
"type": "module"
}
//// [package.json]
{
@@ -63,7 +63,8 @@ export { type };
"./mjs/*": "./*.mjs",
"./js/*": "./*.js"
}
}
}
//// [index.js]
// esm format file
@@ -64,7 +64,7 @@ node_modules/inner/index.d.ts(3,22): error TS1479: The current file is a CommonJ
{
"name": "package",
"private": true,
"type": "module",
"type": "module"
}
==== node_modules/inner/package.json (0 errors) ====
{
@@ -75,4 +75,5 @@ node_modules/inner/index.d.ts(3,22): error TS1479: The current file is a CommonJ
"./mjs/*": "./*.mjs",
"./js/*": "./*.js"
}
}
}
@@ -52,7 +52,7 @@ export { type };
{
"name": "package",
"private": true,
"type": "module",
"type": "module"
}
//// [package.json]
{
@@ -63,7 +63,8 @@ export { type };
"./mjs/*": "./*.mjs",
"./js/*": "./*.js"
}
}
}
//// [index.js]
// esm format file
@@ -55,7 +55,7 @@ node_modules/inner/index.d.ts(2,13): error TS2303: Circular definition of import
{
"name": "package",
"private": true,
"type": "module",
"type": "module"
}
==== node_modules/inner/package.json (0 errors) ====
{
@@ -66,4 +66,5 @@ node_modules/inner/index.d.ts(2,13): error TS2303: Circular definition of import
"./mjs/*": "./*.mjs",
"./js/*": "./*.js"
}
}
}
@@ -52,7 +52,7 @@ export { type };
{
"name": "package",
"private": true,
"type": "module",
"type": "module"
}
//// [package.json]
{
@@ -63,7 +63,8 @@ export { type };
"./mjs/*": "./*.mjs",
"./js/*": "./*.js"
}
}
}
//// [index.js]
// esm format file
@@ -64,7 +64,7 @@ node_modules/inner/index.d.ts(3,22): error TS1479: The current file is a CommonJ
{
"name": "package",
"private": true,
"type": "module",
"type": "module"
}
==== node_modules/inner/package.json (0 errors) ====
{
@@ -75,4 +75,5 @@ node_modules/inner/index.d.ts(3,22): error TS1479: The current file is a CommonJ
"./mjs/*.mjs": "./*.mjs",
"./js/*.js": "./*.js"
}
}
}
@@ -52,7 +52,7 @@ export { type };
{
"name": "package",
"private": true,
"type": "module",
"type": "module"
}
//// [package.json]
{
@@ -63,7 +63,8 @@ export { type };
"./mjs/*.mjs": "./*.mjs",
"./js/*.js": "./*.js"
}
}
}
//// [index.js]
// esm format file
@@ -64,7 +64,7 @@ node_modules/inner/index.d.ts(3,22): error TS1479: The current file is a CommonJ
{
"name": "package",
"private": true,
"type": "module",
"type": "module"
}
==== node_modules/inner/package.json (0 errors) ====
{
@@ -75,4 +75,5 @@ node_modules/inner/index.d.ts(3,22): error TS1479: The current file is a CommonJ
"./mjs/*.mjs": "./*.mjs",
"./js/*.js": "./*.js"
}
}
}
@@ -52,7 +52,7 @@ export { type };
{
"name": "package",
"private": true,
"type": "module",
"type": "module"
}
//// [package.json]
{
@@ -63,7 +63,8 @@ export { type };
"./mjs/*.mjs": "./*.mjs",
"./js/*.js": "./*.js"
}
}
}
//// [index.js]
// esm format file
@@ -55,7 +55,7 @@ node_modules/inner/index.d.ts(2,13): error TS2303: Circular definition of import
{
"name": "package",
"private": true,
"type": "module",
"type": "module"
}
==== node_modules/inner/package.json (0 errors) ====
{
@@ -66,4 +66,5 @@ node_modules/inner/index.d.ts(2,13): error TS2303: Circular definition of import
"./mjs/*.mjs": "./*.mjs",
"./js/*.js": "./*.js"
}
}
}
@@ -52,7 +52,7 @@ export { type };
{
"name": "package",
"private": true,
"type": "module",
"type": "module"
}
//// [package.json]
{
@@ -63,7 +63,8 @@ export { type };
"./mjs/*.mjs": "./*.mjs",
"./js/*.js": "./*.js"
}
}
}
//// [index.js]
// esm format file
@@ -126,7 +126,7 @@ node_modules/inner/index.d.ts(2,13): error TS2303: Circular definition of import
"./types": {
"types": {
"import": "./index.d.mts",
"require": "./index.d.cts",
"require": "./index.d.cts"
},
"node": {
"import": "./index.mjs",
@@ -134,4 +134,5 @@ node_modules/inner/index.d.ts(2,13): error TS2303: Circular definition of import
}
}
}
}
}
@@ -112,7 +112,7 @@ export const cjsSource = true;
"./types": {
"types": {
"import": "./index.d.mts",
"require": "./index.d.cts",
"require": "./index.d.cts"
},
"node": {
"import": "./index.mjs",
@@ -120,7 +120,8 @@ export const cjsSource = true;
}
}
}
}
}
//// [index.mjs]
// esm format file
@@ -126,7 +126,7 @@ node_modules/inner/index.d.ts(2,13): error TS2303: Circular definition of import
"./types": {
"types": {
"import": "./index.d.mts",
"require": "./index.d.cts",
"require": "./index.d.cts"
},
"node": {
"import": "./index.mjs",
@@ -134,4 +134,5 @@ node_modules/inner/index.d.ts(2,13): error TS2303: Circular definition of import
}
}
}
}
}
@@ -112,7 +112,7 @@ export const cjsSource = true;
"./types": {
"types": {
"import": "./index.d.mts",
"require": "./index.d.cts",
"require": "./index.d.cts"
},
"node": {
"import": "./index.mjs",
@@ -120,7 +120,8 @@ export const cjsSource = true;
}
}
}
}
}
//// [index.mjs]
// esm format file
@@ -120,7 +120,7 @@ node_modules/inner/index.d.ts(2,13): error TS2303: Circular definition of import
"./types": {
"types": {
"import": "./index.d.mts",
"require": "./index.d.cts",
"require": "./index.d.cts"
},
"node": {
"import": "./index.mjs",
@@ -128,4 +128,5 @@ node_modules/inner/index.d.ts(2,13): error TS2303: Circular definition of import
}
}
}
}
}
@@ -112,7 +112,7 @@ export const cjsSource = true;
"./types": {
"types": {
"import": "./index.d.mts",
"require": "./index.d.cts",
"require": "./index.d.cts"
},
"node": {
"import": "./index.mjs",
@@ -120,7 +120,8 @@ export const cjsSource = true;
}
}
}
}
}
//// [index.mjs]
// esm format file
@@ -64,7 +64,7 @@ node_modules/inner/index.d.ts(3,22): error TS1479: The current file is a CommonJ
{
"name": "package",
"private": true,
"type": "module",
"type": "module"
}
==== node_modules/inner/package.json (0 errors) ====
{
@@ -75,4 +75,5 @@ node_modules/inner/index.d.ts(3,22): error TS1479: The current file is a CommonJ
"./mjs/*": "./*.mjs",
"./js/*": "./*.js"
}
}
}
@@ -52,7 +52,7 @@ export { type };
{
"name": "package",
"private": true,
"type": "module",
"type": "module"
}
//// [package.json]
{
@@ -63,7 +63,8 @@ export { type };
"./mjs/*": "./*.mjs",
"./js/*": "./*.js"
}
}
}
//// [index.js]
// esm format file
@@ -64,7 +64,7 @@ node_modules/inner/index.d.ts(3,22): error TS1479: The current file is a CommonJ
{
"name": "package",
"private": true,
"type": "module",
"type": "module"
}
==== node_modules/inner/package.json (0 errors) ====
{
@@ -75,4 +75,5 @@ node_modules/inner/index.d.ts(3,22): error TS1479: The current file is a CommonJ
"./mjs/*": "./*.mjs",
"./js/*": "./*.js"
}
}
}
@@ -52,7 +52,7 @@ export { type };
{
"name": "package",
"private": true,
"type": "module",
"type": "module"
}
//// [package.json]
{
@@ -63,7 +63,8 @@ export { type };
"./mjs/*": "./*.mjs",
"./js/*": "./*.js"
}
}
}
//// [index.js]
// esm format file
@@ -55,7 +55,7 @@ node_modules/inner/index.d.ts(2,13): error TS2303: Circular definition of import
{
"name": "package",
"private": true,
"type": "module",
"type": "module"
}
==== node_modules/inner/package.json (0 errors) ====
{
@@ -66,4 +66,5 @@ node_modules/inner/index.d.ts(2,13): error TS2303: Circular definition of import
"./mjs/*": "./*.mjs",
"./js/*": "./*.js"
}
}
}
@@ -52,7 +52,7 @@ export { type };
{
"name": "package",
"private": true,
"type": "module",
"type": "module"
}
//// [package.json]
{
@@ -63,7 +63,8 @@ export { type };
"./mjs/*": "./*.mjs",
"./js/*": "./*.js"
}
}
}
//// [index.js]
// esm format file
@@ -64,7 +64,7 @@ node_modules/inner/index.d.ts(3,22): error TS1479: The current file is a CommonJ
{
"name": "package",
"private": true,
"type": "module",
"type": "module"
}
==== node_modules/inner/package.json (0 errors) ====
{
@@ -75,4 +75,5 @@ node_modules/inner/index.d.ts(3,22): error TS1479: The current file is a CommonJ
"./mjs/*.mjs": "./*.mjs",
"./js/*.js": "./*.js"
}
}
}
@@ -52,7 +52,7 @@ export { type };
{
"name": "package",
"private": true,
"type": "module",
"type": "module"
}
//// [package.json]
{
@@ -63,7 +63,8 @@ export { type };
"./mjs/*.mjs": "./*.mjs",
"./js/*.js": "./*.js"
}
}
}
//// [index.js]
// esm format file
@@ -64,7 +64,7 @@ node_modules/inner/index.d.ts(3,22): error TS1479: The current file is a CommonJ
{
"name": "package",
"private": true,
"type": "module",
"type": "module"
}
==== node_modules/inner/package.json (0 errors) ====
{
@@ -75,4 +75,5 @@ node_modules/inner/index.d.ts(3,22): error TS1479: The current file is a CommonJ
"./mjs/*.mjs": "./*.mjs",
"./js/*.js": "./*.js"
}
}
}
@@ -52,7 +52,7 @@ export { type };
{
"name": "package",
"private": true,
"type": "module",
"type": "module"
}
//// [package.json]
{
@@ -63,7 +63,8 @@ export { type };
"./mjs/*.mjs": "./*.mjs",
"./js/*.js": "./*.js"
}
}
}
//// [index.js]
// esm format file
@@ -55,7 +55,7 @@ node_modules/inner/index.d.ts(2,13): error TS2303: Circular definition of import
{
"name": "package",
"private": true,
"type": "module",
"type": "module"
}
==== node_modules/inner/package.json (0 errors) ====
{
@@ -66,4 +66,5 @@ node_modules/inner/index.d.ts(2,13): error TS2303: Circular definition of import
"./mjs/*.mjs": "./*.mjs",
"./js/*.js": "./*.js"
}
}
}
@@ -52,7 +52,7 @@ export { type };
{
"name": "package",
"private": true,
"type": "module",
"type": "module"
}
//// [package.json]
{
@@ -63,7 +63,8 @@ export { type };
"./mjs/*.mjs": "./*.mjs",
"./js/*.js": "./*.js"
}
}
}
//// [index.js]
// esm format file
@@ -34,7 +34,7 @@ export const futureVersionApplied = true;
{
"name": "package",
"private": true,
"type": "module",
"type": "module"
}
//// [package.json]
{
@@ -47,9 +47,10 @@ export const futureVersionApplied = true;
"types": "./old-types.d.ts",
"import": "./index.mjs",
"node": "./index.js"
},
}
}
}
}
//// [index.js]
// esm format file
@@ -34,7 +34,7 @@ export const futureVersionApplied = true;
{
"name": "package",
"private": true,
"type": "module",
"type": "module"
}
//// [package.json]
{
@@ -47,9 +47,10 @@ export const futureVersionApplied = true;
"types": "./old-types.d.ts",
"import": "./index.mjs",
"node": "./index.js"
},
}
}
}
}
//// [index.js]
// esm format file
@@ -34,7 +34,7 @@ export const futureVersionApplied = true;
{
"name": "package",
"private": true,
"type": "module",
"type": "module"
}
//// [package.json]
{
@@ -47,9 +47,10 @@ export const futureVersionApplied = true;
"types": "./old-types.d.ts",
"import": "./index.mjs",
"node": "./index.js"
},
}
}
}
}
//// [index.js]
// esm format file
@@ -7,7 +7,7 @@ export const a = <div></div>;
"name": "@types/react",
"version": "0.0.1",
"main": "",
"types": "index.d.ts",
"types": "index.d.ts"
}
//// [index.d.ts]
declare namespace JSX {
@@ -8,7 +8,7 @@ export const a = <div></div>;
"name": "@types/react",
"version": "0.0.1",
"main": "",
"types": "index.d.ts",
"types": "index.d.ts"
}
// @filename: node_modules/@types/react/index.d.ts
declare namespace JSX {
@@ -15,7 +15,7 @@
".": {
"import": "./dist/index.mjs",
"require": "./dist/index.js",
"types": "./dist/index.d.ts",
"types": "./dist/index.d.ts"
}
}
}
@@ -31,4 +31,4 @@ import {} from "dep";
// Should be an untyped resolution to dep/dist/index.mjs,
// but the first search is only for TS files, and when
// there's no dist/index.d.mts, it continues looking for
// matching conditions and resolves via `types`.
// matching conditions and resolves via `types`.
@@ -4,6 +4,14 @@
// @noEmit: true
// @traceResolution: true
// The correct behavior for this test would be for both the module augmentation
// and the import to resolve to `walk.mjs` (triggering at least one implicit any
// error, I think?). However, https://github.com/microsoft/TypeScript/issues/50762
// causes the import to resolve through the `default` condition, replacing `.js`
// with `.d.ts` to find `walk.d.ts`. While this is incorrect, it's important that
// the module augmentation, which resolves through self-name resolution, resolves
// to the same module as the external import.
// @Filename: /node_modules/acorn-walk/package.json
{
"name": "acorn-walk",
@@ -23,14 +31,6 @@
}
}
// The correct behavior for this test would be for both the module augmentation
// and the import to resolve to `walk.mjs` (triggering at least one implicit any
// error, I think?). However, https://github.com/microsoft/TypeScript/issues/50762
// causes the import to resolve through the `default` condition, replacing `.js`
// with `.d.ts` to find `walk.d.ts`. While this is incorrect, it's important that
// the module augmentation, which resolves through self-name resolution, resolves
// to the same module as the external import.
// @Filename: /node_modules/acorn-walk/dist/walk.d.ts
export {};
declare module 'acorn-walk' {
@@ -115,7 +115,7 @@ export const cjsSource = true;
"./types": {
"types": {
"import": "./index.d.mts",
"require": "./index.d.cts",
"require": "./index.d.cts"
},
"node": {
"import": "./index.mjs",
@@ -123,4 +123,4 @@ export const cjsSource = true;
}
}
}
}
}
@@ -55,7 +55,7 @@ export { type };
{
"name": "package",
"private": true,
"type": "module",
"type": "module"
}
// @filename: node_modules/inner/package.json
{
@@ -66,4 +66,4 @@ export { type };
"./mjs/*": "./*.mjs",
"./js/*": "./*.js"
}
}
}
@@ -55,7 +55,7 @@ export { type };
{
"name": "package",
"private": true,
"type": "module",
"type": "module"
}
// @filename: node_modules/inner/package.json
{
@@ -66,4 +66,4 @@ export { type };
"./mjs/*.mjs": "./*.mjs",
"./js/*.js": "./*.js"
}
}
}
@@ -113,7 +113,7 @@ export const cjsSource = true;
"./types": {
"types": {
"import": "./index.d.mts",
"require": "./index.d.cts",
"require": "./index.d.cts"
},
"node": {
"import": "./index.mjs",
@@ -121,4 +121,4 @@ export const cjsSource = true;
}
}
}
}
}
@@ -53,7 +53,7 @@ export { type };
{
"name": "package",
"private": true,
"type": "module",
"type": "module"
}
// @filename: node_modules/inner/package.json
{
@@ -64,4 +64,4 @@ export { type };
"./mjs/*": "./*.mjs",
"./js/*": "./*.js"
}
}
}
@@ -54,7 +54,7 @@ export { type };
{
"name": "package",
"private": true,
"type": "module",
"type": "module"
}
// @filename: node_modules/inner/package.json
{
@@ -65,4 +65,4 @@ export { type };
"./mjs/*.mjs": "./*.mjs",
"./js/*.js": "./*.js"
}
}
}
@@ -35,7 +35,7 @@ export const futureVersionApplied = true;
{
"name": "package",
"private": true,
"type": "module",
"type": "module"
}
// @filename: node_modules/inner/package.json
{
@@ -48,6 +48,6 @@ export const futureVersionApplied = true;
"types": "./old-types.d.ts",
"import": "./index.mjs",
"node": "./index.js"
},
}
}
}
}
@@ -9,7 +9,7 @@
//// "./only-for-node": {
//// "node": "./something.js"
//// },
//// "./for-everywhere": "./other.js",
//// "./for-everywhere": "./other.js"
//// }
//// }
@@ -9,7 +9,7 @@
//// "exports": {
//// "./only-with-custom-conditions": {
//// "custom-condition": "./something.js"
//// },
//// }
//// }
//// }
@@ -9,7 +9,7 @@
//// "#only-for-node": {
//// "node": "./something.js"
//// },
//// "#for-everywhere": "./other.js",
//// "#for-everywhere": "./other.js"
//// }
//// }
@@ -9,7 +9,7 @@
//// "imports": {
//// "#only-with-custom-conditions": {
//// "custom-condition": "./something.js"
//// },
//// }
//// }
//// }