deps: update to cjs-module-lexer@0.5.2

PR-URL: https://github.com/nodejs/node/pull/35901
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
This commit is contained in:
Guy Bedford 2020-10-31 09:07:31 -07:00
parent 7ebae13e13
commit 5735525404
8 changed files with 214 additions and 69 deletions

View File

@ -1,3 +1,9 @@
0.5.2
- Support named getter functions (https://github.com/guybedford/cjs-module-lexer/pull/26)
0.5.1:
- Feature: Implement specific reexport getter forms (https://github.com/guybedford/cjs-module-lexer/pull/25)
0.5.0
- Breaking Change: No longer emit Object.defineProperty exports (https://github.com/guybedford/cjs-module-lexer/pull/24)
- Doc: Update link to WASI SDK (https://github.com/guybedford/cjs-module-lexer/pull/19)

View File

@ -70,31 +70,35 @@ STRING_LITERAL: A `"` or `'` bounded ECMA-262 string literal.
IDENTIFIER_STRING: ( `"` IDENTIFIER `"` | `'` IDENTIFIER `'` )
COMMENT_SPACE: Any ECMA-262 whitespace, ECMA-262 block comment or ECMA-262 line comment
MODULE_EXPORTS: `module` COMMENT_SPACE `.` COMMENT_SPACE `exports`
MODULE_EXPORTS: `module` `.` `exports`
EXPORTS_IDENTIFIER: MODULE_EXPORTS_IDENTIFIER | `exports`
EXPORTS_DOT_ASSIGN: EXPORTS_IDENTIFIER COMMENT_SPACE `.` COMMENT_SPACE IDENTIFIER COMMENT_SPACE `=`
EXPORTS_DOT_ASSIGN: EXPORTS_IDENTIFIER `.` IDENTIFIER `=`
EXPORTS_LITERAL_COMPUTED_ASSIGN: EXPORTS_IDENTIFIER COMMENT_SPACE `[` COMMENT_SPACE IDENTIFIER_STRING COMMENT_SPACE `]` COMMENT_SPACE `=`
EXPORTS_LITERAL_COMPUTED_ASSIGN: EXPORTS_IDENTIFIER `[` IDENTIFIER_STRING `]` `=`
EXPORTS_LITERAL_PROP: (IDENTIFIER (COMMENT_SPACE `:` COMMENT_SPACE IDENTIFIER)?) | (IDENTIFIER_STRING COMMENT_SPACE `:` COMMENT_SPACE IDENTIFIER)
EXPORTS_LITERAL_PROP: (IDENTIFIER `:` IDENTIFIER)?) | (IDENTIFIER_STRING `:` IDENTIFIER)
EXPORTS_SPREAD: `...` COMMENT_SPACE (IDENTIFIER | REQUIRE)
EXPORTS_SPREAD: `...` (IDENTIFIER | REQUIRE)
EXPORTS_MEMBER: EXPORTS_DOT_ASSIGN | EXPORTS_LITERAL_COMPUTED_ASSIGN
ES_MODULE_DEFINE: `Object` COMMENT_SPACE `.` COMMENT_SPACE `defineProperty COMMENT_SPACE `(` COMMENT_SPACE `__esModule` COMMENT_SPACE `,` COMMENT_SPACE IDENTIFIER_STRING
EXPORTS_DEFINE: `Object` `.` `defineProperty `(` IDENTIFIER_STRING `, {`
(`enumerable: true,`)?
(
`value:` |
`get` (`: function` IDENTIFIER? )? `()` {` return IDENTIFIER (`.` IDENTIFIER | `[` IDENTIFIER_STRING `]`)? `;`? `}`
)
`})`
EXPORTS_LITERAL: MODULE_EXPORTS COMMENT_SPACE `=` COMMENT_SPACE `{` COMMENT_SPACE (EXPORTS_LITERAL_PROP | EXPORTS_SPREAD) COMMENT_SPACE `,` COMMENT_SPACE)+ `}`
EXPORTS_LITERAL: MODULE_EXPORTS `=` `{` (EXPORTS_LITERAL_PROP | EXPORTS_SPREAD) `,`)+ `}`
REQUIRE: `require` COMMENT_SPACE `(` COMMENT_SPACE STRING_LITERAL COMMENT_SPACE `)`
REQUIRE: `require` `(` STRING_LITERAL `)`
EXPORTS_ASSIGN: (`var` | `const` | `let`) IDENTIFIER `=` REQUIRE
MODULE_EXPORTS_ASSIGN: MODULE_EXPORTS COMMENT_SPACE `=` COMMENT_SPACE REQUIRE
MODULE_EXPORTS_ASSIGN: MODULE_EXPORTS `=` REQUIRE
EXPORT_STAR: (`__export` | `__exportStar`) `(` REQUIRE
@ -113,9 +117,9 @@ EXPORT_STAR_LIB: `Object.keys(` IDENTIFIER$1 `).forEach(function (` IDENTIFIER$2
`})`
```
* The returned export names are taken to be the combination of:
1. `IDENTIFIER` and `IDENTIFIER_STRING` slots for all `EXPORTS_MEMBER` and `EXPORTS_LITERAL` matches.
2. `__esModule` if there is an `ES_MODULE_DEFINE` match.
Spacing between tokens is taken to be any ECMA-262 whitespace, ECMA-262 block comment or ECMA-262 line comment.
* 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.
* The reexport specifiers are taken to be the the combination of:
1. The `REQUIRE` matches of the last matched of either `MODULE_EXPORTS_ASSIGN` or `EXPORTS_LITERAL`.
2. All _top-level_ `EXPORT_STAR` `REQUIRE` matches and `EXPORTS_ASSIGN` matches whose `IDENTIFIER` also matches the first `IDENTIFIER` in `EXPORT_STAR_LIB`.
@ -156,17 +160,66 @@ It will in turn underclassify in cases where the identifiers are renamed:
})(exports);
```
#### __esModule Detection
In addition, `__esModule` is detected as an export when set by `Object.defineProperty`:
`Object.defineProperty` is detected for specifically value and getter forms returning an identifier or member expression:
```js
// DETECTS: __esModule
Object.defineProperty(exports, 'a', { value: 'a' });
// DETECTS: a, b, c, d, __esModule
Object.defineProperty(exports, 'a', {
enumerable: true,
get: function () {
return q.p;
}
});
Object.defineProperty(exports, 'b', {
enumerable: true,
get: function () {
return q['p'];
}
});
Object.defineProperty(exports, 'c', {
enumerable: true,
get () {
return b;
}
});
Object.defineProperty(exports, 'd', { value: 'd' });
Object.defineProperty(exports, '__esModule', { value: true });
```
No other named exports are detected for `defineProperty` calls in order not to trigger getters or non-enumerable properties unnecessarily.
Alternative object definition structures or getter function bodies are not detected:
```js
// DETECTS: NO EXPORTS
Object.defineProperty(exports, 'a', {
enumerable: false,
get () {
return p;
}
});
Object.defineProperty(exports, 'b', {
configurable: true,
get () {
return p;
}
});
Object.defineProperty(exports, 'c', {
get: () => p
});
Object.defineProperty(exports, 'd', {
enumerable: true,
get: function () {
return dynamic();
}
});
Object.defineProperty(exports, 'e', {
enumerable: true,
get () {
return 'str';
}
});
```
`Object.defineProperties` is also not supported.
#### Exports Object Assignment
@ -281,65 +334,64 @@ Current results:
JS Build:
```
--- JS Build ---
Module load time
> 2ms
> 5ms
Cold Run, All Samples
test/samples/*.js (3635 KiB)
> 311ms
> 323ms
Warm Runs (average of 25 runs)
test/samples/angular.js (1410 KiB)
> 14.76ms
> 14.84ms
test/samples/angular.min.js (303 KiB)
> 5.04ms
> 4.8ms
test/samples/d3.js (553 KiB)
> 7.12ms
> 7.84ms
test/samples/d3.min.js (250 KiB)
> 4ms
test/samples/magic-string.js (34 KiB)
> 0.84ms
> 0.72ms
test/samples/magic-string.min.js (20 KiB)
> 0.08ms
> 0.4ms
test/samples/rollup.js (698 KiB)
> 9.08ms
> 9.32ms
test/samples/rollup.min.js (367 KiB)
> 6ms
> 6.52ms
Warm Runs, All Samples (average of 25 runs)
test/samples/*.js (3635 KiB)
> 41.32ms
> 44ms
```
Wasm Build:
```
Module load time
> 10ms
> 11ms
Cold Run, All Samples
test/samples/*.js (3635 KiB)
> 47ms
> 42ms
Warm Runs (average of 25 runs)
test/samples/angular.js (1410 KiB)
> 12.96ms
> 9.92ms
test/samples/angular.min.js (303 KiB)
> 4ms
> 3.2ms
test/samples/d3.js (553 KiB)
> 6.12ms
> 5.2ms
test/samples/d3.min.js (250 KiB)
> 3.08ms
> 2.52ms
test/samples/magic-string.js (34 KiB)
> 0.32ms
> 0.16ms
test/samples/magic-string.min.js (20 KiB)
> 0ms
> 0.04ms
test/samples/rollup.js (698 KiB)
> 7.8ms
> 6.44ms
test/samples/rollup.min.js (367 KiB)
> 4.64ms
> 3.96ms
Warm Runs, All Samples (average of 25 runs)
test/samples/*.js (3635 KiB)
> 35.64ms
> 30.48ms
```
### Wasm Build Steps

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -260,30 +260,117 @@ function tryParseObjectDefineOrKeys (keys) {
pos++;
ch = commentWhitespace();
if (ch === 100/*d*/ && source.startsWith('efineProperty', pos + 1)) {
pos += 14;
revertPos = pos - 1;
ch = commentWhitespace();
if (ch !== 40/*(*/) {
pos = revertPos;
return;
}
pos++;
ch = commentWhitespace();
if (readExportsOrModuleDotExports(ch)) {
while (true) {
pos += 14;
revertPos = pos - 1;
ch = commentWhitespace();
if (ch === 44/*,*/) {
if (ch !== 40/*(*/) break;
pos++;
ch = commentWhitespace();
if (!readExportsOrModuleDotExports(ch)) break;
ch = commentWhitespace();
if (ch !== 44/*,*/) break;
pos++;
ch = commentWhitespace();
if (ch !== 39/*'*/ && ch !== 34/*"*/) break;
let quot = ch;
const exportPos = ++pos;
if (!identifier() || source.charCodeAt(pos) !== quot) break;
const expt = source.slice(exportPos, pos);
pos++;
ch = commentWhitespace();
if (ch !== 44/*,*/) break;
pos++;
ch = commentWhitespace();
if (ch !== 123/*{*/) break;
pos++;
ch = commentWhitespace();
if (ch === 101/*e*/) {
if (!source.startsWith('numerable', pos + 1)) break;
pos += 10;
ch = commentWhitespace();
if (ch !== 58/*:*/) break;
pos++;
ch = commentWhitespace();
if (ch !== 116/*t*/ || !source.startsWith('rue', pos + 1)) break;
pos += 4;
ch = commentWhitespace();
if (ch !== 44) break;
pos++;
ch = commentWhitespace();
if (ch === 39/*'*/ || ch === 34/*"*/) {
const exportPos = ++pos;
if (identifier() && source.charCodeAt(pos) === ch) {
// revert for "("
const expt = source.slice(exportPos, pos);
if (expt === '__esModule')
addExport(expt);
}
}
}
if (ch === 118/*v*/) {
if (!source.startsWith('alue', pos + 1)) break;
pos += 5;
ch = commentWhitespace();
if (ch !== 58/*:*/) break;
pos++;
addExport(expt);
break;
}
else if (ch === 103/*g*/) {
if (!source.startsWith('et', pos + 1)) break;
pos += 3;
ch = commentWhitespace();
if (ch === 58/*:*/) {
pos++;
ch = commentWhitespace();
if (ch !== 102/*f*/) break;
if (!source.startsWith('unction', pos + 1)) break;
pos += 8;
let lastPos = pos;
ch = commentWhitespace();
if (ch !== 40 && (lastPos === pos || !identifier())) break;
ch = commentWhitespace();
}
if (ch !== 40/*(*/) break;
pos++;
ch = commentWhitespace();
if (ch !== 41/*)*/) break;
pos++;
ch = commentWhitespace();
if (ch !== 123/*{*/) break;
pos++;
ch = commentWhitespace();
if (ch !== 114/*r*/) break;
if (!source.startsWith('eturn', pos + 1)) break;
pos += 6;
ch = commentWhitespace();
if (!identifier()) break;
ch = commentWhitespace();
if (ch === 46/*.*/) {
pos++;
commentWhitespace();
if (!identifier()) break;
ch = commentWhitespace();
}
else if (ch === 91/*[*/) {
pos++;
ch = commentWhitespace();
if (ch === 39/*'*/) singleQuoteString();
else if (ch === 34/*"*/) doubleQuoteString();
else break;
pos++;
ch = commentWhitespace();
if (ch !== 93/*]*/) break;
pos++;
ch = commentWhitespace();
}
if (ch === 59/*;*/) {
pos++;
ch = commentWhitespace();
}
if (ch !== 125/*}*/) break;
pos++;
ch = commentWhitespace();
if (ch !== 125/*}*/) break;
pos++;
ch = commentWhitespace();
if (ch !== 41/*)*/) break;
addExport(expt);
return;
}
break;
}
}
else if (keys && ch === 107/*k*/ && source.startsWith('eys', pos + 1)) {

View File

@ -1,6 +1,6 @@
{
"name": "cjs-module-lexer",
"version": "0.5.0",
"version": "0.5.2",
"description": "Lexes CommonJS modules, returning their named exports metadata",
"main": "lexer.js",
"exports": {

View File

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

View File

@ -3,7 +3,7 @@ import { strictEqual, deepEqual } from 'assert';
import m, { π } from './exports-cases.js';
import * as ns from './exports-cases.js';
deepEqual(Object.keys(ns), ['default', 'isObject', 'π']);
deepEqual(Object.keys(ns), ['default', 'isObject', 'z', 'π']);
strictEqual(π, 'yes');
strictEqual(typeof m.isObject, 'undefined');
strictEqual(m.π, 'yes');
@ -18,7 +18,7 @@ strictEqual(typeof m2, 'object');
strictEqual(m2.default, 'the default');
strictEqual(ns2.__esModule, true);
strictEqual(ns2.name, 'name');
deepEqual(Object.keys(ns2), ['__esModule', 'case2', 'default', 'name']);
deepEqual(Object.keys(ns2), ['__esModule', 'case2', 'default', 'name', 'pi']);
import m3, { __esModule as __esModule3, name as name3 } from './exports-cases3.js';
import * as ns3 from './exports-cases3.js';