Skip to content

Commit 2e23e67

Browse files
feat: more types for JSON type
1 parent b71c780 commit 2e23e67

4 files changed

Lines changed: 41 additions & 4 deletions

File tree

lib/json/JsonData.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,19 @@
77

88
const { register } = require("../util/serialization");
99

10+
/** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
11+
/** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
12+
/** @typedef {import("../util/Hash")} Hash */
13+
/** @typedef {import("./JsonModulesPlugin").RawJsonData} RawJsonData */
14+
1015
class JsonData {
16+
/**
17+
* @param {Buffer | RawJsonData} data JSON data
18+
*/
1119
constructor(data) {
20+
/** @type {Buffer | undefined} */
1221
this._buffer = undefined;
22+
/** @type {RawJsonData | undefined} */
1323
this._data = undefined;
1424
if (Buffer.isBuffer(data)) {
1525
this._buffer = data;
@@ -18,13 +28,20 @@ class JsonData {
1828
}
1929
}
2030

31+
/**
32+
* @returns {RawJsonData|undefined} Raw JSON data
33+
*/
2134
get() {
2235
if (this._data === undefined && this._buffer !== undefined) {
2336
this._data = JSON.parse(this._buffer.toString());
2437
}
2538
return this._data;
2639
}
2740

41+
/**
42+
* @param {Hash} hash hash to be updated
43+
* @returns {Hash} the updated hash
44+
*/
2845
updateHash(hash) {
2946
if (this._buffer === undefined && this._data !== undefined) {
3047
this._buffer = Buffer.from(JSON.stringify(this._data));
@@ -35,12 +52,20 @@ class JsonData {
3552
}
3653

3754
register(JsonData, "webpack/lib/json/JsonData", null, {
55+
/**
56+
* @param {JsonData} obj JSONData object
57+
* @param {ObjectSerializerContext} context context
58+
*/
3859
serialize(obj, { write }) {
3960
if (obj._buffer === undefined && obj._data !== undefined) {
4061
obj._buffer = Buffer.from(JSON.stringify(obj._data));
4162
}
4263
write(obj._buffer);
4364
},
65+
/**
66+
* @param {ObjectDeserializerContext} context context
67+
* @returns {JsonData} deserialized JSON data
68+
*/
4469
deserialize({ read }) {
4570
return new JsonData(read());
4671
}

lib/json/JsonGenerator.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@ const RuntimeGlobals = require("../RuntimeGlobals");
1717
/** @typedef {import("../Module").ConcatenationBailoutReasonContext} ConcatenationBailoutReasonContext */
1818
/** @typedef {import("../NormalModule")} NormalModule */
1919
/** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
20+
/** @typedef {import("./JsonData")} JsonData */
21+
/** @typedef {import("./JsonModulesPlugin").RawJsonData} RawJsonData */
2022

23+
/**
24+
* @param {RawJsonData} data Raw JSON data
25+
* @returns {undefined|string} stringified data
26+
*/
2127
const stringifySafe = data => {
2228
const stringified = JSON.stringify(data);
2329
if (!stringified) {
@@ -30,10 +36,10 @@ const stringifySafe = data => {
3036
};
3137

3238
/**
33-
* @param {Object} data data (always an object or array)
39+
* @param {RawJsonData} data Raw JSON data (always an object or array)
3440
* @param {ExportsInfo} exportsInfo exports info
3541
* @param {RuntimeSpec} runtime the runtime
36-
* @returns {Object} reduced data
42+
* @returns {RawJsonData} reduced data
3743
*/
3844
const createObjectForExportsInfo = (data, exportsInfo, runtime) => {
3945
if (exportsInfo.otherExportsInfo.getUsed(runtime) !== UsageState.Unused)
@@ -116,7 +122,8 @@ class JsonGenerator extends Generator {
116122
* @returns {number} estimate size of the module
117123
*/
118124
getSize(module, type) {
119-
let data =
125+
/** @type {RawJsonData | undefined} */
126+
const data =
120127
module.buildInfo &&
121128
module.buildInfo.jsonData &&
122129
module.buildInfo.jsonData.get();
@@ -148,6 +155,7 @@ class JsonGenerator extends Generator {
148155
concatenationScope
149156
}
150157
) {
158+
/** @type {RawJsonData | undefined} */
151159
const data =
152160
module.buildInfo &&
153161
module.buildInfo.jsonData &&
@@ -160,6 +168,7 @@ class JsonGenerator extends Generator {
160168
);
161169
}
162170
const exportsInfo = moduleGraph.getExportsInfo(module);
171+
/** @type {RawJsonData} */
163172
let finalJson =
164173
typeof data === "object" &&
165174
data &&
@@ -172,6 +181,7 @@ class JsonGenerator extends Generator {
172181
jsonStr.length > 20 && typeof finalJson === "object"
173182
? `JSON.parse('${jsonStr.replace(/[\\']/g, "\\$&")}')`
174183
: jsonStr;
184+
/** @type {string} */
175185
let content;
176186
if (concatenationScope) {
177187
content = `${runtimeTemplate.supportsConst() ? "const" : "var"} ${

lib/json/JsonModulesPlugin.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const JsonGenerator = require("./JsonGenerator");
1111
const JsonParser = require("./JsonParser");
1212

1313
/** @typedef {import("../Compiler")} Compiler */
14+
/** @typedef {Record<string, any>} RawJsonData */
1415

1516
const validate = createSchemaValidation(
1617
require("../../schemas/plugins/JsonModulesPluginParser.check.js"),

lib/json/JsonParser.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const JsonData = require("./JsonData");
1313
/** @typedef {import("../../declarations/plugins/JsonModulesPluginParser").JsonModulesPluginParserOptions} JsonModulesPluginParserOptions */
1414
/** @typedef {import("../Parser").ParserState} ParserState */
1515
/** @typedef {import("../Parser").PreparsedAst} PreparsedAst */
16+
/** @typedef {import("./JsonModulesPlugin").RawJsonData} RawJsonData */
1617

1718
class JsonParser extends Parser {
1819
/**
@@ -36,7 +37,7 @@ class JsonParser extends Parser {
3637
/** @type {JsonModulesPluginParserOptions["parse"]} */
3738
const parseFn =
3839
typeof this.options.parse === "function" ? this.options.parse : parseJson;
39-
40+
/** @type {Buffer | RawJsonData} */
4041
const data =
4142
typeof source === "object"
4243
? source

0 commit comments

Comments
 (0)