deps: update to cjs-module-lexer@1.2.1

PR-URL: https://github.com/nodejs/node/pull/38450
Backport-PR-URL: https://github.com/nodejs/node/pull/39422
Reviewed-By: Bradley Farias <bradley.meck@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Jan Krems <jan.krems@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
This commit is contained in:
Guy Bedford 2021-04-28 13:11:40 +02:00 committed by Richard Lau
parent 7d5a2f9588
commit b3c698a5d8
No known key found for this signature in database
GPG Key ID: C43CEC45C17AB93C
9 changed files with 96 additions and 107 deletions

View File

@ -1,3 +1,10 @@
1.2.1
- Support Unicode escapes in strings (https://github.com/guybedford/cjs-module-lexer/pull/55)
- Filter export strings to valid surrogate pairs (https://github.com/guybedford/cjs-module-lexer/pull/56)
1.2.0
- Support for non-identifier exports (https://github.com/guybedford/cjs-module-lexer/pull/54, @nicolo-ribaudo)
1.1.1
- Better support for Babel reexport getter function forms (https://github.com/guybedford/cjs-module-lexer/issues/50)
- Support Babel interopRequireWildcard reexports patterns (https://github.com/guybedford/cjs-module-lexer/issues/52)

View File

@ -70,29 +70,27 @@ IDENTIFIER: As defined by ECMA-262, without support for identifier `\` escapes,
STRING_LITERAL: A `"` or `'` bounded ECMA-262 string literal.
IDENTIFIER_STRING: ( `"` IDENTIFIER `"` | `'` IDENTIFIER `'` )
MODULE_EXPORTS: `module` `.` `exports`
EXPORTS_IDENTIFIER: MODULE_EXPORTS_IDENTIFIER | `exports`
EXPORTS_DOT_ASSIGN: EXPORTS_IDENTIFIER `.` IDENTIFIER `=`
EXPORTS_LITERAL_COMPUTED_ASSIGN: EXPORTS_IDENTIFIER `[` IDENTIFIER_STRING `]` `=`
EXPORTS_LITERAL_COMPUTED_ASSIGN: EXPORTS_IDENTIFIER `[` STRING_LITERAL `]` `=`
EXPORTS_LITERAL_PROP: (IDENTIFIER (`:` IDENTIFIER)?) | (IDENTIFIER_STRING `:` IDENTIFIER)
EXPORTS_LITERAL_PROP: (IDENTIFIER (`:` IDENTIFIER)?) | (STRING_LITERAL `:` IDENTIFIER)
EXPORTS_SPREAD: `...` (IDENTIFIER | REQUIRE)
EXPORTS_MEMBER: EXPORTS_DOT_ASSIGN | EXPORTS_LITERAL_COMPUTED_ASSIGN
EXPORTS_DEFINE: `Object` `.` `defineProperty `(` EXPORTS_IDENFITIER `,` IDENTIFIER_STRING
EXPORTS_DEFINE: `Object` `.` `defineProperty `(` EXPORTS_IDENFITIER `,` STRING_LITERAL
EXPORTS_DEFINE_VALUE: EXPORTS_DEFINE `, {`
(`enumerable: true,`)?
(
`value:` |
`get` (`: function` IDENTIFIER? )? `() {` return IDENTIFIER (`.` IDENTIFIER | `[` IDENTIFIER_STRING `]`)? `;`? `}` `,`?
`get` (`: function` IDENTIFIER? )? `() {` return IDENTIFIER (`.` IDENTIFIER | `[` STRING_LITERAL `]`)? `;`? `}` `,`?
)
`})`
@ -127,8 +125,8 @@ EXPORT_STAR_LIB: `Object.keys(` IDENTIFIER$1 `).forEach(function (` IDENTIFIER$2
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:
1. All `IDENTIFIER` and `IDENTIFIER_STRING` slots for `EXPORTS_MEMBER` and `EXPORTS_LITERAL` matches.
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.
1. All `IDENTIFIER` and `STRING_LITERAL` slots for `EXPORTS_MEMBER` and `EXPORTS_LITERAL` matches.
2. The first `STRING_LITERAL` 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.
* The reexport specifiers are taken to be 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`.

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -37,8 +37,6 @@ const Import = 0;
const ExportAssign = 1;
const ExportStar = 2;
const strictReserved = new Set(['implements', 'interface', 'let', 'package', 'private', 'protected', 'public', 'static', 'yield', 'enum']);
function parseCJS (source, name = '@') {
resetState();
try {
@ -49,14 +47,39 @@ function parseCJS (source, name = '@') {
e.loc = pos;
throw e;
}
const result = { exports: [..._exports].filter(expt => !unsafeGetters.has(expt)), reexports: [...reexports] };
const result = { exports: [..._exports].filter(expt => expt !== undefined && !unsafeGetters.has(expt)), reexports: [...reexports].filter(reexpt => reexpt !== undefined) };
resetState();
return result;
}
function addExport (name) {
if (!strictReserved.has(name))
_exports.add(name);
function decode (str) {
if (str[0] === '"' || str[0] === '\'') {
try {
const decoded = (0, eval)(str);
// Filter to exclude non-matching UTF-16 surrogate strings
for (let i = 0; i < decoded.length; i++) {
const surrogatePrefix = decoded.charCodeAt(i) & 0xFC00;
if (surrogatePrefix < 0xD800) {
// Not a surrogate
continue;
}
else if (surrogatePrefix === 0xD800) {
// Validate surrogate pair
if ((decoded.charCodeAt(++i) & 0xFC00) !== 0xDC00)
return;
}
else {
// Out-of-range surrogate code (above 0xD800)
return;
}
}
return decoded;
}
catch {}
}
else {
return str;
}
}
function parseSource (cjsSource) {
@ -173,10 +196,8 @@ function parseSource (cjsSource) {
// TODO: <!-- XML comment support
break;
case 39/*'*/:
singleQuoteString();
break;
case 34/*"*/:
doubleQuoteString();
stringLiteral(ch);
break;
case 47/*/*/: {
const next_ch = source.charCodeAt(pos + 1);
@ -329,11 +350,9 @@ function tryParseObjectDefineOrKeys (keys) {
pos++;
ch = commentWhitespace();
if (ch !== 39/*'*/ && ch !== 34/*"*/) break;
let quot = ch;
const exportPos = ++pos;
if (!identifier() || source.charCodeAt(pos) !== quot) break;
expt = source.slice(exportPos, pos);
pos++;
const exportPos = pos;
stringLiteral(ch);
expt = source.slice(exportPos, ++pos);
ch = commentWhitespace();
if (ch !== 44/*,*/) break;
pos++;
@ -360,7 +379,7 @@ function tryParseObjectDefineOrKeys (keys) {
pos += 5;
ch = commentWhitespace();
if (ch !== 58/*:*/) break;
addExport(expt);
_exports.add(decode(expt));
pos = revertPos;
return;
}
@ -403,8 +422,7 @@ function tryParseObjectDefineOrKeys (keys) {
else if (ch === 91/*[*/) {
pos++;
ch = commentWhitespace();
if (ch === 39/*'*/) singleQuoteString();
else if (ch === 34/*"*/) doubleQuoteString();
if (ch === 39/*'*/ || ch === 34/*"*/) stringLiteral(ch);
else break;
pos++;
ch = commentWhitespace();
@ -427,13 +445,13 @@ function tryParseObjectDefineOrKeys (keys) {
pos++;
ch = commentWhitespace();
if (ch !== 41/*)*/) break;
addExport(expt);
_exports.add(decode(expt));
return;
}
break;
}
if (expt) {
unsafeGetters.add(expt);
unsafeGetters.add(decode(expt));
}
}
else if (keys && ch === 107/*k*/ && source.startsWith('eys', pos + 1)) {
@ -801,7 +819,7 @@ function tryParseObjectDefineOrKeys (keys) {
const starExportSpecifier = starExportMap[id];
if (starExportSpecifier) {
reexports.add(starExportSpecifier);
reexports.add(decode(starExportSpecifier));
pos = revertPos;
return;
}
@ -863,7 +881,7 @@ function tryParseExportsDotAssign (assign) {
const endPos = pos;
ch = commentWhitespace();
if (ch === 61/*=*/) {
addExport(source.slice(startPos, endPos));
_exports.add(decode(source.slice(startPos, endPos)));
return;
}
}
@ -874,19 +892,15 @@ function tryParseExportsDotAssign (assign) {
pos++;
ch = commentWhitespace();
if (ch === 39/*'*/ || ch === 34/*"*/) {
pos++;
const startPos = pos;
if (identifier() && source.charCodeAt(pos) === ch) {
const endPos = pos++;
ch = commentWhitespace();
if (ch !== 93/*]*/)
break;
pos++;
ch = commentWhitespace();
if (ch !== 61/*=*/)
break;
addExport(source.slice(startPos, endPos));
}
stringLiteral(ch);
const endPos = ++pos;
ch = commentWhitespace();
if (ch !== 93/*]*/) break;
pos++;
ch = commentWhitespace();
if (ch !== 61/*=*/) break;
_exports.add(decode(source.slice(startPos, endPos)));
}
break;
}
@ -921,39 +935,21 @@ function tryParseRequire (requireType) {
if (ch === 40/*(*/) {
pos++;
ch = commentWhitespace();
const reexportStart = pos + 1;
if (ch === 39/*'*/) {
singleQuoteString();
const reexportEnd = pos++;
const reexportStart = pos;
if (ch === 39/*'*/ || ch === 34/*"*/) {
stringLiteral(ch);
const reexportEnd = ++pos;
ch = commentWhitespace();
if (ch === 41/*)*/) {
switch (requireType) {
case ExportAssign:
reexports.add(source.slice(reexportStart, reexportEnd));
reexports.add(decode(source.slice(reexportStart, reexportEnd)));
return true;
case ExportStar:
reexports.add(source.slice(reexportStart, reexportEnd));
reexports.add(decode(source.slice(reexportStart, reexportEnd)));
return true;
default:
lastStarExportSpecifier = source.slice(reexportStart, reexportEnd);
return true;
}
}
}
else if (ch === 34/*"*/) {
doubleQuoteString();
const reexportEnd = pos++;
ch = commentWhitespace();
if (ch === 41/*)*/) {
switch (requireType) {
case ExportAssign:
reexports.add(source.slice(reexportStart, reexportEnd));
return true;
case ExportStar:
reexports.add(source.slice(reexportStart, reexportEnd));
return true;
default:
lastStarExportSpecifier = source.slice(reexportStart, reexportEnd);
lastStarExportSpecifier = decode(source.slice(reexportStart, reexportEnd));
return true;
}
}
@ -982,7 +978,7 @@ function tryParseLiteralExports () {
}
ch = source.charCodeAt(pos);
}
addExport(source.slice(startPos, endPos));
_exports.add(decode(source.slice(startPos, endPos)));
}
else if (ch === 46/*.*/ && source.startsWith('..', pos + 1)) {
pos += 3;
@ -996,21 +992,20 @@ function tryParseLiteralExports () {
ch = commentWhitespace();
}
else if (ch === 39/*'*/ || ch === 34/*"*/) {
const startPos = ++pos;
if (identifier() && source.charCodeAt(pos) === ch) {
const endPos = pos++;
const startPos = pos;
stringLiteral(ch);
const endPos = ++pos;
ch = commentWhitespace();
if (ch === 58/*:*/) {
pos++;
ch = commentWhitespace();
if (ch === 58/*:*/) {
pos++;
ch = commentWhitespace();
// nothing more complex than identifier expressions for now
if (!identifier()) {
pos = revertPos;
return;
}
ch = source.charCodeAt(pos);
addExport(source.slice(startPos, endPos));
// nothing more complex than identifier expressions for now
if (!identifier()) {
pos = revertPos;
return;
}
ch = source.charCodeAt(pos);
_exports.add(decode(source.slice(startPos, endPos)));
}
}
else {
@ -1248,26 +1243,10 @@ function lineComment () {
}
}
function singleQuoteString () {
function stringLiteral (quote) {
while (pos++ < end) {
let ch = source.charCodeAt(pos);
if (ch === 39/*'*/)
return;
if (ch === 92/*\*/) {
ch = source.charCodeAt(++pos);
if (ch === 13/*\r*/ && source.charCodeAt(pos + 1) === 10/*\n*/)
pos++;
}
else if (isBr(ch))
break;
}
throw new Error('Unterminated string.');
}
function doubleQuoteString () {
while (pos++ < end) {
let ch = source.charCodeAt(pos);
if (ch === 34/*"*/)
if (ch === quote)
return;
if (ch === 92/*\*/) {
ch = source.charCodeAt(++pos);

View File

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

View File

@ -1284,7 +1284,7 @@ success!
[`Uint8Array`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array
[dynamic instantiate hook]: #esm_code_dynamicinstantiate_code_hook
[`util.TextDecoder`]: util.md#util_class_util_textdecoder
[cjs-module-lexer]: https://github.com/guybedford/cjs-module-lexer/tree/1.1.1
[cjs-module-lexer]: https://github.com/guybedford/cjs-module-lexer/tree/1.2.1
[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,11 +3,14 @@ import { strictEqual, deepEqual } from 'assert';
import m, { π } from './exports-cases.js';
import * as ns from './exports-cases.js';
deepEqual(Object.keys(ns), ['default', 'isObject', 'z', ']);
deepEqual(Object.keys(ns), ['?invalid', 'default', 'invalid identifier', 'isObject', 'package', 'z', ', '\u{d83c}\u{df10}']);
strictEqual(π, 'yes');
strictEqual(typeof m.isObject, 'undefined');
strictEqual(m.π, 'yes');
strictEqual(m.z, 'yes');
strictEqual(m.package, 10);
strictEqual(m['invalid identifier'], 'yes');
strictEqual(m['?invalid'], 'yes');
import m2, { __esModule as __esModule2, name as name2 } from './exports-cases2.js';
import * as ns2 from './exports-cases2.js';

View File

@ -1,7 +1,9 @@
if (global.maybe)
module.exports = require('../is-object');
exports['invalid identifier'] = 'no';
module.exports['?invalid'] = 'no';
exports['invalid identifier'] = 'yes';
module.exports['?invalid'] = 'yes';
module.exports['π'] = 'yes';
exports.package = 10; // reserved word -> not used
exports['\u{D83C}'] = 'no';
exports['\u{D83C}\u{DF10}'] = 'yes';
exports.package = 10; // reserved word
Object.defineProperty(exports, 'z', { value: 'yes' });