allow reserved words as keys when parsing destructured contexts

pull/4390/head
Conduitry 6 years ago
parent ba9365446f
commit 71d604b67e

@ -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`

@ -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,

Loading…
Cancel
Save