* 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)
| 4978 | * @returns {ParserState} the parser state |
| 4979 | */ |
| 4980 | parse(source, state) { |
| 4981 | if (source === null) { |
| 4982 | throw new Error("source must not be null"); |
| 4983 | } |
| 4984 | |
| 4985 | if (Buffer.isBuffer(source)) { |
| 4986 | source = source.toString("utf8"); |
| 4987 | // Keep `state.source` as a string so downstream walkers can read |
| 4988 | // the original text without re-decoding the Buffer on every use. |
| 4989 | state.source = source; |
| 4990 | } |
| 4991 | |
| 4992 | let ast; |
| 4993 | /** @type {Comment[]} */ |
| 4994 | let comments; |
| 4995 | /** @type {Set<number>} */ |
| 4996 | let semicolons; |
| 4997 | |
| 4998 | if (typeof source === "object") { |
| 4999 | semicolons = new Set(); |
| 5000 | |
| 5001 | ast = /** @type {Program} */ (source); |
| 5002 | comments = source.comments; |
| 5003 | if (source.semicolons) { |
| 5004 | // Forward semicolon information from the preparsed AST if present |
| 5005 | // This ensures the output is consistent with that of a fresh AST |
| 5006 | for (const pos of source.semicolons) { |
| 5007 | semicolons.add(pos); |
| 5008 | } |
| 5009 | } |
| 5010 | } else { |
| 5011 | ({ ast, comments, semicolons } = JavascriptParser._parse( |
| 5012 | source, |
| 5013 | { |
| 5014 | sourceType: this.sourceType, |
| 5015 | locations: true, |
| 5016 | ranges: true, |
| 5017 | comments: true, |
| 5018 | semicolons: true |
| 5019 | }, |
| 5020 | this.options.parse |
| 5021 | )); |
| 5022 | } |
| 5023 | |
| 5024 | const oldScope = this.scope; |
| 5025 | const oldState = this.state; |
| 5026 | const oldComments = this.comments; |
| 5027 | const oldSemicolons = this.semicolons; |
| 5028 | const oldStatementPath = this.statementPath; |
| 5029 | const oldPrevStatement = this.prevStatement; |
| 5030 | this.scope = { |
| 5031 | topLevelScope: true, |
| 5032 | inTry: false, |
| 5033 | inShorthand: false, |
| 5034 | inTaggedTemplateTag: false, |
| 5035 | isStrict: false, |
| 5036 | isAsmJs: false, |
| 5037 | terminated: undefined, |
no test coverage detected