diff --git a/.changeset/old-flies-jog.md b/.changeset/old-flies-jog.md new file mode 100644 index 0000000000..aa207c47e1 --- /dev/null +++ b/.changeset/old-flies-jog.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: ensure generated code does not use keywords as variable names diff --git a/packages/svelte/src/compiler/phases/constants.js b/packages/svelte/src/compiler/phases/constants.js index d31a30f051..ff766e0e0a 100644 --- a/packages/svelte/src/compiler/phases/constants.js +++ b/packages/svelte/src/compiler/phases/constants.js @@ -196,3 +196,43 @@ export const EventModifiers = [ 'self', 'trusted' ]; + +export const JsKeywords = [ + 'class', + 'break', + 'const', + 'let', + 'var', + 'continue', + 'if', + 'for', + 'while', + 'do', + 'new', + 'static', + 'true', + 'false', + 'void', + 'with', + 'yield', + 'await', + 'typeof', + 'throw', + 'throws', + 'null', + 'delete', + 'default', + 'catch', + 'debugger', + 'case', + 'arguments', + 'else', + 'extends', + 'export', + 'import', + 'extends', + 'switch', + 'instanceof', + 'return', + 'this' +]; diff --git a/packages/svelte/src/compiler/phases/scope.js b/packages/svelte/src/compiler/phases/scope.js index 09b3e8934c..02cd980adc 100644 --- a/packages/svelte/src/compiler/phases/scope.js +++ b/packages/svelte/src/compiler/phases/scope.js @@ -4,7 +4,7 @@ import { is_element_node } from './nodes.js'; import * as b from '../utils/builders.js'; import { error } from '../errors.js'; import { extract_identifiers, extract_identifiers_from_expression } from '../utils/ast.js'; -import { Runes } from './constants.js'; +import { JsKeywords, Runes } from './constants.js'; export class Scope { /** @type {ScopeRoot} */ @@ -133,7 +133,8 @@ export class Scope { while ( this.references.has(name) || this.declarations.has(name) || - this.root.conflicts.has(name) + this.root.conflicts.has(name) || + JsKeywords.includes(name) ) { name = `${preferred_name}_${n++}`; }