Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

module: CJS exports detection for modules with __esModule export #33416

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@ _UpgradeReport_Files/
/deps/icu-tmp
./node_modules
/android-toolchain/
/deps/cjs-module-lexer/bench
/deps/cjs-module-lexer/test
/deps/cjs-module-lexer/include-wasm
/deps/cjs-module-lexer/lib
/deps/cjs-module-lexer/Makefile
/deps/cjs-module-lexer/README.md
/deps/cjs-module-lexer/build.js
/deps/cjs-module-lexer/src/lexer.js
/deps/cjs-module-lexer/.*
guybedford marked this conversation as resolved.
Show resolved Hide resolved
# generated by gyp on Windows
/deps/openssl/openssl.props
/deps/openssl/openssl.targets
Expand Down
10 changes: 10 additions & 0 deletions deps/cjs-module-lexer/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
MIT License
-----------

Copyright (C) 2018-2020 Guy Bedford

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16 changes: 16 additions & 0 deletions deps/cjs-module-lexer/cjs-module-lexer.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
'targets': [
{
'target_name': 'cjs-module-lexer',
'type': 'static_library',
'cflags': ['-Wno-implicit-fallthrough', '-Wno-parentheses'],
'include_dirs': ['include'],
'sources': [
'src/lexer.c',
],
'direct_dependent_settings': {
'include_dirs': ['include']
},
}
]
}
102 changes: 102 additions & 0 deletions deps/cjs-module-lexer/include/cjs-module-lexer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#ifndef __CJS_MODULE_LEXER_H__
#define __CJS_MODULE_LEXER_H__

