Skip to content

Commit 5cb77f2

Browse files
guybedfordtargos
authored andcommitted
deps: upgrade to cjs-module-lexer@1.0.0
PR-URL: #35928 Reviewed-By: Myles Borins <myles.borins@gmail.com> Reviewed-By: Bradley Farias <bradley.meck@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
1 parent c09fdba commit 5cb77f2

File tree

8 files changed

+85
-39
lines changed

8 files changed

+85
-39
lines changed

deps/cjs-module-lexer/CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
1.0.0
2+
- Unsafe getter tracking (https://github.com/guybedford/cjs-module-lexer/pull/29)
3+
4+
0.6.0
5+
- API-only breaking change: Unify JS and Wasm interfaces (https://github.com/guybedford/cjs-module-lexer/pull/27)
6+
- Add type definitions (https://github.com/guybedford/cjs-module-lexer/pull/28)
7+
18
0.5.2
29
- Support named getter functions (https://github.com/guybedford/cjs-module-lexer/pull/26)
310

deps/cjs-module-lexer/README.md

+49-23
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ npm install cjs-module-lexer
1919
For use in CommonJS:
2020

2121
```js
22-
const parse = require('cjs-module-lexer');
22+
const { parse } = require('cjs-module-lexer');
23+
24+
// `init` return a promise for parity with the ESM API, but you do not have to call it
2325

2426
const { exports, reexports } = parse(`
2527
// named exports detection
@@ -84,7 +86,9 @@ EXPORTS_SPREAD: `...` (IDENTIFIER | REQUIRE)
8486
8587
EXPORTS_MEMBER: EXPORTS_DOT_ASSIGN | EXPORTS_LITERAL_COMPUTED_ASSIGN
8688
87-
EXPORTS_DEFINE: `Object` `.` `defineProperty `(` IDENTIFIER_STRING `, {`
89+
EXPORTS_DEFINE: `Object` `.` `defineProperty `(` EXPORTS_IDENFITIER `,` IDENTIFIER_STRING
90+
91+
EXPORTS_DEFINE_VALUE: EXPORTS_DEFINE `, {`
8892
(`enumerable: true,`)?
8993
(
9094
`value:` |
@@ -119,7 +123,9 @@ EXPORT_STAR_LIB: `Object.keys(` IDENTIFIER$1 `).forEach(function (` IDENTIFIER$2
119123

120124
Spacing between tokens is taken to be any ECMA-262 whitespace, ECMA-262 block comment or ECMA-262 line comment.
121125

122-
* The returned export names are taken to be the combination of the `IDENTIFIER` and `IDENTIFIER_STRING` slots for all `EXPORTS_MEMBER`, `EXPORTS_LITERAL` and `EXPORTS_DEFINE` matches.
126+
* The returned export names are taken to be the combination of:
127+
1. All `IDENTIFIER` and `IDENTIFIER_STRING` slots for `EXPORTS_MEMBER` and `EXPORTS_LITERAL` matches.
128+
2. The first `IDENTIFIER_STRING` slot for all `EXPORTS_DEFINE_VALUE` matches where that same string is not an `EXPORTS_DEFINE` match that is not also an `EXPORTS_DEFINE_VALUE` match.
123129
* The reexport specifiers are taken to be the the combination of:
124130
1. The `REQUIRE` matches of the last matched of either `MODULE_EXPORTS_ASSIGN` or `EXPORTS_LITERAL`.
125131
2. All _top-level_ `EXPORT_STAR` `REQUIRE` matches and `EXPORTS_ASSIGN` matches whose `IDENTIFIER` also matches the first `IDENTIFIER` in `EXPORT_STAR_LIB`.
@@ -160,6 +166,8 @@ It will in turn underclassify in cases where the identifiers are renamed:
160166
})(exports);
161167
```
162168

169+
#### Getter Exports Parsing
170+
163171
`Object.defineProperty` is detected for specifically value and getter forms returning an identifier or member expression:
164172

165173
```js
@@ -186,6 +194,24 @@ Object.defineProperty(exports, 'd', { value: 'd' });
186194
Object.defineProperty(exports, '__esModule', { value: true });
187195
```
188196

197+
To avoid matching getters that have side effects, any getter for an export name that does not support the forms above will
198+
opt-out of the getter matching:
199+
200+
```js
201+
// DETECTS: NO EXPORTS
202+
Object.defineProperty(exports, 'a', {
203+
value: 'no problem'
204+
});
205+
206+
if (false) {
207+
Object.defineProperty(module.exports, 'a', {
208+
get () {
209+
return dynamic();
210+
}
211+
})
212+
}
213+
```
214+
189215
Alternative object definition structures or getter function bodies are not detected:
190216

191217
```js
@@ -335,63 +361,63 @@ JS Build:
335361

336362
```
337363
Module load time
338-
> 5ms
364+
> 4ms
339365
Cold Run, All Samples
340366
test/samples/*.js (3635 KiB)
341-
> 323ms
367+
> 299ms
342368
343369
Warm Runs (average of 25 runs)
344370
test/samples/angular.js (1410 KiB)
345-
> 14.84ms
371+
> 13.96ms
346372
test/samples/angular.min.js (303 KiB)
347-
> 4.8ms
373+
> 4.72ms
348374
test/samples/d3.js (553 KiB)
349-
> 7.84ms
375+
> 6.76ms
350376
test/samples/d3.min.js (250 KiB)
351377
> 4ms
352378
test/samples/magic-string.js (34 KiB)
353-
> 0.72ms
379+
> 0.64ms
354380
test/samples/magic-string.min.js (20 KiB)
355-
> 0.4ms
381+
> 0ms
356382
test/samples/rollup.js (698 KiB)
357-
> 9.32ms
383+
> 8.48ms
358384
test/samples/rollup.min.js (367 KiB)
359-
> 6.52ms
385+
> 5.36ms
360386
361387
Warm Runs, All Samples (average of 25 runs)
362388
test/samples/*.js (3635 KiB)
363-
> 44ms
389+
> 40.28ms
364390
```
365391

366392
Wasm Build:
367393
```
368394
Module load time
369-
> 11ms
395+
> 10ms
370396
Cold Run, All Samples
371397
test/samples/*.js (3635 KiB)
372-
> 42ms
398+
> 43ms
373399
374400
Warm Runs (average of 25 runs)
375401
test/samples/angular.js (1410 KiB)
376-
> 9.92ms
402+
> 9.32ms
377403
test/samples/angular.min.js (303 KiB)
378-
> 3.2ms
404+
> 3.16ms
379405
test/samples/d3.js (553 KiB)
380-
> 5.2ms
406+
> 5ms
381407
test/samples/d3.min.js (250 KiB)
382-
> 2.52ms
408+
> 2.32ms
383409
test/samples/magic-string.js (34 KiB)
384410
> 0.16ms
385411
test/samples/magic-string.min.js (20 KiB)
386-
> 0.04ms
412+
> 0ms
387413
test/samples/rollup.js (698 KiB)
388-
> 6.44ms
414+
> 6.28ms
389415
test/samples/rollup.min.js (367 KiB)
390-
> 3.96ms
416+
> 3.6ms
391417
392418
Warm Runs, All Samples (average of 25 runs)
393419
test/samples/*.js (3635 KiB)
394-
> 30.48ms
420+
> 27.76ms
395421
```
396422

397423
### Wasm Build Steps

deps/cjs-module-lexer/dist/lexer.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

deps/cjs-module-lexer/dist/lexer.mjs

+2-2
Large diffs are not rendered by default.

deps/cjs-module-lexer/lexer.js

+20-9
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ let openTokenDepth,
1111
starExportMap,
1212
lastStarExportSpecifier,
1313
_exports,
14+
unsafeGetters,
1415
reexports;
1516

1617
function resetState () {
@@ -27,6 +28,7 @@ function resetState () {
2728
lastStarExportSpecifier = null;
2829

2930
_exports = new Set();
31+
unsafeGetters = new Set();
3032
reexports = new Set();
3133
}
3234

@@ -37,7 +39,7 @@ const ExportStar = 2;
3739

3840
const strictReserved = new Set(['implements', 'interface', 'let', 'package', 'private', 'protected', 'public', 'static', 'yield', 'enum']);
3941

40-
module.exports = function parseCJS (source, name = '@') {
42+
function parseCJS (source, name = '@') {
4143
resetState();
4244
try {
4345
parseSource(source);
@@ -47,7 +49,7 @@ module.exports = function parseCJS (source, name = '@') {
4749
e.loc = pos;
4850
throw e;
4951
}
50-
const result = { exports: [..._exports], reexports: [...reexports] };
52+
const result = { exports: [..._exports].filter(expt => !unsafeGetters.has(expt)), reexports: [...reexports] };
5153
resetState();
5254
return result;
5355
}
@@ -260,6 +262,7 @@ function tryParseObjectDefineOrKeys (keys) {
260262
pos++;
261263
ch = commentWhitespace();
262264
if (ch === 100/*d*/ && source.startsWith('efineProperty', pos + 1)) {
265+
let expt;
263266
while (true) {
264267
pos += 14;
265268
revertPos = pos - 1;
@@ -276,7 +279,7 @@ function tryParseObjectDefineOrKeys (keys) {
276279
let quot = ch;
277280
const exportPos = ++pos;
278281
if (!identifier() || source.charCodeAt(pos) !== quot) break;
279-
const expt = source.slice(exportPos, pos);
282+
expt = source.slice(exportPos, pos);
280283
pos++;
281284
ch = commentWhitespace();
282285
if (ch !== 44/*,*/) break;
@@ -304,9 +307,9 @@ function tryParseObjectDefineOrKeys (keys) {
304307
pos += 5;
305308
ch = commentWhitespace();
306309
if (ch !== 58/*:*/) break;
307-
pos++;
308310
addExport(expt);
309-
break;
311+
pos = revertPos;
312+
return;
310313
}
311314
else if (ch === 103/*g*/) {
312315
if (!source.startsWith('et', pos + 1)) break;
@@ -372,6 +375,9 @@ function tryParseObjectDefineOrKeys (keys) {
372375
}
373376
break;
374377
}
378+
if (expt) {
379+
unsafeGetters.add(expt);
380+
}
375381
}
376382
else if (keys && ch === 107/*k*/ && source.startsWith('eys', pos + 1)) {
377383
while (true) {
@@ -899,7 +905,7 @@ function tryParseLiteralExports () {
899905

900906
// --- Extracted from AcornJS ---
901907
//(https://github.com/acornjs/acorn/blob/master/acorn/src/identifier.js#L23
902-
//
908+
//
903909
// MIT License
904910

905911
// Copyright (C) 2012-2018 by various contributors (see AUTHORS)
@@ -1034,7 +1040,7 @@ function throwIfImportStatement () {
10341040
case 46/*.*/:
10351041
throw new Error('Unexpected import.meta in CJS module.');
10361042
return;
1037-
1043+
10381044
default:
10391045
// no space after "import" -> not an import keyword
10401046
if (pos === startPos + 6)
@@ -1203,7 +1209,7 @@ function readPrecedingKeyword (pos, match) {
12031209
}
12041210

12051211
function readPrecedingKeyword1 (pos, ch) {
1206-
return source.charCodeAt(pos) === ch && (pos === 0 || isBrOrWsOrPunctuatorNotDot(source.charCodeAt(pos - 1)));
1212+
return source.charCodeAt(pos) === ch && (pos === 0 || isBrOrWsOrPunctuatorNotDot(source.charCodeAt(pos - 1)));
12071213
}
12081214

12091215
// Detects one of case, debugger, delete, do, else, in, instanceof, new,
@@ -1274,7 +1280,7 @@ function isExpressionKeyword (pos) {
12741280
// throw
12751281
return readPrecedingKeyword(pos - 2, 'thr');
12761282
default:
1277-
return false;
1283+
return false;
12781284
}
12791285
}
12801286
return false;
@@ -1320,3 +1326,8 @@ function isExpressionTerminator (curPos) {
13201326
}
13211327
return false;
13221328
}
1329+
1330+
const initPromise = Promise.resolve();
1331+
1332+
module.exports.init = () => initPromise;
1333+
module.exports.parse = parseCJS;

deps/cjs-module-lexer/package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
{
22
"name": "cjs-module-lexer",
3-
"version": "0.5.2",
3+
"version": "1.0.0",
44
"description": "Lexes CommonJS modules, returning their named exports metadata",
55
"main": "lexer.js",
66
"exports": {
77
"import": "./dist/lexer.mjs",
88
"default": "./lexer.js"
99
},
10+
"types": "lexer.d.ts",
1011
"scripts": {
1112
"test-js": "mocha -b -u tdd test/*.js",
1213
"test-wasm": "WASM=1 mocha -b -u tdd test/*.js",
@@ -28,7 +29,8 @@
2829
"terser": "^4.1.4"
2930
},
3031
"files": [
31-
"dist"
32+
"dist",
33+
"lexer.d.ts"
3234
],
3335
"repository": {
3436
"type": "git",

doc/api/esm.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1287,7 +1287,7 @@ success!
12871287
[`transformSource` hook]: #esm_transformsource_source_context_defaulttransformsource
12881288
[`string`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String
12891289
[`util.TextDecoder`]: util.md#util_class_util_textdecoder
1290-
[cjs-module-lexer]: https://github.com/guybedford/cjs-module-lexer/tree/0.5.2
1290+
[cjs-module-lexer]: https://github.com/guybedford/cjs-module-lexer/tree/1.0.0
12911291
[special scheme]: https://url.spec.whatwg.org/#special-scheme
12921292
[the official standard format]: https://tc39.github.io/ecma262/#sec-modules
12931293
[transpiler loader example]: #esm_transpiler_loader

lib/internal/modules/esm/translators.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ const { emitWarningSync } = require('internal/process/warning');
6363
let cjsParse;
6464
async function initCJSParse() {
6565
if (typeof WebAssembly === 'undefined') {
66-
cjsParse = require('internal/deps/cjs-module-lexer/lexer');
66+
cjsParse = require('internal/deps/cjs-module-lexer/lexer').parse;
6767
} else {
6868
const { parse, init } =
6969
require('internal/deps/cjs-module-lexer/dist/lexer');

0 commit comments

Comments
 (0)