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 <richard.a.harris@gmail.com>
pull/9792/head
Dominic Gannaway 2 years ago committed by GitHub
parent 9c3516dd3d
commit 3b15e32a91
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: ensure generated code does not use keywords as variable names

@ -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'
];

@ -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++}`;
}

Loading…
Cancel
Save