Skip to content

Commit

Permalink
Improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Nov 12, 2019
1 parent b0a0598 commit a3a2315
Show file tree
Hide file tree
Showing 30 changed files with 430 additions and 243 deletions.
13 changes: 1 addition & 12 deletions src/Chunk.ts
Expand Up @@ -644,19 +644,8 @@ export default class Chunk {
) {
timeStart('render format', 3);

if (!this.renderedSource)
throw new Error('Internal error: Chunk render called before preRender');

const format = options.format as string;
const finalise = finalisers[format];
if (!finalise) {
error({
code: 'INVALID_OPTION',
message: `Invalid format: ${format} - valid options are ${Object.keys(finalisers).join(
', '
)}.`
});
}
if (options.dynamicImportFunction && format !== 'es') {
this.graph.warn({
code: 'INVALID_OPTION',
Expand Down Expand Up @@ -709,7 +698,7 @@ export default class Chunk {
}

const magicString = finalise(
this.renderedSource,
this.renderedSource as MagicStringBundle,
{
accessedGlobals,
dependencies: this.renderedDeclarations.dependencies,
Expand Down
27 changes: 12 additions & 15 deletions src/utils/PluginCache.ts
Expand Up @@ -63,39 +63,36 @@ export const NO_CACHE: PluginCache = {
}
};

function uncacheablePluginError(pluginName: string) {
function uncacheablePluginError(pluginName: string): never {
if (
pluginName.startsWith(ANONYMOUS_PLUGIN_PREFIX) ||
pluginName.startsWith(ANONYMOUS_OUTPUT_PLUGIN_PREFIX)
)
error({
) {
return error({
code: 'ANONYMOUS_PLUGIN_CACHE',
message:
'A plugin is trying to use the Rollup cache but is not declaring a plugin name or cacheKey.'
});
else
error({
code: 'DUPLICATE_PLUGIN_NAME',
message: `The plugin name ${pluginName} is being used twice in the same build. Plugin names must be distinct or provide a cacheKey (please post an issue to the plugin if you are a plugin user).`
});
}
return error({
code: 'DUPLICATE_PLUGIN_NAME',
message: `The plugin name ${pluginName} is being used twice in the same build. Plugin names must be distinct or provide a cacheKey (please post an issue to the plugin if you are a plugin user).`
});
}

export function getCacheForUncacheablePlugin(pluginName: string): PluginCache {
return {
has() {
uncacheablePluginError(pluginName);
return false;
return uncacheablePluginError(pluginName);
},
get() {
uncacheablePluginError(pluginName);
return undefined as any;
return uncacheablePluginError(pluginName);
},
set() {
uncacheablePluginError(pluginName);
return uncacheablePluginError(pluginName);
},
delete() {
uncacheablePluginError(pluginName);
return false;
return uncacheablePluginError(pluginName);
}
};
}
12 changes: 5 additions & 7 deletions src/utils/PluginDriver.ts
Expand Up @@ -109,7 +109,7 @@ export class PluginDriver {
replaceContext?: ReplaceContext
): R {
for (let i = 0; i < this.plugins.length; i++) {
const result = this.runHookSync(hookName, args, i, false, replaceContext);
const result = this.runHookSync(hookName, args, i, replaceContext);
if (result != null) return result as any;
}
return null as any;
Expand Down Expand Up @@ -158,7 +158,7 @@ export class PluginDriver {
replaceContext?: ReplaceContext
): R {
for (let i = 0; i < this.plugins.length; i++) {
const result: any = this.runHookSync(hookName, [arg0, ...args], i, false, replaceContext);
const result: any = this.runHookSync(hookName, [arg0, ...args], i, replaceContext);
arg0 = reduce.call(this.pluginContexts[i], arg0, result, this.plugins[i]);
}
return arg0;
Expand Down Expand Up @@ -195,7 +195,7 @@ export class PluginDriver {
): T {
let acc = initialValue;
for (let i = 0; i < this.plugins.length; i++) {
const result: any = this.runHookSync(hookName, args, i, true, replaceContext);
const result: any = this.runHookSync(hookName, args, i, replaceContext);
acc = reduce.call(this.pluginContexts[i], acc, result, this.plugins[i]);
}
return acc;
Expand All @@ -222,14 +222,14 @@ export class PluginDriver {
replaceContext?: ReplaceContext
): void {
for (let i = 0; i < this.plugins.length; i++)
this.runHookSync<void>(hookName, args as any[], i, false, replaceContext);
this.runHookSync<void>(hookName, args as any[], i, replaceContext);
}

private runHook<T>(
hookName: string,
args: any[],
pluginIndex: number,
permitValues = false,
permitValues: boolean,
hookContext?: ReplaceContext | null
): Promise<T> {
this.previousHooks.add(hookName);
Expand Down Expand Up @@ -260,7 +260,6 @@ export class PluginDriver {
hookName: string,
args: any[],
pluginIndex: number,
permitValues = false,
hookContext?: ReplaceContext
): T {
this.previousHooks.add(hookName);
Expand All @@ -275,7 +274,6 @@ export class PluginDriver {
try {
// permit values allows values to be returned instead of a functional hook
if (typeof hook !== 'function') {
if (permitValues) return hook;
error({
code: 'INVALID_PLUGIN_HOOK',
message: `Error running plugin hook ${hookName} for ${plugin.name}, expected a function hook.`
Expand Down
11 changes: 11 additions & 0 deletions test/function/samples/file-and-dir/_config.js
@@ -0,0 +1,11 @@
module.exports = {
description: 'throws when using both the file and the dir option',
options: {
output: { file: 'bundle.js', dir: 'dist' }
},
generateError: {
code: 'INVALID_OPTION',
message:
'You must set either "output.file" for a single-file build or "output.dir" when generating multiple chunks.'
}
};
1 change: 1 addition & 0 deletions test/function/samples/file-and-dir/foo.js
@@ -0,0 +1 @@
export default 42;
1 change: 1 addition & 0 deletions test/function/samples/file-and-dir/main.js
@@ -0,0 +1 @@
export default () => import('./foo.js');
10 changes: 10 additions & 0 deletions test/function/samples/iife-code-splitting/_config.js
@@ -0,0 +1,10 @@
module.exports = {
description: 'throws when generating multiple chunks for an IIFE build',
options: {
output: { format: 'iife' }
},
generateError: {
code: 'INVALID_OPTION',
message: 'UMD and IIFE output formats are not supported for code-splitting builds.'
}
};
1 change: 1 addition & 0 deletions test/function/samples/iife-code-splitting/foo.js
@@ -0,0 +1 @@
export default 42;
1 change: 1 addition & 0 deletions test/function/samples/iife-code-splitting/main.js
@@ -0,0 +1 @@
export default () => import('./foo.js');
11 changes: 11 additions & 0 deletions test/function/samples/invalid-top-level-await/_config.js
@@ -0,0 +1,11 @@
module.exports = {
description: 'throws for invalid top-level-await format',
options: {
experimentalTopLevelAwait: true
},
generateError: {
code: 'INVALID_TLA_FORMAT',
message:
'Module format cjs does not support top-level await. Use the "es" or "system" output formats rather.'
}
};
1 change: 1 addition & 0 deletions test/function/samples/invalid-top-level-await/main.js
@@ -0,0 +1 @@
await Promise.resolve();
15 changes: 15 additions & 0 deletions test/function/samples/non-function-hook-async/_config.js
@@ -0,0 +1,15 @@
module.exports = {
description: 'throws when providing a value for an async function hook',
options: {
plugins: {
resolveId: 'value'
}
},
error: {
code: 'PLUGIN_ERROR',
hook: 'resolveId',
message: 'Error running plugin hook resolveId for at position 1, expected a function hook.',
plugin: 'at position 1',
pluginCode: 'INVALID_PLUGIN_HOOK'
}
};
1 change: 1 addition & 0 deletions test/function/samples/non-function-hook-async/foo.js
@@ -0,0 +1 @@
export default 42;
1 change: 1 addition & 0 deletions test/function/samples/non-function-hook-async/main.js
@@ -0,0 +1 @@
export default () => import('./foo.js');
15 changes: 15 additions & 0 deletions test/function/samples/non-function-hook-sync/_config.js
@@ -0,0 +1,15 @@
module.exports = {
description: 'throws when providing a value for a sync function hook',
options: {
plugins: {
outputOptions: 'value'
}
},
generateError: {
code: 'PLUGIN_ERROR',
hook: 'outputOptions',
message: 'Error running plugin hook outputOptions for at position 1, expected a function hook.',
plugin: 'at position 1',
pluginCode: 'INVALID_PLUGIN_HOOK'
}
};
1 change: 1 addition & 0 deletions test/function/samples/non-function-hook-sync/foo.js
@@ -0,0 +1 @@
export default 42;
1 change: 1 addition & 0 deletions test/function/samples/non-function-hook-sync/main.js
@@ -0,0 +1 @@
export default () => import('./foo.js');
18 changes: 18 additions & 0 deletions test/function/samples/plugin-cache/anonymous-delete/_config.js
@@ -0,0 +1,18 @@
module.exports = {
description: 'throws for anonymous plugins deleting from the cache',
options: {
plugins: {
buildStart() {
this.cache.delete('asdf');
}
}
},
error: {
code: 'PLUGIN_ERROR',
hook: 'buildStart',
message:
'A plugin is trying to use the Rollup cache but is not declaring a plugin name or cacheKey.',
plugin: 'at position 1',
pluginCode: 'ANONYMOUS_PLUGIN_CACHE'
}
};
@@ -0,0 +1 @@
assert.equal( 1, 1 );
18 changes: 18 additions & 0 deletions test/function/samples/plugin-cache/anonymous-get/_config.js
@@ -0,0 +1,18 @@
module.exports = {
description: 'throws for anonymous plugins reading the cache',
options: {
plugins: {
buildStart() {
this.cache.get('asdf');
}
}
},
error: {
code: 'PLUGIN_ERROR',
hook: 'buildStart',
message:
'A plugin is trying to use the Rollup cache but is not declaring a plugin name or cacheKey.',
plugin: 'at position 1',
pluginCode: 'ANONYMOUS_PLUGIN_CACHE'
}
};
1 change: 1 addition & 0 deletions test/function/samples/plugin-cache/anonymous-get/main.js
@@ -0,0 +1 @@
assert.equal( 1, 1 );
18 changes: 18 additions & 0 deletions test/function/samples/plugin-cache/anonymous-has/_config.js
@@ -0,0 +1,18 @@
module.exports = {
description: 'throws for anonymous plugins checking the cache',
options: {
plugins: {
buildStart() {
this.cache.has('asdf');
}
}
},
error: {
code: 'PLUGIN_ERROR',
hook: 'buildStart',
message:
'A plugin is trying to use the Rollup cache but is not declaring a plugin name or cacheKey.',
plugin: 'at position 1',
pluginCode: 'ANONYMOUS_PLUGIN_CACHE'
}
};
1 change: 1 addition & 0 deletions test/function/samples/plugin-cache/anonymous-has/main.js
@@ -0,0 +1 @@
assert.equal( 1, 1 );
18 changes: 18 additions & 0 deletions test/function/samples/plugin-cache/anonymous-set/_config.js
@@ -0,0 +1,18 @@
module.exports = {
description: 'throws for anonymous plugins adding to the cache',
options: {
plugins: {
buildStart() {
this.cache.set('asdf', 'asdf');
}
}
},
error: {
code: 'PLUGIN_ERROR',
hook: 'buildStart',
message:
'A plugin is trying to use the Rollup cache but is not declaring a plugin name or cacheKey.',
plugin: 'at position 1',
pluginCode: 'ANONYMOUS_PLUGIN_CACHE'
}
};
1 change: 1 addition & 0 deletions test/function/samples/plugin-cache/anonymous-set/main.js
@@ -0,0 +1 @@
assert.equal( 1, 1 );
@@ -0,0 +1,15 @@
module.exports = {
description: 'allows plugins to have the same name if they do not access the cache',
options: {
plugins: [
{
name: 'test-plugin',
buildStart() {}
},
{
name: 'test-plugin',
buildStart() {}
}
]
}
};
@@ -0,0 +1 @@
assert.equal( 1, 1 );
27 changes: 27 additions & 0 deletions test/function/samples/plugin-cache/duplicate-names/_config.js
@@ -0,0 +1,27 @@
module.exports = {
description: 'throws if two plugins with the same name and no cache key access the cache',
options: {
plugins: [
{
name: 'test-plugin',
buildStart() {
this.cache.set('asdf', 'asdf');
}
},
{
name: 'test-plugin',
buildStart() {
this.cache.set('asdf', 'asdf');
}
}
]
},
error: {
code: 'PLUGIN_ERROR',
hook: 'buildStart',
message:
'The plugin name test-plugin is being used twice in the same build. Plugin names must be distinct or provide a cacheKey (please post an issue to the plugin if you are a plugin user).',
plugin: 'test-plugin',
pluginCode: 'DUPLICATE_PLUGIN_NAME'
}
};
1 change: 1 addition & 0 deletions test/function/samples/plugin-cache/duplicate-names/main.js
@@ -0,0 +1 @@
assert.equal( 1, 1 );

0 comments on commit a3a2315

Please sign in to comment.