Skip to content

Commit 88f1fee

Browse files
refactor: derive ASI positions from source instead of acorn callback
Stop passing onInsertedSemicolon to acorn; determine automatic-semicolon- insertion positions from the source text via getLocation's source instead. Custom parsers no longer need to collect and return a semicolons set, so the oxc example drops its collect-all-semicolons workaround.
1 parent 4968684 commit 88f1fee

13 files changed

Lines changed: 192 additions & 183 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Derive ASI positions from the source text instead of acorn's onInsertedSemicolon, so custom parsers no longer need to collect semicolons.

examples/custom-javascript-parser/README.md

Lines changed: 6 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ const acorn = require("acorn");
2727
/** @typedef {import("estree").SourceLocation} SourceLocation */
2828
/** @typedef {import("../../../lib/javascript/JavascriptParser").ParseOptions} ParseOptions */
2929
/** @typedef {import("../../../lib/javascript/JavascriptParser").ParseResult} ParseResult */
30-
/** @typedef {Set<number>} Semicolons */
3130

3231
/**
3332
* @param {string} sourceCode the source code
@@ -37,27 +36,17 @@ const acorn = require("acorn");
3736
const acornParse = (sourceCode, options) => {
3837
/** @type {(Comment & { start: number, end: number, loc: SourceLocation })[]} */
3938
const comments = [];
40-
/** @type {Semicolons} */
41-
const semicolons = new Set();
4239

4340
const ast =
4441
/** @type {import("estree").Program} */
4542
(
4643
acorn.parse(sourceCode, {
4744
...options,
48-
onComment: options.comments ? comments : undefined,
49-
onInsertedSemicolon: options.semicolons
50-
? // Set semicolons
51-
/**
52-
* @param {number} pos a position of semicolon
53-
* @returns {Semicolons} set with semicolon positions
54-
*/
55-
(pos) => semicolons.add(pos)
56-
: undefined
45+
onComment: options.comments ? comments : undefined
5746
})
5847
);
5948

60-
return { ast, comments, semicolons };
49+
return { ast, comments };
6150
};
6251

6352
module.exports = acornParse;
@@ -77,36 +66,15 @@ const oxc = require("oxc-parser");
7766
/** @typedef {import("../../../lib/javascript/JavascriptParser").ParseOptions} ParseOptions */
7867
/** @typedef {import("../../../lib/javascript/JavascriptParser").ParseResult} ParseResult */
7968

80-
/**
81-
* @param {string} sourceCode source code
82-
* @returns {Set<number>} semicolons
83-
*/
84-
const collectSemicolons = (sourceCode) => {
85-
const semiSet = new Set();
86-
let pos = sourceCode.indexOf(";");
87-
88-
while (pos !== -1) {
89-
semiSet.add(pos);
90-
pos = sourceCode.indexOf(";", pos + 1);
91-
}
92-
93-
return semiSet;
94-
};
95-
9669
/**
9770
* Oxc has no location API — none is needed: webpack derives line/column
98-
* locations from node offsets and the source text itself.
71+
* locations from node offsets and the source text itself. ASI positions are
72+
* likewise read from the source, so no semicolon collection is required.
9973
* @param {string} sourceCode the source code
10074
* @param {ParseOptions} options options
10175
* @returns {ParseResult} the parsed result
10276
*/
10377
const oxcParse = (sourceCode, options) => {
104-
// We need only automatic semicolon insertion position, but there is no API, so let's collect all semicolons
105-
// But there are rooms to improve it
106-
const semicolons = options.semicolons
107-
? collectSemicolons(sourceCode)
108-
: new Set();
109-
11078
const result = oxc.parseSync("file.js", sourceCode, {
11179
astType: "js",
11280
range: true,
@@ -126,8 +94,7 @@ const oxcParse = (sourceCode, options) => {
12694

12795
return {
12896
ast: /** @type {Program} */ (/** @type {unknown} */ (result.program)),
129-
comments,
130-
semicolons
97+
comments
13198
};
13299
};
133100

@@ -149,7 +116,6 @@ const meriyah = require("meriyah");
149116
/** @typedef {import("estree").SourceLocation} SourceLocation */
150117
/** @typedef {import("../../../lib/javascript/JavascriptParser").ParseOptions} ParseOptions */
151118
/** @typedef {import("../../../lib/javascript/JavascriptParser").ParseResult} ParseResult */
152-
/** @typedef {Set<number>} Semicolons */
153119

154120
/**
155121
* @param {string} sourceCode the source code
@@ -159,8 +125,6 @@ const meriyah = require("meriyah");
159125
const meriyahParse = (sourceCode, options) => {
160126
/** @type {(Comment & { start: number, end: number, loc: SourceLocation })[]} */
161127
const comments = [];
162-
/** @type {Semicolons} */
163-
const semicolons = new Set();
164128

165129
const ast =
166130
/** @type {import("estree").Program} */
@@ -182,19 +146,11 @@ const meriyahParse = (sourceCode, options) => {
182146
});
183147
}
184148
}
185-
: undefined,
186-
onInsertedSemicolon: options.semicolons
187-
? // Set semicolons
188-
/**
189-
* @param {number} pos a position of semicolon
190-
* @returns {Semicolons} set with semicolon positions
191-
*/
192-
(pos) => semicolons.add(pos)
193149
: undefined
194150
})
195151
);
196152

197-
return { ast, comments, semicolons };
153+
return { ast, comments };
198154
};
199155

