Skip to content

Commit 122bbce

Browse files
authored
refactor: Improve handling of non-empty, non-manifest JSON files (#1668)
JIRA: CPOUI5FOUNDATION-1046 This PR steps over and extends #1667. The reason behind this change is that besides it's not related to `manifest.json` parsing, it follows the same pattern for handling empty JSON files and providing more context in the error message. Thus, we handle this edge case of parsing empty JSON files the same. **Note:** There are special cases where an empty JSON is expected and is handled explicitly. Those are not touched. i.e. https://github.com/UI5/cli/blob/main/packages/builder/lib/processors/jsdoc/lib/createIndexFiles.js#L66-L68
1 parent 0177275 commit 122bbce

13 files changed

Lines changed: 124 additions & 17 deletions

File tree

‎packages/builder/lib/lbt/bundle/Builder.js‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {SectionType} from "./BundleDefinition.js";
1818
import BundleWriter from "./BundleWriter.js";
1919
import {getLogger} from "@ui5/logger";
2020
import semver from "semver";
21+
import parseJSON from "../../utils/json.js";
2122
const log = getLogger("lbt:bundle:Builder");
2223

2324
const sourceMappingUrlPattern = /\/\/# sourceMappingURL=(\S+)\s*$/;
@@ -502,9 +503,9 @@ class BundleBuilder {
502503
let fileContent = stripBOM((await resource.buffer()).toString());
503504
if ( this.optimize ) {
504505
try {
505-
fileContent = JSON.stringify( JSON.parse( fileContent) );
506+
fileContent = JSON.stringify( parseJSON( fileContent, moduleName) );
506507
} catch (e) {
507-
log.verbose(`Failed to parse JSON file ${moduleName}. Ignoring error, skipping compression.`);
508+
log.verbose(`Skipping compression of module ${moduleName}.`);
508509
log.verbose(e);
509510
}
510511
}
@@ -673,6 +674,7 @@ class BundleBuilder {
673674

674675
async getSourceMapForModule({moduleName, moduleContent, resourcePath}) {
675676
let moduleSourceMap = null;
677+
let sourceMapOriginPath = null;
676678
let newModuleContent = moduleContent;
677679

678680
const sourceMapUrlMatch = moduleContent.match(sourceMappingUrlPattern);
@@ -691,6 +693,7 @@ class BundleBuilder {
691693
if (sourceMapUrl.startsWith(expectedTypeAndEncoding)) {
692694
const base64Content = sourceMapUrl.slice(expectedTypeAndEncoding.length);
693695
moduleSourceMap = Buffer.from(base64Content, "base64").toString();
696+
sourceMapOriginPath = `${moduleName} (inline source map)`;
694697
} else {
695698
log.warn(
696699
`Source map reference in module ${moduleName} is a data URI but has an unexpected` +
@@ -709,6 +712,7 @@ class BundleBuilder {
709712
try {
710713
const sourceMapResource = await this.pool.findResource(sourceMapPath);
711714
moduleSourceMap = (await sourceMapResource.buffer()).toString();
715+
sourceMapOriginPath = sourceMapPath;
712716
} catch (e) {
713717
// No input source map
714718
log.warn(`Unable to read source map for module ${moduleName}: ${e.message}`);
@@ -722,6 +726,7 @@ class BundleBuilder {
722726
try {
723727
const sourceMapResource = await this.pool.findResource(sourceMapFileCandidate);
724728
moduleSourceMap = (await sourceMapResource.buffer()).toString();
729+
sourceMapOriginPath = sourceMapFileCandidate;
725730
} catch (e) {
726731
// No input source map
727732
log.silly(`Could not find a source map for module ${moduleName}: ${e.message}`);
@@ -730,7 +735,7 @@ class BundleBuilder {
730735

731736

732737
if (moduleSourceMap) {
733-
moduleSourceMap = JSON.parse(moduleSourceMap);
738+
moduleSourceMap = parseJSON(moduleSourceMap, sourceMapOriginPath);
734739

735740
// Check for index map, which is currently not supported
736741
if (Array.isArray(moduleSourceMap.sections)) {

‎packages/builder/lib/processors/bundlers/flexChangesBundler.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {getLogger} from "@ui5/logger";
22
const log = getLogger("builder:processors:bundlers:flexChangesBundler");
33
import {createResource} from "@ui5/fs/resourceFactory";
4+
import parseJSON from "../../utils/json.js";
45

56
/**
67
* @public
@@ -170,7 +171,7 @@ export default function({resources, options: {pathPrefix, hasFlexBundleVersion},
170171

171172
return Promise.all(resources.map((resource) => {
172173
return resource.getBuffer().then((buffer) => {
173-
return JSON.parse(buffer.toString());
174+
return parseJSON(buffer.toString(), resource.getPath());
174175
});
175176
})).then((changesContent) => {
176177
const nNumberOfChanges = changesContent.length;

‎packages/builder/lib/processors/minifier.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import os from "node:os";
55
import workerpool from "workerpool";
66
import Resource from "@ui5/fs/Resource";
77
import {getLogger} from "@ui5/logger";
8+
import parseJSON from "../utils/json.js";
89
const log = getLogger("builder:processors:minifier");
910
import {setTimeout as setTimeoutPromise} from "node:timers/promises";
1011

@@ -230,7 +231,7 @@ export default async function({
230231
});
231232

232233
if (sourceMapContent) {
233-
const sourceMapJson = JSON.parse(sourceMapContent);
234+
const sourceMapJson = parseJSON(sourceMapContent, resourcePath + ".map");
234235

235236
if (sourceMapJson.sections) {
236237
// TODO 5.0

‎packages/builder/lib/tasks/utils/dotTheming.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import parseJSON from "../../utils/json.js";
2+
13
export async function updateLibraryDotTheming({resource, namespace, version, hasThemes}) {
2-
const dotTheming = JSON.parse(await resource.getString());
4+
const dotTheming = parseJSON(await resource.getString(), resource.getPath());
35

46
if (!dotTheming.sEntity) {
57
throw new Error(`Missing 'sEntity' property in ${resource.getPath()}`);

‎packages/builder/test/lib/lbt/bundle/Builder.js‎

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,10 @@ test.serial("writePreloadModule: with invalid json content", async (t) => {
6666
const result = await builder.writePreloadModule("invalid.json", undefined, invalidJsonResource);
6767

6868
t.is(verboseLogStub.callCount, 2, "called 2 times");
69-
t.is(verboseLogStub.firstCall.args[0], "Failed to parse JSON file invalid.json. Ignoring error, skipping compression.",
69+
t.is(verboseLogStub.firstCall.args[0], "Skipping compression of module invalid.json.",
7070
"first verbose log argument 0 is correct");
7171
t.true(verboseLogStub.secondCall.args[0] instanceof SyntaxError, "second verbose log with SyntaxError");
72+
t.is(verboseLogStub.secondCall.args[0].code, "ERR_INVALID_JSON", "second verbose log error has ERR_INVALID_JSON code");
7273
t.regex(verboseLogStub.secondCall.args[0].message, /JSON/, "second verbose log about incorrect JSON");
7374

7475
t.true(result, "result is true");
@@ -3288,6 +3289,75 @@ ${SOURCE_MAPPING_URL}=data:application/pony;charset=utf-8;base64,AAAA`
32883289
}, "Expected transitive source map has been generated");
32893290
});
32903291

3292+
test("getSourceMapForModule: Data URI with malformed JSON reports inline origin in error", async (t) => {
3293+
const pool = new ResourcePool();
3294+
const builder = new Builder(pool);
3295+
await t.throwsAsync(
3296+
builder.getSourceMapForModule({
3297+
moduleName: "my/test/module",
3298+
resourcePath: "/resources/my/test/module.js",
3299+
moduleContent: `// Some content
3300+
${SOURCE_MAPPING_URL}=data:application/json;charset=utf-8;base64,${Buffer.from("not json").toString("base64")}`
3301+
}),
3302+
{
3303+
instanceOf: SyntaxError,
3304+
message: /my\/test\/module \(inline source map\)/
3305+
},
3306+
"Error message references the inline source map origin"
3307+
);
3308+
});
3309+
3310+
test("getSourceMapForModule: Relative URL with malformed JSON reports resolved path in error", async (t) => {
3311+
const pool = new ResourcePool();
3312+
pool.addResource({
3313+
name: "my/test/module.js.map",
3314+
getPath: () => "my/test/module.js.map",
3315+
string: function() {
3316+
return this.buffer();
3317+
},
3318+
buffer: async () => "not json"
3319+
});
3320+
const builder = new Builder(pool);
3321+
await t.throwsAsync(
3322+
builder.getSourceMapForModule({
3323+
moduleName: "my/test/module",
3324+
resourcePath: "/resources/my/test/module.js",
3325+
moduleContent: `// Some content
3326+
${SOURCE_MAPPING_URL}=module.js.map`
3327+
}),
3328+
{
3329+
instanceOf: SyntaxError,
3330+
message: /my\/test\/module\.js\.map/
3331+
},
3332+
"Error message references the resolved source map file path"
3333+
);
3334+
});
3335+
3336+
test("getSourceMapForModule: Fallback file with malformed JSON reports candidate path in error", async (t) => {
3337+
const pool = new ResourcePool();
3338+
pool.addResource({
3339+
name: "my/test/module.js.map",
3340+
getPath: () => "my/test/module.js.map",
3341+
string: function() {
3342+
return this.buffer();
3343+
},
3344+
buffer: async () => "not json"
3345+
});
3346+
const builder = new Builder(pool);
3347+
await t.throwsAsync(
3348+
builder.getSourceMapForModule({
3349+
moduleName: "my/test/module",
3350+
resourcePath: "/resources/my/test/module.js",
3351+
moduleContent: "// Some content\n"
3352+
}),
3353+
{
3354+
instanceOf: SyntaxError,
3355+
message: /my\/test\/module\.js\.map/
3356+
},
3357+
"Error message references the fallback source map candidate path"
3358+
);
3359+
});
3360+
32913361
test("createTransientSourceMap: includeContent=false", (t) => {
32923362
const {createTransientSourceMap} = __localFunctions__;
32933363

‎packages/builder/test/lib/processors/bundlers/flexChangesBundler.js‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ test("flexChangesBundler with 2 changes", async (t) => {
9191
flexBundle.forEach((change) => {
9292
resources.push({
9393
name: "flexChange",
94+
getPath: () => "/test/change.json",
9495
getBuffer: async () => JSON.stringify(change)
9596
});
9697
});
@@ -127,6 +128,7 @@ test("includes annotation_changes in flexibility-bundle when hasFlexBundleVersio
127128

128129
const resources = [{
129130
name: "flexChange",
131+
getPath: () => "/test/change.json",
130132
getBuffer: async () => JSON.stringify(change)
131133
}];
132134

@@ -156,6 +158,7 @@ test("annotation_changes triggers error when hasFlexBundleVersion = false", asyn
156158

157159
const resources = [{
158160
name: "flexChange",
161+
getPath: () => "/test/change.json",
159162
getBuffer: async () => JSON.stringify(change)
160163
}];
161164

@@ -431,6 +434,7 @@ test("flexChangesBundler has ctrl_variant and hasFlexBundleVersion = true", asyn
431434
entities.forEach((change) => {
432435
resources.push({
433436
name: "flexChange",
437+
getPath: () => "/test/change.json",
434438
getBuffer: async () => JSON.stringify(change)
435439
});
436440
});
@@ -531,12 +535,14 @@ test("flexChangesBundler has ctrl_variant and hasFlexBundleVersion = false", asy
531535
change.forEach((change) => {
532536
resources.push({
533537
name: "flexChange",
538+
getPath: () => "/test/change.json",
534539
getBuffer: async () => JSON.stringify(change)
535540
});
536541
});
537542
ctrlVariant.forEach((change) => {
538543
resources.push({
539544
name: "flexChange",
545+
getPath: () => "/test/change.json",
540546
getBuffer: async () => JSON.stringify(change)
541547
});
542548
});
@@ -937,36 +943,42 @@ test("flexChangesBundler with existing flexibility-bundle.json", async (t) => {
937943
changeList.forEach((change) => {
938944
resources.push({
939945
name: "flexChange",
946+
getPath: () => "/test/change.json",
940947
getBuffer: async () => JSON.stringify(change)
941948
});
942949
});
943950
compVariants.forEach((change) => {
944951
resources.push({
945952
name: "flexChange",
953+
getPath: () => "/test/change.json",
946954
getBuffer: async () => JSON.stringify(change)
947955
});
948956
});
949957
variantDependentControlChange.forEach((change) => {
950958
resources.push({
951959
name: "flexChange",
960+
getPath: () => "/test/change.json",
952961
getBuffer: async () => JSON.stringify(change)
953962
});
954963
});
955964
ctrlVariantChange.forEach((change) => {
956965
resources.push({
957966
name: "flexChange",
967+
getPath: () => "/test/change.json",
958968
getBuffer: async () => JSON.stringify(change)
959969
});
960970
});
961971
ctrlVariantManagementChange.forEach((change) => {
962972
resources.push({
963973
name: "flexChange",
974+
getPath: () => "/test/change.json",
964975
getBuffer: async () => JSON.stringify(change)
965976
});
966977
});
967978
ctrlVariant.forEach((change) => {
968979
resources.push({
969980
name: "flexChange",
981+
getPath: () => "/test/change.json",
970982
getBuffer: async () => JSON.stringify(change)
971983
});
972984
});
@@ -1243,36 +1255,42 @@ test("flexChangesBundler with existing flexibility-bundle.json and missing/wrong
12431255
changeList.forEach((change) => {
12441256
resources.push({
12451257
name: "flexChange",
1258+
getPath: () => "/test/change.json",
12461259
getBuffer: async () => JSON.stringify(change)
12471260
});
12481261
});
12491262
compVariants.forEach((change) => {
12501263
resources.push({
12511264
name: "flexChange",
1265+
getPath: () => "/test/change.json",
12521266
getBuffer: async () => JSON.stringify(change)
12531267
});
12541268
});
12551269
variantDependentControlChange.forEach((change) => {
12561270
resources.push({
12571271
name: "flexChange",
1272+
getPath: () => "/test/change.json",
12581273
getBuffer: async () => JSON.stringify(change)
12591274
});
12601275
});
12611276
ctrlVariantChange.forEach((change) => {
12621277
resources.push({
12631278
name: "flexChange",
1279+
getPath: () => "/test/change.json",
12641280
getBuffer: async () => JSON.stringify(change)
12651281
});
12661282
});
12671283
ctrlVariantManagementChange.forEach((change) => {
12681284
resources.push({
12691285
name: "flexChange",
1286+
getPath: () => "/test/change.json",
12701287
getBuffer: async () => JSON.stringify(change)
12711288
});
12721289
});
12731290
ctrlVariant.forEach((change) => {
12741291
resources.push({
12751292
name: "flexChange",
1293+
getPath: () => "/test/change.json",
12761294
getBuffer: async () => JSON.stringify(change)
12771295
});
12781296
});
@@ -1396,6 +1414,7 @@ test("flexChangesBundler with existing flexibility-bundle.json and version lower
13961414
flexBundle.forEach((change) => {
13971415
resources.push({
13981416
name: "flexChange",
1417+
getPath: () => "/test/change.json",
13991418
getBuffer: async () => JSON.stringify(change)
14001419
});
14011420
});

‎packages/builder/test/lib/tasks/generateThemeDesignerResources.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ test.serial("generateThemeDesignerResources: Library sap.ui.core with existing l
252252
};
253253

254254
const coreLibraryDotThemingResource = {
255+
getPath: () => "/resources/sap/ui/core/.theming",
255256
getString: async () => JSON.stringify({
256257
sEntity: "Library",
257258
sId: "sap/ui/core",
@@ -348,6 +349,7 @@ test.serial("generateThemeDesignerResources: Library sap.ui.core without themes,
348349
const {sinon, generateThemeDesignerResources, libraryLessGeneratorStub, fsInterfaceStub, ResourceStub} = t.context;
349350

350351
const coreLibraryDotThemingResource = {
352+
getPath: () => "/resources/sap/ui/core/.theming",
351353
getString: async () => JSON.stringify({
352354
sEntity: "Library",
353355
sId: "sap/ui/core",

‎packages/project/lib/graph/Module.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import ui5YamlSchema from "./helpers/ui5YamlSchema.js";
77
import {createReader} from "@ui5/fs/resourceFactory";
88
import Specification from "../specifications/Specification.js";
99
import {validate} from "../validation/validator.js";
10+
import parseJSON from "../utils/json.js";
1011

1112
import {getLogger} from "@ui5/logger";
1213

@@ -395,7 +396,7 @@ class Module {
395396
const reader = this.getReader();
396397
const buildManifestResource = await reader.byPath("/.ui5/build-manifest.json");
397398
if (buildManifestResource) {
398-
return JSON.parse(await buildManifestResource.getString());
399+
return parseJSON(await buildManifestResource.getString(), buildManifestResource.getPath());
399400
}
400401
}
401402

‎packages/project/lib/graph/Workspace.js‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {promisify} from "node:util";
66
import {getLogger} from "@ui5/logger";
77
import Module from "./Module.js";
88
import {validateWorkspace} from "../validation/validator.js";
9+
import parseJSON from "../utils/json.js";
910

1011
const readFile = promisify(fs.readFile);
1112
const log = getLogger("graph:Workspace");
@@ -261,8 +262,9 @@ class Workspace {
261262
* @returns {object} Package json content
262263
*/
263264
async _readPackageJson(modulePath) {
264-
const content = await readFile(path.join(modulePath, "package.json"), "utf8");
265-
return JSON.parse(content);
265+
const packageJsonPath = path.join(modulePath, "package.json");
266+
const content = await readFile(packageJsonPath, "utf8");
267+
return parseJSON(content, packageJsonPath);
266268
}
267269

268270
async _validateConfig() {

0 commit comments

Comments
 (0)