Add plugin for import.meta proposal (#544)

* Add plugin for import.meta proposal

Fixes https://github.com/babel/babylon/issues/539

* Tests for assignment/mutation of import.meta

* Use correct identifier in failure message

* Simpler & more consistent script errors for import.meta
This commit is contained in:
Jan Olaf Krems
2017-05-30 16:28:51 -07:00
committed by Henry Zhu
parent 2f5d146d54
commit d4e842d4eb
15 changed files with 1164 additions and 2 deletions

View File

@@ -427,6 +427,10 @@ export default class ExpressionParser extends LValParser {
return this.finishNode(node, "Super");
case tt._import:
if (this.hasPlugin("importMeta") && this.lookahead().type === tt.dot) {
return this.parseImportMetaProperty();
}
if (!this.hasPlugin("dynamicImport")) this.unexpected();
node = this.startNode();
@@ -588,12 +592,22 @@ export default class ExpressionParser extends LValParser {
node.property = this.parseIdentifier(true);
if (node.property.name !== propertyName) {
this.raise(node.property.start, `The only valid meta property for new is ${meta.name}.${propertyName}`);
this.raise(node.property.start, `The only valid meta property for ${meta.name} is ${meta.name}.${propertyName}`);
}
return this.finishNode(node, "MetaProperty");
}
parseImportMetaProperty(): N.MetaProperty {
const node = this.startNode();
const id = this.parseIdentifier(true);
this.expect(tt.dot);
if (!this.inModule) {
this.raise(id.start, "import.meta may appear only with 'sourceType: module'");
}
return this.parseMetaProperty(node, id, "meta");
}
parseLiteral<T : N.Literal>(value: any, type: /*T["kind"]*/string, startPos?: number, startLoc?: Position): T {
startPos = startPos || this.state.start;
startLoc = startLoc || this.state.startLoc;