Browse Source

use bundle.warn and module.warn throughout codebase

gh-786
Rich-Harris 8 years ago
parent
commit
4dba546885
  1. 30
      src/Bundle.js
  2. 16
      src/Module.js
  3. 7
      src/ast/nodes/ExportDefaultDeclaration.js
  4. 8
      src/ast/nodes/MemberExpression.js
  5. 12
      src/ast/nodes/ThisExpression.js
  6. 6
      src/ast/scopes/ModuleScope.js
  7. 2
      src/finalisers/iife.js
  8. 8
      src/finalisers/shared/getGlobalNameMaker.js
  9. 5
      src/finalisers/shared/warnOnBuiltins.js
  10. 2
      src/finalisers/umd.js
  11. 8
      src/utils/collapseSourcemaps.js
  12. 6
      src/utils/getExportMode.js
  13. 11
      test/function/assign-namespace-to-var/_config.js
  14. 12
      test/function/custom-path-resolver-async/_config.js
  15. 12
      test/function/custom-path-resolver-sync/_config.js
  16. 12
      test/function/does-not-hang-on-missing-module/_config.js
  17. 13
      test/function/double-named-export-from/_config.js
  18. 29
      test/function/empty-exports/_config.js
  19. 11
      test/function/module-tree/_config.js
  20. 24
      test/function/namespace-missing-export/_config.js
  21. 22
      test/function/unused-import/_config.js
  22. 24
      test/function/warn-on-ambiguous-function-export/_config.js
  23. 14
      test/function/warn-on-auto-named-default-exports/_config.js
  24. 13
      test/function/warn-on-empty-bundle/_config.js
  25. 6
      test/function/warn-on-eval/_config.js
  26. 9
      test/function/warn-on-namespace-conflict/_config.js
  27. 0
      test/function/warn-on-namespace-conflict/bar.js
  28. 0
      test/function/warn-on-namespace-conflict/deep.js
  29. 0
      test/function/warn-on-namespace-conflict/foo.js
  30. 0
      test/function/warn-on-namespace-conflict/main.js
  31. 24
      test/function/warn-on-top-level-this/_config.js
  32. 23
      test/function/warn-on-unused-missing-imports/_config.js
  33. 22
      test/sourcemaps/transform-without-sourcemap/_config.js
  34. 59
      test/test.js

30
src/Bundle.js

