(sourceCode, options)
| 181 | * @returns {ParseResult} the parsed result |
| 182 | */ |
| 183 | const oxcParse = (sourceCode, options) => { |
| 184 | const locationResolver = new LocationResolver(sourceCode); |
| 185 | |
| 186 | // We need only automatic semicolon insertion position, but there is no API, so let's collect all semicolons |
| 187 | // But there are rooms to improve it |
| 188 | /** @type {Semicolons} */ |
| 189 | const semicolons = options.semicolons |
| 190 | ? collectSemicolons(sourceCode) |
| 191 | : new Set(); |
| 192 | |
| 193 | const result = oxc.parseSync("file.js", sourceCode, { |
| 194 | ...options, |
| 195 | astType: "js", |
| 196 | range: true, |
| 197 | sourceType: options.sourceType === "module" ? "module" : "script", |
| 198 | // @ts-expect-error no types |
| 199 | experimentalRawTransfer: true |
| 200 | }); |
| 201 | |
| 202 | const comments = |
| 203 | /** @type {(Comment & { start: number, end: number, loc: SourceLocation })[]} */ |
| 204 | (result.comments); |
| 205 | |
| 206 | for (const comment of comments) { |
| 207 | Object.defineProperty(comment, "loc", { |
| 208 | get() { |
| 209 | const loc = locationResolver.getLocation(comment.start, comment.end); |
| 210 | return { |
| 211 | start: { |
| 212 | line: loc[0], |
| 213 | column: loc[1] |
| 214 | }, |
| 215 | end: { |
| 216 | line: loc[2], |
| 217 | column: loc[3] |
| 218 | } |
| 219 | }; |
| 220 | }, |
| 221 | configurable: true, |
| 222 | enumerable: true |
| 223 | }); |
| 224 | Object.defineProperty(comment, "range", { |
| 225 | get() { |
| 226 | return [comment.start, comment.end]; |
| 227 | }, |
| 228 | configurable: true, |
| 229 | enumerable: true |
| 230 | }); |
| 231 | } |
| 232 | |
| 233 | const ast = createLazyProxy( |
| 234 | /** @type {NodeWithRange<Program>} */ |
| 235 | (result.program), |
| 236 | locationResolver, |
| 237 | new WeakMap() |
| 238 | ); |
| 239 | return { ast, comments, semicolons }; |
| 240 | }; |
no test coverage detected