-
-
Notifications
You must be signed in to change notification settings - Fork 9.5k
Expand file tree
/
Copy pathmeriyah-parse.js
More file actions
59 lines (53 loc) · 1.67 KB
/
Copy pathmeriyah-parse.js
File metadata and controls
59 lines (53 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
"use strict";
const meriyah = require("meriyah");
/** @typedef {import("estree").Program} Program */
/** @typedef {import("estree").Node} Node */
/** @typedef {import("estree").Comment} Comment */
/** @typedef {import("estree").SourceLocation} SourceLocation */
/** @typedef {import("../../../lib/javascript/JavascriptParser").ParseOptions} ParseOptions */
/** @typedef {import("../../../lib/javascript/JavascriptParser").ParseResult} ParseResult */
/** @typedef {Set<number>} Semicolons */
/**
* @param {string} sourceCode the source code
* @param {ParseOptions} options options
* @returns {ParseResult} the parsed result
*/
const meriyahParse = (sourceCode, options) => {
/** @type {(Comment & { start: number, end: number, loc: SourceLocation })[]} */
const comments = [];
/** @type {Semicolons} */
const semicolons = new Set();
const ast =
/** @type {import("estree").Program} */
(
meriyah.parse(sourceCode, {
...options,
module: options.sourceType === "module",
loc: options.locations,
onComment: options.comments
? (type, value, start, end, loc) => {
if (type === "SingleLine" || type === "MultiLine") {
comments.push({
type: type === "SingleLine" ? "Line" : "Block",
value,
start,
end,
range: [start, end],
loc
});
}
}
: undefined,
onInsertedSemicolon: options.semicolons
? // Set semicolons
/**
* @param {number} pos a position of semicolon
* @returns {Semicolons} set with semicolon positions
*/
(pos) => semicolons.add(pos)
: undefined
})
);
return { ast, comments, semicolons };
};
module.exports = meriyahParse;