From e152bb95c588a13ce05b0434572bcad6c54728c9 Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Sun, 18 Sep 2016 15:03:20 -0400 Subject: [PATCH] warn on missing unused imports in deshadowing phase, rather than throwing - fixes #928 --- src/ast/scopes/ModuleScope.js | 5 +++++ .../warn-on-unused-missing-imports/_config.js | 12 ++++++++++++ test/function/warn-on-unused-missing-imports/foo.js | 1 + test/function/warn-on-unused-missing-imports/main.js | 3 +++ 4 files changed, 21 insertions(+) create mode 100644 test/function/warn-on-unused-missing-imports/_config.js create mode 100644 test/function/warn-on-unused-missing-imports/foo.js create mode 100644 test/function/warn-on-unused-missing-imports/main.js diff --git a/src/ast/scopes/ModuleScope.js b/src/ast/scopes/ModuleScope.js index 051fcb4..5a18d6f 100644 --- a/src/ast/scopes/ModuleScope.js +++ b/src/ast/scopes/ModuleScope.js @@ -22,8 +22,13 @@ export default class ModuleScope extends Scope { specifier.module.getExports().forEach( name => { names.set(name); }); + if ( specifier.name !== '*' ) { const declaration = specifier.module.traceExport( specifier.name ); + if ( !declaration ) { + this.module.bundle.onwarn( `Non-existent export '${specifier.name}' is imported from ${specifier.module.id} by ${this.module.id}` ); + return; + } const name = declaration.getName( true ); if ( name !== specifier.name ) { names.set( declaration.getName( true ) ); diff --git a/test/function/warn-on-unused-missing-imports/_config.js b/test/function/warn-on-unused-missing-imports/_config.js new file mode 100644 index 0000000..16d69c1 --- /dev/null +++ b/test/function/warn-on-unused-missing-imports/_config.js @@ -0,0 +1,12 @@ +const path = require( 'path' ); +const assert = require( 'assert' ); + +module.exports = { + solo: true, + description: 'warns on missing (but unused) imports', + warnings: warnings => { + assert.deepEqual( warnings, [ + `Non-existent export 'b' is imported from ${path.resolve(__dirname, 'foo.js')} by ${path.resolve(__dirname, 'main.js')}` + ]); + } +}; diff --git a/test/function/warn-on-unused-missing-imports/foo.js b/test/function/warn-on-unused-missing-imports/foo.js new file mode 100644 index 0000000..71becd3 --- /dev/null +++ b/test/function/warn-on-unused-missing-imports/foo.js @@ -0,0 +1 @@ +export const a = 42; diff --git a/test/function/warn-on-unused-missing-imports/main.js b/test/function/warn-on-unused-missing-imports/main.js new file mode 100644 index 0000000..7eaceb9 --- /dev/null +++ b/test/function/warn-on-unused-missing-imports/main.js @@ -0,0 +1,3 @@ +import { a, b } from './foo.js'; + +assert.equal( a, 42 );