From 1fc4b1c86777f486d5778f6ad8442cbd958d4579 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Mon, 24 Jun 2019 18:09:01 -0400 Subject: [PATCH] add Error to known globals, declare catch block parameter. fixes #3064 --- src/compiler/compile/utils/scope.ts | 7 +++++++ src/compiler/utils/names.ts | 1 + .../catch-declares-error-variable/input.svelte | 13 +++++++++++++ .../catch-declares-error-variable/warnings.json | 1 + 4 files changed, 22 insertions(+) create mode 100644 test/validator/samples/catch-declares-error-variable/input.svelte create mode 100644 test/validator/samples/catch-declares-error-variable/warnings.json diff --git a/src/compiler/compile/utils/scope.ts b/src/compiler/compile/utils/scope.ts index 75e44f0adf..0255ac9419 100644 --- a/src/compiler/compile/utils/scope.ts +++ b/src/compiler/compile/utils/scope.ts @@ -40,6 +40,13 @@ export function create_scopes(expression: Node) { map.set(node, scope); } else if (/(Class|Variable)Declaration/.test(node.type)) { scope.add_declaration(node); + } else if (node.type === 'CatchClause') { + scope = new Scope(scope, true); + map.set(node, scope); + + extract_names(node.param).forEach(name => { + scope.declarations.set(name, node.param); + }); } else if (node.type === 'Identifier' && is_reference(node as ESTreeNode, parent as ESTreeNode)) { if (!scope.has(node.name) && !globals.has(node.name)) { globals.set(node.name, node); diff --git a/src/compiler/utils/names.ts b/src/compiler/utils/names.ts index f1413548aa..9805c92b91 100644 --- a/src/compiler/utils/names.ts +++ b/src/compiler/utils/names.ts @@ -13,6 +13,7 @@ export const globals = new Set([ 'document', 'encodeURI', 'encodeURIComponent', + 'Error', 'Infinity', 'Intl', 'isFinite', diff --git a/test/validator/samples/catch-declares-error-variable/input.svelte b/test/validator/samples/catch-declares-error-variable/input.svelte new file mode 100644 index 0000000000..b44e854cf5 --- /dev/null +++ b/test/validator/samples/catch-declares-error-variable/input.svelte @@ -0,0 +1,13 @@ + + + + +{String(value)} diff --git a/test/validator/samples/catch-declares-error-variable/warnings.json b/test/validator/samples/catch-declares-error-variable/warnings.json new file mode 100644 index 0000000000..0637a088a0 --- /dev/null +++ b/test/validator/samples/catch-declares-error-variable/warnings.json @@ -0,0 +1 @@ +[] \ No newline at end of file