Skip to content
This repository has been archived by the owner on May 19, 2018. It is now read-only.

Commit

Permalink
Add plugin for import.meta proposal
Browse files Browse the repository at this point in the history
Fixes #539
  • Loading branch information
Jan Krems committed May 27, 2017
1 parent f326ef6 commit 826ced9
Show file tree
Hide file tree
Showing 11 changed files with 891 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ require("babylon").parse("code", {
- `functionSent`
- `dynamicImport` ([proposal](https://github.com/tc39/proposal-dynamic-import))
- `numericSeparator` ([proposal](https://github.com/samuelgoto/proposal-numeric-separator))
- `importMeta` ([proposal](https://github.com/tc39/proposal-import-meta))

### FAQ

Expand Down
14 changes: 14 additions & 0 deletions src/parser/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -594,6 +598,16 @@ export default class ExpressionParser extends LValParser {
return this.finishNode(node, "MetaProperty");
}

parseImportMetaProperty(): N.MetaProperty {
const node = this.startNode();
const id = this.parseIdentifier(true);
this.expect(tt.dot);
if (this.options.sourceType !== "module") {
this.raise(id.start, "import.meta can only be used in modules");
}
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;
Expand Down
3 changes: 2 additions & 1 deletion src/parser/statement.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ export default class StatementParser extends ExpressionParser {
case tt.semi: return this.parseEmptyStatement(node);
case tt._export:
case tt._import:
if (this.hasPlugin("dynamicImport") && this.lookahead().type === tt.parenL) break;
if ((this.hasPlugin("dynamicImport") && this.lookahead().type === tt.parenL) ||
(this.hasPlugin("importMeta") && this.lookahead().type === tt.dot)) break;

if (!this.options.allowImportExportEverywhere) {
if (!topLevel) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const x = import.meta;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"throws": "import.meta can only be used in modules (1:10)",
"plugins": ["dynamicImport", "importMeta"],
"sourceType": "script"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const x = import.meta;
const url = import.meta.url;
import.meta;
import.meta.url;
Loading

0 comments on commit 826ced9

Please sign in to comment.