Skip to content

Commit 5a0aff9

Browse files
Merge pull request #52 from UmamiAppearance/irregular-import-statements---quick-fix
Irregular import statements - quick fix
2 parents e860fa2 + 8e643b5 commit 5a0aff9

File tree

5 files changed

+113
-147
lines changed

5 files changed

+113
-147
lines changed

cjs/import-manager.cjs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,14 +388,15 @@ class ImportManagerUnitMethods {
388388
* It handles code analysis, creates units from import
389389
* statements, attaches methods to the units and more.
390390
*
391-
* @version 0.4.2
391+
* @version 0.4.3
392392
* @author UmamiAppearance [mail@umamiappearance.eu]
393393
* @license MIT
394394
* @see https://github.com/UmamiAppearance/rollup-plugin-import-manager
395395
*/
396396

397397

398398

399+
399400
class ImportManager {
400401

401402
/**
@@ -497,6 +498,11 @@ class ImportManager {
497498

498499
if (node.type === "ImportDeclaration") {
499500
const unit = this.#es6NodeToUnit(node);
501+
if (!unit) {
502+
this.#unitCreationFailedWarning(node);
503+
return;
504+
}
505+
500506
unit.id = es6Id ++;
501507
unit.index = es6Index ++;
502508
unit.hash = this.#makeHash(unit);
@@ -511,6 +517,11 @@ class ImportManager {
511517

512518
if (part.type === "ImportExpression") {
513519
const unit = this.#dynamicNodeToUnit(node, part);
520+
if (!unit) {
521+
this.#unitCreationFailedWarning(node);
522+
return;
523+
}
524+
514525
unit.id = dynamicId ++;
515526
unit.index = dynamicIndex ++;
516527
unit.hash = this.#makeHash(unit);
@@ -520,6 +531,11 @@ class ImportManager {
520531

521532
else if (part.type === "Identifier" && part.name === "require") {
522533
const unit = this.#cjsNodeToUnit(node);
534+
if (!unit) {
535+
this.#unitCreationFailedWarning(node);
536+
return;
537+
}
538+
523539
unit.id = cjsId ++;
524540
unit.index = cjsIndex ++;
525541
unit.hash = this.#makeHash(unit);
@@ -600,6 +616,7 @@ class ImportManager {
600616
* @returns {object} - Import Manager Unit Object.
601617
*/
602618
#es6NodeToUnit(node, oStart, oEnd) {
619+
if (!node) return;
603620

604621
let code;
605622
if (typeof node === "string") {
@@ -736,6 +753,7 @@ class ImportManager {
736753
* @returns {object} - Import Manager Unit Object.
737754
*/
738755
#dynamicNodeToUnit(node, importObject) {
756+
if (!node) return;
739757

740758
const code = this.code.slice(node.start, node.end);
741759

@@ -772,6 +790,7 @@ class ImportManager {
772790
* @returns {object} - Import Manager Unit Object.
773791
*/
774792
#cjsNodeToUnit(node) {
793+
if (!node || !node.declarations) return;
775794

776795
const code = this.code.slice(node.start, node.end);
777796

@@ -1259,6 +1278,13 @@ class ImportManager {
12591278
this.warnSpamProtection.add(hash);
12601279
this.warn(msg);
12611280
}
1281+
1282+
1283+
#unitCreationFailedWarning(node) {
1284+
const codeSnippet = this.code.slice(node.start, node.end);
1285+
const message = `Could not create a unit from code snippet:${os.EOL}---${os.EOL}${codeSnippet}${os.EOL}---${os.EOL}If the related code is correct, this might be a bug. You can report this on:${os.EOL}https://github.com/UmamiAppearance/ImportManager/issues${os.EOL}`;
1286+
this.warn(message);
1287+
}
12621288
}
12631289

12641290

cjs/import-manager.cjs.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)