Skip to content

Commit 3b15e32

Browse files
trueadmRich-Harris
andauthored
fix: ensure generate guards against keywords (#9790)
* fix: ensure generate guards against keywords * changeset * lint * Update .changeset/old-flies-jog.md --------- Co-authored-by: Rich Harris <[email protected]>
1 parent 9c3516d commit 3b15e32

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

.changeset/old-flies-jog.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: ensure generated code does not use keywords as variable names

packages/svelte/src/compiler/phases/constants.js

+40
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,43 @@ export const EventModifiers = [
196196
'self',
197197
'trusted'
198198
];
199+
200+
export const JsKeywords = [
201+
'class',
202+
'break',
203+
'const',
204+
'let',
205+
'var',
206+
'continue',
207+
'if',
208+
'for',
209+
'while',
210+
'do',
211+
'new',
212+
'static',
213+
'true',
214+
'false',
215+
'void',
216+
'with',
217+
'yield',
218+
'await',
219+
'typeof',
220+
'throw',
221+
'throws',
222+
'null',
223+
'delete',
224+
'default',
225+
'catch',
226+
'debugger',
227+
'case',
228+
'arguments',
229+
'else',
230+
'extends',
231+
'export',
232+
'import',
233+
'extends',
234+
'switch',
235+
'instanceof',
236+
'return',
237+
'this'
238+
];

packages/svelte/src/compiler/phases/scope.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { is_element_node } from './nodes.js';
44
import * as b from '../utils/builders.js';
55
import { error } from '../errors.js';
66
import { extract_identifiers, extract_identifiers_from_expression } from '../utils/ast.js';
7-
import { Runes } from './constants.js';
7+
import { JsKeywords, Runes } from './constants.js';
88

99
export class Scope {
1010
/** @type {ScopeRoot} */
@@ -133,7 +133,8 @@ export class Scope {
133133
while (
134134
this.references.has(name) ||
135135
this.declarations.has(name) ||
136-
this.root.conflicts.has(name)
136+
this.root.conflicts.has(name) ||
137+
JsKeywords.includes(name)
137138
) {
138139
name = `${preferred_name}_${n++}`;
139140
}

0 commit comments

Comments
 (0)