#ifdef __cplusplus
extern "C" {
#endif

#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>

struct Slice {
const uint16_t* start;
const uint16_t* end;
};
typedef struct Slice Slice;

struct StarExportBinding {
const uint16_t* specifier_start;
const uint16_t* specifier_end;
const uint16_t* id_start;
const uint16_t* id_end;
};
typedef struct StarExportBinding StarExportBinding;

void bail (uint32_t err);

bool parseCJS (uint16_t* source, uint32_t sourceLen, void (*addExport)(const uint16_t*, const uint16_t*), void (*addReexport)(const uint16_t*, const uint16_t*));
guybedford marked this conversation as resolved.
Show resolved Hide resolved

void tryBacktrackAddStarExportBinding (uint16_t* pos);
bool tryParseRequire (bool directStarExport);
guybedford marked this conversation as resolved.
Show resolved Hide resolved
void tryParseLiteralExports ();
bool readExportsOrModuleDotExports (uint16_t ch);
void tryParseModuleExportsDotAssign ();
void tryParseExportsDotAssign (bool assign);
void tryParseObjectDefineOrKeys ();
bool identifier (uint16_t ch);

void throwIfImportStatement ();
void throwIfExportStatement ();

void readImportString (const uint16_t* ss, uint16_t ch);
uint16_t readExportAs (uint16_t* startPos, uint16_t* endPos);

uint16_t commentWhitespace ();
void singleQuoteString ();
void doubleQuoteString ();
void regularExpression ();
void templateString ();
void blockComment ();
void lineComment ();

uint16_t readToWsOrPunctuator (uint16_t ch);

uint32_t fullCharCodeAtLast (uint16_t* pos);
bool isIdentifierStart (uint32_t code);
bool isIdentifierChar (uint32_t code);
int charCodeByteLen (uint32_t ch);

bool isBr (uint16_t c);
bool isBrOrWs (uint16_t c);
bool isBrOrWsOrPunctuator (uint16_t c);
bool isBrOrWsOrPunctuatorNotDot (uint16_t c);

bool str_eq2 (uint16_t* pos, uint16_t c1, uint16_t c2);
bool str_eq3 (uint16_t* pos, uint16_t c1, uint16_t c2, uint16_t c3);
bool str_eq4 (uint16_t* pos, uint16_t c1, uint16_t c2, uint16_t c3, uint16_t c4);
bool str_eq5 (uint16_t* pos, uint16_t c1, uint16_t c2, uint16_t c3, uint16_t c4, uint16_t c5);
bool str_eq6 (uint16_t* pos, uint16_t c1, uint16_t c2, uint16_t c3, uint16_t c4, uint16_t c5, uint16_t c6);
bool str_eq7 (uint16_t* pos, uint16_t c1, uint16_t c2, uint16_t c3, uint16_t c4, uint16_t c5, uint16_t c6, uint16_t c7);
bool str_eq9 (uint16_t* pos, uint16_t c1, uint16_t c2, uint16_t c3, uint16_t c4, uint16_t c5, uint16_t c6, uint16_t c7, uint16_t c8, uint16_t c9);
bool str_eq10 (uint16_t* pos, uint16_t c1, uint16_t c2, uint16_t c3, uint16_t c4, uint16_t c5, uint16_t c6, uint16_t c7, uint16_t c8, uint16_t c9, uint16_t c10);
bool str_eq13 (uint16_t* pos, uint16_t c1, uint16_t c2, uint16_t c3, uint16_t c4, uint16_t c5, uint16_t c6, uint16_t c7, uint16_t c8, uint16_t c9, uint16_t c10, uint16_t c11, uint16_t c12, uint16_t c13);
bool str_eq18 (uint16_t* pos, uint16_t c1, uint16_t c2, uint16_t c3, uint16_t c4, uint16_t c5, uint16_t c6, uint16_t c7, uint16_t c8, uint16_t c9, uint16_t c10, uint16_t c11, uint16_t c12, uint16_t c13, uint16_t c14, uint16_t c15, uint16_t c16, uint16_t c17, uint16_t c18);

bool readPrecedingKeyword2(uint16_t* pos, uint16_t c1, uint16_t c2);
bool readPrecedingKeyword3(uint16_t* pos, uint16_t c1, uint16_t c2, uint16_t c3);
bool readPrecedingKeyword4(uint16_t* pos, uint16_t c1, uint16_t c2, uint16_t c3, uint16_t c4);
bool readPrecedingKeyword5(uint16_t* pos, uint16_t c1, uint16_t c2, uint16_t c3, uint16_t c4, uint16_t c5);
bool readPrecedingKeyword6(uint16_t* pos, uint16_t c1, uint16_t c2, uint16_t c3, uint16_t c4, uint16_t c5, uint16_t c6);
bool readPrecedingKeyword7(uint16_t* pos, uint16_t c1, uint16_t c2, uint16_t c3, uint16_t c4, uint16_t c5, uint16_t c6, uint16_t c7);

bool keywordStart (uint16_t* pos);
bool isExpressionKeyword (uint16_t* pos);
bool isParenKeyword (uint16_t* pos);
bool isPunctuator (uint16_t charCode);
bool isExpressionPunctuator (uint16_t charCode);
bool isExpressionTerminator (uint16_t* pos);

void nextChar (uint16_t ch);
void nextCharSurrogate (uint16_t ch);
uint16_t readChar ();

void syntaxError ();

#ifdef __cplusplus
}
#endif

#endif /* __CJS_MODULE_LEXER_H__ */
36 changes: 36 additions & 0 deletions deps/cjs-module-lexer/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "cjs-module-lexer",
"version": "0.2.12",
"description": "Lexes CommonJS modules, returning their named exports metadata",
"main": "dist/lexer.js",
"module": "dist/lexer.mjs",
"scripts": {
"test": "NODE_OPTIONS=\"--experimental-modules\" mocha -b -u tdd test/*.cjs",
"build": "node --experimental-modules build.js && babel dist/lexer.mjs | terser -o dist/lexer.js",
"build-wasm": "make lib/lexer.wasm && node build.js",
"bench": "node --experimental-modules --expose-gc bench/index.js",
"prepublishOnly": "make optimize && npm run build",
"footprint": "make optimize && npm run build && cat dist/lexer.js | gzip -9f | wc -c"
},
"author": "Guy Bedford",
"license": "MIT",
"devDependencies": {
"@babel/cli": "^7.5.5",
"@babel/core": "^7.5.5",
"@babel/plugin-transform-modules-commonjs": "^7.5.0",
"kleur": "^2.0.2",
"mocha": "^5.2.0",
"terser": "^4.1.4"
},
"files": [
"dist"
],
"repository": {
"type": "git",
"url": "git+https://github.com/guybedford/cjs-module-lexer.git"
},
"bugs": {
"url": "https://github.com/guybedford/cjs-module-lexer/issues"
},
"homepage": "https://github.com/guybedford/cjs-module-lexer#readme"
}
Loading