diff --git a/src/compiler/parse/index.ts b/src/compiler/parse/index.ts index e552374698..a809eeebeb 100644 --- a/src/compiler/parse/index.ts +++ b/src/compiler/parse/index.ts @@ -141,7 +141,7 @@ export class Parser { return result; } - read_identifier() { + read_identifier(allow_reserved = false) { const start = this.index; let i = this.index; @@ -160,7 +160,7 @@ export class Parser { const identifier = this.template.slice(this.index, this.index = i); - if (reserved.has(identifier)) { + if (!allow_reserved && reserved.has(identifier)) { this.error({ code: `unexpected-reserved-word`, message: `'${identifier}' is a reserved word in JavaScript and cannot be used here` diff --git a/src/compiler/parse/read/context.ts b/src/compiler/parse/read/context.ts index 0db0c676a3..fe666467f8 100644 --- a/src/compiler/parse/read/context.ts +++ b/src/compiler/parse/read/context.ts @@ -1,4 +1,5 @@ import { Parser } from '../index'; +import { reserved } from '../../utils/names'; interface Identifier { start: number; @@ -116,8 +117,11 @@ export default function read_context(parser: Parser) { break; } + // TODO: DRY this out somehow + // We don't know whether we want to allow reserved words until we see whether there's a ':' after it + // Probably ideally we'd use Acorn to do all of this const start = parser.index; - const name = parser.read_identifier(); + const name = parser.read_identifier(true); const key: Identifier = { start, end: parser.index, @@ -126,9 +130,19 @@ export default function read_context(parser: Parser) { }; parser.allow_whitespace(); - const value = parser.eat(':') - ? (parser.allow_whitespace(), read_context(parser)) - : key; + let value: Context; + if (parser.eat(':')) { + parser.allow_whitespace(); + value = read_context(parser); + } else { + if (reserved.has(name)) { + parser.error({ + code: `unexpected-reserved-word`, + message: `'${name}' is a reserved word in JavaScript and cannot be used here` + }, start); + } + value = key; + } const property: Property = { start,