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; return result;
} }
read_identifier() { read_identifier(allow_reserved = false) {
const start = this.index; const start = this.index;
let i = this.index; let i = this.index;
@ -160,7 +160,7 @@ export class Parser {
const identifier = this.template.slice(this.index, this.index = i); const identifier = this.template.slice(this.index, this.index = i);
if (reserved.has(identifier)) { if (!allow_reserved && reserved.has(identifier)) {
this.error({ this.error({
code: `unexpected-reserved-word`, code: `unexpected-reserved-word`,
message: `'${identifier}' is a reserved word in JavaScript and cannot be used here` message: `'${identifier}' is a reserved word in JavaScript and cannot be used here`

@ -1,4 +1,5 @@
import { Parser } from '../index'; import { Parser } from '../index';
import { reserved } from '../../utils/names';
interface Identifier { interface Identifier {
start: number; start: number;
@ -116,8 +117,11 @@ export default function read_context(parser: Parser) {
break; 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 start = parser.index;
const name = parser.read_identifier(); const name = parser.read_identifier(true);
const key: Identifier = { const key: Identifier = {
start, start,
end: parser.index, end: parser.index,
@ -126,9 +130,19 @@ export default function read_context(parser: Parser) {
}; };
parser.allow_whitespace(); parser.allow_whitespace();
const value = parser.eat(':') let value: Context;
? (parser.allow_whitespace(), read_context(parser)) if (parser.eat(':')) {
: key; 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 = { const property: Property = {
start, start,

Loading…
Cancel
Save