@ -188,7 +188,10 @@ export default class Bundle {
`'${unused[0]}' is` :
`${unused.slice( 0, -1 ).map( name => `'${name}'` ).join( ', ' )} and '${unused.pop()}' are`;
this.onwarn( `${names} imported from external module '${module.id}' but never used` );
this.warn({
code: 'UNUSED_EXTERNAL_IMPORT',
message: `${names} imported from external module '${module.id}' but never used`
});
});
this.orderedModules = this.sort();
@ -309,7 +312,10 @@ export default class Bundle {
keys( exportAllModule.exportsAll ).forEach( name => {
if ( name in module.exportsAll ) {
this.onwarn( `Conflicting namespaces: ${module.id} re-exports '${name}' from both ${module.exportsAll[ name ]} (will be ignored) and ${exportAllModule.exportsAll[ name ]}.` );
this.warn({
code: 'NAMESPACE_CONFLICT',
message: `Conflicting namespaces: ${relativeId( module.id )} re-exports '${name}' from both ${relativeId( module.exportsAll[ name ] )} (will be ignored) and ${relativeId( exportAllModule.exportsAll[ name ] )}`
});
}
module.exportsAll[ name ] = exportAllModule.exportsAll[ name ];
});
@ -333,7 +339,11 @@ export default class Bundle {
if ( !resolvedId && !isExternal ) {
if ( isRelative( source ) ) throw new Error( `Could not resolve '${source}' from ${module.id}` );
this.onwarn( `'${source}' is imported by ${relativeId( module.id )}, but could not be resolved – treating it as an external dependency. For help see https://github.com/rollup/rollup/wiki/Troubleshooting#treating-module-as-external-dependency` );
this.warn({
code: 'UNRESOLVED_IMPORT',
message: `'${source}' is imported by ${relativeId( module.id )}, but could not be resolved – treating it as an external dependency`,
url: 'https://github.com/rollup/rollup/wiki/Troubleshooting#treating-module-as-external-dependency'
});
isExternal = true;
}
@ -380,7 +390,11 @@ export default class Bundle {
render ( options = {} ) {
if ( options.format === 'es6' ) {
this.onwarn( 'The es6 format is deprecated – use `es` instead' );
this.warn({
code: 'DEPRECATED_ES6',
message: 'The es6 format is deprecated – use `es` instead'
});
options.format = 'es';
}
@ -404,7 +418,10 @@ export default class Bundle {
});
if ( !magicString.toString().trim() && this.entryModule.getExports().length === 0 ) {
this.onwarn( 'Generated an empty bundle' );
this.warn({
code: 'EMPTY_BUNDLE',
message: 'Generated an empty bundle'
});
}
timeEnd( 'render modules' );
@ -470,7 +487,7 @@ export default class Bundle {
if ( typeof map.mappings === 'string' ) {
map.mappings = decode( map.mappings );
}
map = collapseSourcemaps( file, map, usedModules, bundleSourcemapChain, this.onwarn );
map = collapseSourcemaps( this, file, map, usedModules, bundleSourcemapChain );
} else {
map = magicString.generateMap({ file, includeContent: true });
}
@ -535,6 +552,7 @@ export default class Bundle {
for ( i += 1; i < ordered.length; i += 1 ) {
const b = ordered[i];
// TODO reinstate this! it no longer works
if ( stronglyDependsOn[ a.id ][ b.id ] ) {
// somewhere, there is a module that imports b before a. Because
// b imports a, a is placed before b. We need to find the module

16
src/Module.js

@ -180,7 +180,12 @@ export default class Module {
this.exports[ exportedName ] = { localName };
});
} else {
this.bundle.onwarn( `Module ${this.id} has an empty export declaration` );
// TODO is this really necessary? `export {}` is valid JS, and
// might be used as a hint that this is indeed a module
this.warn({
code: 'EMPTY_EXPORT',
message: `Empty export declaration`
}, node.start );
}
}
}
@ -408,17 +413,12 @@ export default class Module {
}
warn ( warning, pos ) {
if ( pos ) {
if ( pos !== undefined ) {
warning.pos = pos;
const { line, column } = locate( this.code, pos, { offsetLine: 1 }); // TODO trace sourcemaps
warning.loc = {
file: this.id,
line: line + 1,
column
};
warning.loc = { file: this.id, line, column };
warning.frame = getCodeFrame( this.code, line, column );
}

7
src/ast/nodes/ExportDefaultDeclaration.js

@ -74,8 +74,11 @@ export default class ExportDefaultDeclaration extends Node {
const newlineSeparated = /\n/.test( code.original.slice( start, end ) );
if ( newlineSeparated ) {
const { line, column } = locate( this.module.code, this.declaration.start, { offsetLine: 1 });
this.module.bundle.onwarn( `${relativeId( this.module.id )} (${line}:${column}) Ambiguous default export (is a call expression, but looks like a function declaration). See https://github.com/rollup/rollup/wiki/Troubleshooting#ambiguous-default-export` );
this.module.warn({
code: 'AMBIGUOUS_DEFAULT_EXPORT',
message: `Ambiguous default export (is a call expression, but looks like a function declaration)`,
url: 'https://github.com/rollup/rollup/wiki/Troubleshooting#ambiguous-default-export'
}, this.declaration.start );
}
}
}

8
src/ast/nodes/MemberExpression.js

@ -1,5 +1,4 @@
import isReference from 'is-reference';
import { locate } from 'locate-character';
import relativeId from '../../utils/relativeId.js';
import Node from '../Node.js';
import { UNKNOWN } from '../values.js';
@ -34,8 +33,11 @@ export default class MemberExpression extends Node {
declaration = declaration.module.traceExport( part.name );
if ( !declaration ) {
const { line, column } = locate( this.module.code, this.start, { offsetLine: 1 });
this.module.bundle.onwarn( `${relativeId( this.module.id )} (${line}:${column}) '${part.name}' is not exported by '${relativeId( exporterId )}'. See https://github.com/rollup/rollup/wiki/Troubleshooting#name-is-not-exported-by-module` );
this.module.warn({
code: 'MISSING_EXPORT',
message: `'${part.name}' is not exported by '${relativeId( exporterId )}'`,
url: `https://github.com/rollup/rollup/wiki/Troubleshooting#name-is-not-exported-by-module`
}, part.start );
this.replacement = 'undefined';
return;
}

12
src/ast/nodes/ThisExpression.js

@ -1,8 +1,4 @@
import Node from '../Node.js';
import { locate } from 'locate-character';
import relativeId from '../../utils/relativeId.js';
const warning = `The 'this' keyword is equivalent to 'undefined' at the top level of an ES module, and has been rewritten. See https://github.com/rollup/rollup/wiki/Troubleshooting#this-is-undefined for more information`;
export default class ThisExpression extends Node {
initialise ( scope ) {
@ -11,9 +7,11 @@ export default class ThisExpression extends Node {
if ( lexicalBoundary.isModuleScope ) {
this.alias = this.module.context;
if ( this.alias === 'undefined' ) {
const { line, column } = locate( this.module.code, this.start, { offsetLine: 1 });
const detail = `${relativeId( this.module.id )} (${line}:${column + 1})`; // use one-based column number convention
this.module.bundle.onwarn( `${detail} ${warning}` );
this.module.warn({
code: 'THIS_IS_UNDEFINED',
message: `The 'this' keyword is equivalent to 'undefined' at the top level of an ES module, and has been rewritten`,
url: `https://github.com/rollup/rollup/wiki/Troubleshooting#this-is-undefined`
}, this.start );
}
}
}

6
src/ast/scopes/ModuleScope.js

@ -1,4 +1,5 @@
import { forOwn } from '../../utils/object.js';
import relativeId from '../../utils/relativeId.js';
import Scope from './Scope.js';
export default class ModuleScope extends Scope {
@ -26,7 +27,10 @@ export default class ModuleScope extends Scope {
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}` );
this.module.warn({
code: 'NON_EXISTENT_EXPORT',
message: `Non-existent export '${specifier.name}' is imported from ${relativeId( specifier.module.id )}`
}, specifier.specifier.start );
return;
}

2
src/finalisers/iife.js

@ -25,7 +25,7 @@ function setupNamespace ( keypath ) {
}
export default function iife ( bundle, magicString, { exportMode, indentString, intro, outro }, options ) {
const globalNameMaker = getGlobalNameMaker( options.globals || blank(), bundle.onwarn );
const globalNameMaker = getGlobalNameMaker( options.globals || blank(), bundle );
const name = options.moduleName;
const isNamespaced = name && ~name.indexOf( '.' );

8
src/finalisers/shared/getGlobalNameMaker.js

@ -1,11 +1,15 @@
export default function getGlobalNameMaker ( globals, onwarn ) {
export default function getGlobalNameMaker ( globals, bundle ) {
const fn = typeof globals === 'function' ? globals : id => globals[ id ];
return function ( module ) {
const name = fn( module.id );
if ( name ) return name;
onwarn( `No name was provided for external module '${module.id}' in options.globals – guessing '${module.name}'` );
bundle.warn({
code: 'MISSING_GLOBAL_NAME',
message: `No name was provided for external module '${module.id}' in options.globals – guessing '${module.name}'`
});
return module.name;
};
}

5
src/finalisers/shared/warnOnBuiltins.js

@ -35,5 +35,8 @@ export default function warnOnBuiltins ( bundle ) {
`module ('${externalBuiltins[0]}')` :
`modules (${externalBuiltins.slice( 0, -1 ).map( name => `'${name}'` ).join( ', ' )} and '${externalBuiltins.pop()}')`;
bundle.onwarn( `Creating a browser bundle that depends on Node.js built-in ${detail}. You might need to include https://www.npmjs.com/package/rollup-plugin-node-builtins` );
bundle.warn({
code: 'MISSING_NODE_BUILTINS',
message: `Creating a browser bundle that depends on Node.js built-in ${detail}. You might need to include https://www.npmjs.com/package/rollup-plugin-node-builtins`
});
}

2
src/finalisers/umd.js

@ -33,7 +33,7 @@ export default function umd ( bundle, magicString, { exportMode, indentString, i
warnOnBuiltins( bundle );
const globalNameMaker = getGlobalNameMaker( options.globals || blank(), bundle.onwarn );
const globalNameMaker = getGlobalNameMaker( options.globals || blank(), bundle );
const amdDeps = bundle.externalModules.map( quotePath );
const cjsDeps = bundle.externalModules.map( req );

8
src/utils/collapseSourcemaps.js

@ -98,7 +98,7 @@ class Link {
}
}
export default function collapseSourcemaps ( file, map, modules, bundleSourcemapChain, onwarn ) {
export default function collapseSourcemaps ( bundle, file, map, modules, bundleSourcemapChain ) {
const moduleSources = modules.filter( module => !module.excludeFromSourcemap ).map( module => {
let sourceMapChain = module.sourceMapChain;
@ -127,7 +127,11 @@ export default function collapseSourcemaps ( file, map, modules, bundleSourcemap
sourceMapChain.forEach( map => {
if ( map.missing ) {
onwarn( `Sourcemap is likely to be incorrect: a plugin${map.plugin ? ` ('${map.plugin}')` : ``} was used to transform files, but didn't generate a sourcemap for the transformation. Consult https://github.com/rollup/rollup/wiki/Troubleshooting and the plugin documentation for more information` );
bundle.warn({
code: 'SOURCEMAP_BROKEN',
message: `Sourcemap is likely to be incorrect: a plugin${map.plugin ? ` ('${map.plugin}')` : ``} was used to transform files, but didn't generate a sourcemap for the transformation. Consult the plugin documentation for help`,
url: `https://github.com/rollup/rollup/wiki/Troubleshooting#sourcemap-is-likely-to-be-incorrect`
});
map = {
names: [],

6
src/utils/getExportMode.js

@ -24,7 +24,11 @@ export default function getExportMode ( bundle, {exports: exportMode, moduleName
exportMode = 'default';
} else {
if ( bundle.entryModule.exports.default && format !== 'es') {
bundle.onwarn( `Using named and default exports together. Consumers of your bundle will have to use ${moduleName || 'bundle'}['default'] to access the default export, which may not be what you want. Use \`exports: 'named'\` to disable this warning. See https://github.com/rollup/rollup/wiki/JavaScript-API#exports for more information` );
bundle.warn({
code: 'MIXED_EXPORTS',
message: `Using named and default exports together. Consumers of your bundle will have to use ${moduleName || 'bundle'}['default'] to access the default export, which may not be what you want. Use \`exports: 'named'\` to disable this warning`,
url: `https://github.com/rollup/rollup/wiki/JavaScript-API#exports`
});
}
exportMode = 'named';
}

11
test/function/assign-namespace-to-var/_config.js

@ -2,9 +2,10 @@ const assert = require( 'assert' );
module.exports = {
description: 'allows a namespace to be assigned to a variable',
warnings: warnings => {
assert.deepEqual( warnings, [
'Generated an empty bundle'
]);
}
warnings: [
{
code: 'EMPTY_BUNDLE',
message: 'Generated an empty bundle'
}
]
};

12
test/function/custom-path-resolver-async/_config.js

@ -21,11 +21,13 @@ module.exports = {
}
}]
},
warnings: function ( warnings ) {
assert.deepEqual( warnings, [
`'path' is imported by main.js, but could not be resolved – treating it as an external dependency. For help see https://github.com/rollup/rollup/wiki/Troubleshooting#treating-module-as-external-dependency`
]);
},
warnings: [
{
code: 'UNRESOLVED_IMPORT',
message: `'path' is imported by main.js, but could not be resolved – treating it as an external dependency`,
url: `https://github.com/rollup/rollup/wiki/Troubleshooting#treating-module-as-external-dependency`
}
],
exports: function ( exports ) {
assert.strictEqual( exports.path, require( 'path' ) );
}

12
test/function/custom-path-resolver-sync/_config.js

@ -13,11 +13,13 @@ module.exports = {
}
}]
},
warnings: function ( warnings ) {
assert.deepEqual( warnings, [
`'path' is imported by main.js, but could not be resolved – treating it as an external dependency. For help see https://github.com/rollup/rollup/wiki/Troubleshooting#treating-module-as-external-dependency`
]);
},
warnings: [
{
code: 'UNRESOLVED_IMPORT',
message: `'path' is imported by main.js, but could not be resolved – treating it as an external dependency`,
url: `https://github.com/rollup/rollup/wiki/Troubleshooting#treating-module-as-external-dependency`
}
],
exports: function ( exports ) {
assert.strictEqual( exports.path, require( 'path' ) );
}

12
test/function/does-not-hang-on-missing-module/_config.js

@ -2,11 +2,13 @@ var assert = require( 'assert' );
module.exports = {
description: 'does not hang on missing module (#53)',
warnings: warnings => {
assert.deepEqual( warnings, [
`'unlessYouCreatedThisFileForSomeReason' is imported by main.js, but could not be resolved – treating it as an external dependency. For help see https://github.com/rollup/rollup/wiki/Troubleshooting#treating-module-as-external-dependency`
]);
},
warnings: [
{
code: 'UNRESOLVED_IMPORT',
message: `'unlessYouCreatedThisFileForSomeReason' is imported by main.js, but could not be resolved – treating it as an external dependency`,
url: `https://github.com/rollup/rollup/wiki/Troubleshooting#treating-module-as-external-dependency`
}
],
runtimeError: function ( error ) {
assert.equal( "Cannot find module 'unlessYouCreatedThisFileForSomeReason'", error.message );
}

13
test/function/double-named-export-from/_config.js

@ -1,13 +0,0 @@
const { resolve } = require('path');
const assert = require( 'assert' );
const r = path => resolve( __dirname, path );
module.exports = {
description: 'throws on duplicate export * from',
warnings ( warnings ) {
assert.deepEqual( warnings, [
`Conflicting namespaces: ${r('main.js')} re-exports 'foo' from both ${r('foo.js')} (will be ignored) and ${r('deep.js')}.`
]);
}
};

29
test/function/empty-exports/_config.js

@ -1,12 +1,23 @@
const assert = require( 'assert' );
const path = require( 'path' );
module.exports = {
description: 'warns on export {}, but does not fail',
warnings: warnings => {
assert.deepEqual( warnings, [
`Module ${path.resolve( __dirname, 'main.js' )} has an empty export declaration`,
'Generated an empty bundle'
]);
}
warnings: [
{
code: 'EMPTY_EXPORT',
message: 'Empty export declaration',
pos: 0,
loc: {
file: require( 'path' ).resolve( __dirname, 'main.js' ),
line: 1,
column: 0
},
frame: `
1: export {};
^
`
},
{
code: 'EMPTY_BUNDLE',
message: 'Generated an empty bundle'
}
]
};

11
test/function/module-tree/_config.js

@ -34,9 +34,10 @@ module.exports = {
}
]);
},
warnings: warnings => {
assert.deepEqual( warnings, [
'Generated an empty bundle'
]);
}
warnings: [
{
code: 'EMPTY_BUNDLE',
message: 'Generated an empty bundle'
}
]
};

24
test/function/namespace-missing-export/_config.js

@ -1,9 +1,21 @@
var assert = require( 'assert' );
module.exports = {
options: {
onwarn: function ( msg ) {
assert.equal( msg, `main.js (3:21) 'foo' is not exported by 'empty.js'. See https://github.com/rollup/rollup/wiki/Troubleshooting#name-is-not-exported-by-module` );
warnings: [
{
code: 'MISSING_EXPORT',
message: `'foo' is not exported by 'empty.js'`,
pos: 61,
loc: {
file: require( 'path' ).resolve( __dirname, 'main.js' ),
line: 3,
column: 25
},
frame: `
1: import * as mod from './empty.js';
2:
3: assert.equal( typeof mod.foo, 'undefined' );
^
`,
url: `https://github.com/rollup/rollup/wiki/Troubleshooting#name-is-not-exported-by-module`
}
}
]
};

22
test/function/unused-import/_config.js

@ -2,11 +2,19 @@ const assert = require( 'assert' );
module.exports = {
description: 'warns on unused imports ([#595])',
warnings: warnings => {
assert.deepEqual( warnings, [
`'external' is imported by main.js, but could not be resolved – treating it as an external dependency. For help see https://github.com/rollup/rollup/wiki/Troubleshooting#treating-module-as-external-dependency`,
`'unused', 'notused' and 'neverused' are imported from external module 'external' but never used`,
`Generated an empty bundle`
]);
}
warnings: [
{
code: 'UNRESOLVED_IMPORT',
message: `'external' is imported by main.js, but could not be resolved – treating it as an external dependency`,
url: `https://github.com/rollup/rollup/wiki/Troubleshooting#treating-module-as-external-dependency`
},
{
code: 'UNUSED_EXTERNAL_IMPORT',
message: `'unused', 'notused' and 'neverused' are imported from external module 'external' but never used`
},
{
code: 'EMPTY_BUNDLE',
message: `Generated an empty bundle`
}
]
};

24
test/function/warn-on-ambiguous-function-export/_config.js

@ -2,9 +2,23 @@ const assert = require( 'assert' );
module.exports = {
description: 'uses original name of default export function (#1011)',
warnings: warnings => {
assert.deepEqual( warnings, [
'foo.js (1:15) Ambiguous default export (is a call expression, but looks like a function declaration). See https://github.com/rollup/rollup/wiki/Troubleshooting#ambiguous-default-export'
]);
}
warnings: [
{
code: 'AMBIGUOUS_DEFAULT_EXPORT',
message: `Ambiguous default export (is a call expression, but looks like a function declaration)`,
pos: 15,
loc: {
file: require( 'path' ).resolve( __dirname, 'foo.js' ),
line: 1,
column: 15
},
frame: `
1: export default function foo ( a, b ) {
^
2: assert.equal( a, b );
3: return 3;
`,
url: `https://github.com/rollup/rollup/wiki/Troubleshooting#ambiguous-default-export`
}
]
};

14
test/function/warn-on-auto-named-default-exports/_config.js

@ -1,10 +1,10 @@
var assert = require( 'assert' );
module.exports = {
description: 'warns if default and named exports are used in auto mode',
warnings: function ( warnings ) {
assert.deepEqual( warnings, [
'Using named and default exports together. Consumers of your bundle will have to use bundle[\'default\'] to access the default export, which may not be what you want. Use `exports: \'named\'` to disable this warning. See https://github.com/rollup/rollup/wiki/JavaScript-API#exports for more information'
]);
}
warnings: [
{
code: 'MIXED_EXPORTS',
message: `Using named and default exports together. Consumers of your bundle will have to use bundle['default'] to access the default export, which may not be what you want. Use \`exports: 'named'\` to disable this warning`,
url: `https://github.com/rollup/rollup/wiki/JavaScript-API#exports`
}
]
};

13
test/function/warn-on-empty-bundle/_config.js

@ -1,10 +1,9 @@
const assert = require( 'assert' );
module.exports = {
description: 'warns if empty bundle is generated (#444)',
warnings: warnings => {
assert.deepEqual( warnings, [
'Generated an empty bundle'
]);
}
warnings: [
{
code: 'EMPTY_BUNDLE',
message: 'Generated an empty bundle'
}
]
};

6
test/function/warn-on-eval/_config.js

@ -8,8 +8,12 @@ module.exports = {
loc: {
column: 13,
file: require( 'path' ).resolve( __dirname, 'main.js' ),
line: 2
line: 1
},
frame: `
1: var result = eval( '1 + 1' );
^
`,
url: 'https://github.com/rollup/rollup/wiki/Troubleshooting#avoiding-eval'
}
]

9
test/function/warn-on-namespace-conflict/_config.js

@ -0,0 +1,9 @@
module.exports = {
description: 'warns on duplicate export * from',
warnings: [
{
code: 'NAMESPACE_CONFLICT',
message: `Conflicting namespaces: main.js re-exports 'foo' from both foo.js (will be ignored) and deep.js`
}
]
};

0
test/function/double-named-export-from/bar.js → test/function/warn-on-namespace-conflict/bar.js

0
test/function/double-named-export-from/deep.js → test/function/warn-on-namespace-conflict/deep.js

0
test/function/double-named-export-from/foo.js → test/function/warn-on-namespace-conflict/foo.js

0
test/function/double-named-export-from/main.js → test/function/warn-on-namespace-conflict/main.js

24
test/function/warn-on-top-level-this/_config.js

@ -2,11 +2,25 @@ const assert = require( 'assert' );
module.exports = {
description: 'warns on top-level this (#770)',
warnings: warnings => {
assert.deepEqual( warnings, [
`main.js (3:1) The 'this' keyword is equivalent to 'undefined' at the top level of an ES module, and has been rewritten. See https://github.com/rollup/rollup/wiki/Troubleshooting#this-is-undefined for more information`
]);
},
warnings: [
{
code: 'THIS_IS_UNDEFINED',
message: `The 'this' keyword is equivalent to 'undefined' at the top level of an ES module, and has been rewritten`,
pos: 81,
loc: {
file: require( 'path' ).resolve( __dirname, 'main.js' ),
line: 3,
column: 0
},
frame: `
1: const someVariableJustToCheckOnCorrectLineNumber = true; // eslint-disable-line
2:
3: this.foo = 'bar';
^
`,
url: `https://github.com/rollup/rollup/wiki/Troubleshooting#this-is-undefined`
}
],
runtimeError: err => {
assert.equal( err.message, `Cannot set property 'foo' of undefined` );
}

23
test/function/warn-on-unused-missing-imports/_config.js

@ -3,9 +3,22 @@ const assert = require( 'assert' );
module.exports = {
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')}`
]);
}
warnings: [
{
code: 'NON_EXISTENT_EXPORT',
message: `Non-existent export 'b' is imported from foo.js`,
pos: 12,
loc: {
file: path.resolve( __dirname, 'main.js' ),
line: 1,
column: 12
},
frame: `
1: import { a, b } from './foo.js';
^
2:
3: assert.equal( a, 42 );
`
}
]
};

22
test/sourcemaps/transform-without-sourcemap/_config.js

@ -1,10 +1,5 @@
const assert = require( 'assert' );
let warnings = [];
module.exports = {
description: 'preserves sourcemap chains when transforming',
before: () => warnings = [], // reset
options: {
plugins: [
{
@ -13,14 +8,13 @@ module.exports = {
return code;
}
}
],
onwarn ( msg ) {
warnings.push( msg );
}
]
},
test: () => {
assert.deepEqual( warnings, [
`Sourcemap is likely to be incorrect: a plugin ('fake plugin') was used to transform files, but didn't generate a sourcemap for the transformation. Consult https://github.com/rollup/rollup/wiki/Troubleshooting and the plugin documentation for more information`
]);
}
warnings: [
{
code: `SOURCEMAP_BROKEN`,
message: `Sourcemap is likely to be incorrect: a plugin ('fake plugin') was used to transform files, but didn't generate a sourcemap for the transformation. Consult the plugin documentation for help`,
url: `https://github.com/rollup/rollup/wiki/Troubleshooting#sourcemap-is-likely-to-be-incorrect`
}
]
};

59
test/test.js

@ -58,6 +58,27 @@ function loader ( modules ) {
};
}
function compareWarnings ( actual, expected ) {
assert.deepEqual(
actual.map( warning => {
const clone = Object.assign( {}, warning );
delete clone.toString;
if ( clone.frame ) {
clone.frame = clone.frame.replace( /\s+$/gm, '' );
}
return clone;
}),
expected.map( warning => {
if ( warning.frame ) {
warning.frame = warning.frame.slice( 1 ).replace( /^\t+/gm, '' ).replace( /\s+$/gm, '' ).trim();
}
return warning;
})
);
}
describe( 'rollup', function () {
this.timeout( 10000 );
@ -271,13 +292,7 @@ describe( 'rollup', function () {
if ( config.warnings ) {
if ( Array.isArray( config.warnings ) ) {
assert.deepEqual( warnings.map( warning => {
const clone = Object.assign( {}, warning );
delete clone.toString;
delete clone.frame;
return clone;
}), config.warnings );
compareWarnings( warnings, config.warnings );
} else {
config.warnings( warnings );
}
@ -387,11 +402,18 @@ describe( 'rollup', function () {
const entry = path.resolve( SOURCEMAPS, dir, 'main.js' );
const dest = path.resolve( SOURCEMAPS, dir, '_actual/bundle' );
const options = extend( {}, config.options, { entry });
let warnings;
const options = extend( {}, config.options, {
entry,
onwarn: warning => warnings.push( warning )
});
PROFILES.forEach( profile => {
( config.skip ? it.skip : config.solo ? it.only : it )( 'generates ' + profile.format, () => {
process.chdir( SOURCEMAPS + '/' + dir );
warnings = [];
return rollup.rollup( options ).then( bundle => {
const options = extend( {}, {
format: profile.format,
@ -401,9 +423,16 @@ describe( 'rollup', function () {
bundle.write( options );
if ( config.before ) config.before();
const result = bundle.generate( options );
config.test( result.code, result.map );
if ( config.test ) {
const { code, map } = bundle.generate( options );
config.test( code, map );
}
if ( config.warnings ) {
compareWarnings( warnings, config.warnings );
} else if ( warnings.length ) {
throw new Error( `Unexpected warnings` );
}
});
});
});
@ -747,11 +776,9 @@ describe( 'rollup', function () {
moduleName: 'myBundle'
});
assert.deepEqual( warnings, [
`'util' is imported by entry, but could not be resolved – treating it as an external dependency. For help see https://github.com/rollup/rollup/wiki/Troubleshooting#treating-module-as-external-dependency`,
`Creating a browser bundle that depends on Node.js built-in module ('util'). You might need to include https://www.npmjs.com/package/rollup-plugin-node-builtins`,
`No name was provided for external module 'util' in options.globals – guessing 'util'`
]);
const relevantWarnings = warnings.filter( warning => warning.code === 'MISSING_NODE_BUILTINS' );
assert.equal( relevantWarnings.length, 1 );
assert.equal( relevantWarnings[0].message, `Creating a browser bundle that depends on Node.js built-in module ('util'). You might need to include https://www.npmjs.com/package/rollup-plugin-node-builtins` );
});
});
});

Loading…
Cancel
Save