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 a18d8dc
Show file tree
Hide file tree
Showing 17 changed files with 300 additions and 236 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);
}
};
}
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();
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 a18d8dc

Please sign in to comment.