200156
module.exports = meriyahParse;

examples/custom-javascript-parser/internals/acorn-parse.js

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ const acorn = require("acorn");
77
/** @typedef {import("estree").SourceLocation} SourceLocation */
88
/** @typedef {import("../../../lib/javascript/JavascriptParser").ParseOptions} ParseOptions */
99
/** @typedef {import("../../../lib/javascript/JavascriptParser").ParseResult} ParseResult */
10-
/** @typedef {Set<number>} Semicolons */
1110

1211
/**
1312
* @param {string} sourceCode the source code
@@ -17,27 +16,17 @@ const acorn = require("acorn");
1716
const acornParse = (sourceCode, options) => {
1817
/** @type {(Comment & { start: number, end: number, loc: SourceLocation })[]} */
1918
const comments = [];
20-
/** @type {Semicolons} */
21-
const semicolons = new Set();
2219

2320
const ast =
2421
/** @type {import("estree").Program} */
2522
(
2623
acorn.parse(sourceCode, {
2724
...options,
28-
onComment: options.comments ? comments : undefined,
29-
onInsertedSemicolon: options.semicolons
30-
? // Set semicolons
31-
/**
32-
* @param {number} pos a position of semicolon
33-
* @returns {Semicolons} set with semicolon positions
34-
*/
35-
(pos) => semicolons.add(pos)
36-
: undefined
25+
onComment: options.comments ? comments : undefined
3726
})
3827
);
3928

40-
return { ast, comments, semicolons };
29+
return { ast, comments };
4130
};
4231

4332
module.exports = acornParse;

examples/custom-javascript-parser/internals/bench.mjs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ const options = {
1313
locations: true,
1414
comments: true,
1515
allowHashBang: true,
16-
allowReturnOutsideFunction: false,
17-
semicolons: true
16+
allowReturnOutsideFunction: false
1817
};
1918

2019
const bench = new Bench({ name: "simple benchmark", time: 100 });

examples/custom-javascript-parser/internals/meriyah-parse.js

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ const meriyah = require("meriyah");
88
/** @typedef {import("estree").SourceLocation} SourceLocation */
99
/** @typedef {import("../../../lib/javascript/JavascriptParser").ParseOptions} ParseOptions */
1010
/** @typedef {import("../../../lib/javascript/JavascriptParser").ParseResult} ParseResult */
11-
/** @typedef {Set<number>} Semicolons */
1211

1312
/**
1413
* @param {string} sourceCode the source code
@@ -18,8 +17,6 @@ const meriyah = require("meriyah");
1817
const meriyahParse = (sourceCode, options) => {
1918
/** @type {(Comment & { start: number, end: number, loc: SourceLocation })[]} */
2019
const comments = [];
21-
/** @type {Semicolons} */
22-
const semicolons = new Set();
2320

2421
const ast =
2522
/** @type {import("estree").Program} */
@@ -41,19 +38,11 @@ const meriyahParse = (sourceCode, options) => {
4138
});
4239
}
4340
}
44-
: undefined,
45-
onInsertedSemicolon: options.semicolons
46-
? // Set semicolons
47-
/**
48-
* @param {number} pos a position of semicolon
49-
* @returns {Semicolons} set with semicolon positions
50-
*/
51-
(pos) => semicolons.add(pos)
5241
: undefined
5342
})
5443
);
5544

56-
return { ast, comments, semicolons };
45+
return { ast, comments };
5746
};
5847

5948
module.exports = meriyahParse;

examples/custom-javascript-parser/internals/oxc-parse.js

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,15 @@ const oxc = require("oxc-parser");
77
/** @typedef {import("../../../lib/javascript/JavascriptParser").ParseOptions} ParseOptions */
88
/** @typedef {import("../../../lib/javascript/JavascriptParser").ParseResult} ParseResult */
99

10-
/**
11-
* @param {string} sourceCode source code
12-
* @returns {Set<number>} semicolons
13-
*/
14-
const collectSemicolons = (sourceCode) => {
15-
const semiSet = new Set();
16-
let pos = sourceCode.indexOf(";");
17-
18-
while (pos !== -1) {
19-
semiSet.add(pos);
20-
pos = sourceCode.indexOf(";", pos + 1);
21-
}
22-
23-
return semiSet;
24-
};
25-
2610
/**
2711
* Oxc has no location API — none is needed: webpack derives line/column
28-
* locations from node offsets and the source text itself.
12+
* locations from node offsets and the source text itself. ASI positions are
13+
* likewise read from the source, so no semicolon collection is required.
2914
* @param {string} sourceCode the source code
3015
* @param {ParseOptions} options options
3116
* @returns {ParseResult} the parsed result
3217
*/
3318
const oxcParse = (sourceCode, options) => {
34-
// We need only automatic semicolon insertion position, but there is no API, so let's collect all semicolons
35-
// But there are rooms to improve it
36-
const semicolons = options.semicolons
37-
? collectSemicolons(sourceCode)
38-
: new Set();
39-
4019
const result = oxc.parseSync("file.js", sourceCode, {
4120
astType: "js",
4221
range: true,
@@ -56,8 +35,7 @@ const oxcParse = (sourceCode, options) => {
5635

5736
return {
5837
ast: /** @type {Program} */ (/** @type {unknown} */ (result.program)),
59-
comments,
60-
semicolons
38+
comments
6139
};
6240
};
6341

0 commit comments

Comments
 (0)
Sponsor
SponsoredKunjungi sekarang
Promo