From 6d0199e0e22061cba9ca97204198777762d64322 Mon Sep 17 00:00:00 2001 From: Lukas Taegert-Atkinson Date: Sat, 14 Nov 2020 14:57:32 +0100 Subject: [PATCH] Restore labels after included try statement block --- src/ast/nodes/TryStatement.ts | 8 ++++++++ .../samples/conditional-catch/_config.js | 3 +++ test/function/samples/conditional-catch/main.js | 17 +++++++++++++++++ test/watch/index.js | 17 ++++++++++------- 4 files changed, 38 insertions(+), 7 deletions(-) create mode 100644 test/function/samples/conditional-catch/_config.js create mode 100644 test/function/samples/conditional-catch/main.js diff --git a/src/ast/nodes/TryStatement.ts b/src/ast/nodes/TryStatement.ts index 0727b671c89..3a9483d1d26 100644 --- a/src/ast/nodes/TryStatement.ts +++ b/src/ast/nodes/TryStatement.ts @@ -12,6 +12,7 @@ export default class TryStatement extends StatementBase { type!: NodeType.tTryStatement; private directlyIncluded = false; + private includedLabelsAfterBlock: string[] | null = null; hasEffects(context: HasEffectsContext): boolean { return ( @@ -33,7 +34,14 @@ export default class TryStatement extends StatementBase { context, tryCatchDeoptimization ? INCLUDE_PARAMETERS : includeChildrenRecursively ); + if (context.includedLabels.size > 0) { + this.includedLabelsAfterBlock = [...context.includedLabels]; + } context.brokenFlow = brokenFlow; + } else if (this.includedLabelsAfterBlock) { + for (const label of this.includedLabelsAfterBlock) { + context.includedLabels.add(label); + } } if (this.handler !== null) { this.handler.include(context, includeChildrenRecursively); diff --git a/test/function/samples/conditional-catch/_config.js b/test/function/samples/conditional-catch/_config.js new file mode 100644 index 00000000000..7fe0c318b80 --- /dev/null +++ b/test/function/samples/conditional-catch/_config.js @@ -0,0 +1,3 @@ +module.exports = { + description: 'handles conditional catch blocks (#3869)' +}; diff --git a/test/function/samples/conditional-catch/main.js b/test/function/samples/conditional-catch/main.js new file mode 100644 index 00000000000..eb63434480e --- /dev/null +++ b/test/function/samples/conditional-catch/main.js @@ -0,0 +1,17 @@ +function g() { + var result; + g: { + try { + break g; + } catch (_) {} + return; + } + try { + throw 'Expected'; + } catch (e) { + result = e; + } + assert.strictEqual(result, 'Expected'); +} + +g(); diff --git a/test/watch/index.js b/test/watch/index.js index 8ef70c49a69..f7d9124fb52 100644 --- a/test/watch/index.js +++ b/test/watch/index.js @@ -292,7 +292,7 @@ describe('rollup.watch', () => { assert.strictEqual(run('../_tmp/output/bundle.js'), 42); assert.deepStrictEqual(events, ['create', 'update']); assert.deepStrictEqual(ids, expectedIds); - sander.rimrafSync(WATCHED_ID); + sander.unlinkSync(WATCHED_ID); }, 'START', 'BUNDLE_START', @@ -308,6 +308,7 @@ describe('rollup.watch', () => { it('correctly rewrites change event during build delay', async () => { const WATCHED_ID = path.resolve('test/_tmp/input/watched'); + const MAIN_ID = path.resolve('test/_tmp/input/main.js'); let lastEvent = null; await sander.copydir('test/watch/samples/watch-files').to('test/_tmp/input'); await wait(100); @@ -329,9 +330,10 @@ describe('rollup.watch', () => { this.addWatchFile(WATCHED_ID); }, watchChange(id, { event }) { - assert.strictEqual(lastEvent, null); - assert.strictEqual(id, WATCHED_ID); - lastEvent = event; + if (id === WATCHED_ID) { + assert.strictEqual(lastEvent, null); + lastEvent = event; + } } } }); @@ -342,11 +344,10 @@ describe('rollup.watch', () => { 'BUNDLE_END', 'END', async () => { - assert.strictEqual(run('../_tmp/output/bundle.js'), 42); assert.strictEqual(lastEvent, null); sander.writeFileSync(WATCHED_ID, 'another'); await wait(100); - sander.rimrafSync(WATCHED_ID); + sander.unlinkSync(WATCHED_ID); }, 'START', 'BUNDLE_START', @@ -357,7 +358,9 @@ describe('rollup.watch', () => { lastEvent = null; sander.writeFileSync(WATCHED_ID, '123'); await wait(100); - sander.rimrafSync(WATCHED_ID); + sander.unlinkSync(WATCHED_ID); + // To ensure there is always another change to trigger a rebuild + sander.writeFileSync(MAIN_ID, 'export default 43;'); }, 'START', 'BUNDLE_START',