Navigation Menu

Skip to content

Commit

Permalink
deps: update to cjs-module-lexer@1.2.1
Browse files Browse the repository at this point in the history
PR-URL: #38450
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>
  • Loading branch information
guybedford authored and targos committed Jun 11, 2021
1 parent 839e8d1 commit b8c4d30
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 107 deletions.
7 changes: 7 additions & 0 deletions deps/cjs-module-lexer/CHANGELOG.md
@@ -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)
Expand Down
14 changes: 6 additions & 8 deletions deps/cjs-module-lexer/README.md
Expand Up @@ -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 `]`)? `;`? `}` `,`?
)
`})`
Expand Down Expand Up @@ -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`.
Expand Down
2 changes: 1 addition & 1 deletion deps/cjs-module-lexer/dist/lexer.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions deps/cjs-module-lexer/dist/lexer.mjs

Large diffs are not rendered by default.

159 changes: 69 additions & 90 deletions deps/cjs-module-lexer/lexer.js
Expand Up @@ -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 {
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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++;
Expand All @@ -360,7 +379,7 @@ function tryParseObjectDefineOrKeys (keys) {
pos += 5;
ch = commentWhitespace();
if (ch !== 58/*:*/) break;
addExport(expt);
_exports.add(decode(expt));
pos = revertPos;
return;
}
Expand Down Expand Up @@ -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();
Expand All @@ -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)) {
Expand Down Expand Up @@ -801,7 +819,7 @@ function tryParseObjectDefineOrKeys (keys) {

const starExportSpecifier = starExportMap[id];
if (starExportSpecifier) {
reexports.add(starExportSpecifier);
reexports.add(decode(starExportSpecifier));
pos = revertPos;
return;
}
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -921,39 +935,21 @@ function tryParseRequire (requireType) {
if (ch === 40/*(*/) {
pos++;
ch = commentWhitespace();
const reexportStart = pos + 1;
if (ch === 39/*'*/) {
singleQuoteString();
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);
return true;
}
}
}
else if (ch === 34/*"*/) {
doubleQuoteString();
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);
lastStarExportSpecifier = decode(source.slice(reexportStart, reexportEnd));
return true;
}
}
Expand Down Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -1248,26 +1243,10 @@ function lineComment () {
}
}

function singleQuoteString () {
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 () {
function stringLiteral (quote) {
while (pos++ < end) {
let ch = source.charCodeAt(pos);
if (ch === 34/*"*/)
if (ch === quote)
return;
if (ch === 92/*\*/) {
ch = source.charCodeAt(++pos);
Expand Down
2 changes: 1 addition & 1 deletion deps/cjs-module-lexer/package.json
@@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion doc/api/esm.md
Expand Up @@ -1327,7 +1327,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/1.1.1
[cjs-module-lexer]: https://github.com/guybedford/cjs-module-lexer/tree/1.2.1
[custom https loader]: #esm_https_loader
[special scheme]: https://url.spec.whatwg.org/#special-scheme
[the official standard format]: https://tc39.github.io/ecma262/#sec-modules
Expand Down
5 changes: 4 additions & 1 deletion test/fixtures/es-modules/cjs-exports.mjs
Expand Up @@ -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';
Expand Down
8 changes: 5 additions & 3 deletions test/fixtures/es-modules/exports-cases.js
@@ -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' });

0 comments on commit b8c4d30

Please sign in to comment.