-
-
Notifications
You must be signed in to change notification settings - Fork 255
Add plugin for import.meta proposal #544
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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.options.sourceType !== "module") { | ||
this.raise(id.start, "import.meta can only be used in modules"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. May want to make this consistent with other sourceType errors?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Heh, good point. Copied the wording from "can only be used in functions". Should've taken the wording from import/export statements. Fixed. |
||
} | ||
return this.parseMetaProperty(node, id, "meta"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The error message if the meta property isn't There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops, good point. Added a test that checks for that & made it use the correct name. |
||
} | ||
|
||
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; | ||
|
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 @@ | ||
import.notMeta; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"throws": "The only valid meta property for import is import.meta (1:7)", | ||
"sourceType": "module", | ||
"plugins": ["dynamicImport", "importMeta"] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import.meta = true; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"throws": "Invalid left-hand side in assignment expression (1:0)", | ||
"sourceType": "module", | ||
"plugins": ["dynamicImport", "importMeta"] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const x = import.meta; | ||
const url = import.meta.url; | ||
import.meta; | ||
import.meta.url; | ||
import.meta.couldBeMutable = true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can also do
if (!this.inModule)
hereThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Weird, I thought I tried that first but
.inModule
was only true top-level. Must have missed something. You're right, definitely works fine.