* Parses the provided source and updates the parser state. * @param {string | Buffer | PreparsedAst} source the source to parse * @param {ParserState} state the parser state * @returns {ParserState} the parser state
(source, state)
| 43 | * @returns {ParserState} the parser state |
| 44 | */ |
| 45 | parse(source, state) { |
| 46 | if (Buffer.isBuffer(source)) { |
| 47 | source = source.toString("utf8"); |
| 48 | } |
| 49 | |
| 50 | const parseFn = |
| 51 | typeof this.options.parse === "function" ? this.options.parse : parseJson; |
| 52 | /** @type {Buffer | JsonValue | undefined} */ |
| 53 | const data = |
| 54 | typeof source === "object" |
| 55 | ? source |
| 56 | : parseFn(source[0] === "\uFEFF" ? source.slice(1) : source); |
| 57 | const jsonData = new JsonData(/** @type {Buffer | JsonValue} */ (data)); |
| 58 | const buildInfo = /** @type {JsonModuleBuildInfo} */ ( |
| 59 | state.module.buildInfo |
| 60 | ); |
| 61 | buildInfo.jsonData = jsonData; |
| 62 | buildInfo.strict = true; |
| 63 | const buildMeta = /** @type {BuildMeta} */ (state.module.buildMeta); |
| 64 | buildMeta.exportsType = "default"; |
| 65 | buildMeta.defaultObject = |
| 66 | typeof data === "object" |
| 67 | ? this.options.namedExports === false |
| 68 | ? false |
| 69 | : this.options.namedExports === true |
| 70 | ? "redirect" |
| 71 | : "redirect-warn" |
| 72 | : false; |
| 73 | state.module.addDependency( |
| 74 | new JsonExportsDependency( |
| 75 | jsonData, |
| 76 | /** @type {number} */ |
| 77 | (this.options.exportsDepth) |
| 78 | ) |
| 79 | ); |
| 80 | return state; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | module.exports = JsonParser; |
no test coverage detected