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

import.meta: Stage 2 #539

Closed
1 of 2 tasks
hzoo opened this issue May 26, 2017 · 6 comments · Fixed by #544
Closed
1 of 2 tasks

import.meta: Stage 2 #539

hzoo opened this issue May 26, 2017 · 6 comments · Fixed by #544

Comments

@hzoo
Copy link
Member

hzoo commented May 26, 2017

import.meta for stage 2 (Domenic Denicola) (@domenic)

Info

Proposed at TC39 Meeting: May 2017
Spec Repo: https://github.com/tc39/proposal-import-meta
Spec Text: https://tc39.github.io/proposal-import-meta/

Example

(async () => {
  const response = await fetch(new URL("../hamsters.jpg", import.meta.url));
  const blob = await response.blob();

  const size = import.meta.scriptElement.dataset.size || 300;

  const image = new Image();
  image.src = URL.createObjectURL(blob);
  image.width = image.height = size;

  document.body.appendChild(image);
})();

Parsing/ESTree

  • make issue
  • Sounds pretty straightforward to make it a MetaProperty like new.target
{
  "type": "MetaProperty",
  "meta": {
    "type": "Identifier",
    "name": "import"
  },
  "property": {
    "type": "Identifier",
    "name": "meta"
  }
}

Transform

  • Not sure, maybe webpack should handle? Or in node?
@Jessidhia
Copy link
Member

How should the tree look like when MemberExpression are involved, like in import.meta.foo?

// super-like
MemberExpression {
  object: MemberExpression {
    object: Import {}
    property: Identifier { name: 'meta' }
  }
  property: Identifier { name: 'foo' }
}
// new.target-like
MemberExpression {
  object: MetaProperty {
    meta: Identifier { name: 'import' }
    property: Identifier { name: 'meta' }
  }
  property: Identifier { name: 'foo' }
}

ESTree specifically defined an Import node expecting it to be used like super in the future.

https://github.com/estree/estree/blob/master/experimental/import-expression.md

@bakkot
Copy link
Contributor

bakkot commented May 26, 2017

I'd vote for making it like new.target, definitely. That's certainly how it's spec'd, and import.somethingElse is not grammatical.

I believe ESTree's import expression is just to support dynamic import: import("whatever") is legal. It has nothing to do with this proposal from the AST's perspective, despite the visual similarity.

@Jessidhia
Copy link
Member

The reasoning for naming it Import instead of DynamicImport was because of the similarity to Super when it got meta-properties added.

It would be nice to be able to have Import {} as the MetaProperty's meta, instead of an Identifier { name: 'import' } though.

@bakkot
Copy link
Contributor

bakkot commented May 26, 2017

I'm sad I wasn't paying attention when that decision was made, then. import.meta is a thing of its own, and has nothing to do with import("") except that they have one token in common.

Not a big fan of the MetaProperty node type either, come to that. new.target and import.meta really ought to be their own node types (possibly subtypes of MetaProperty, I guess, but not involving any strings anywhere). Since that ship has apparently sailed, I think Identifier { name: 'import' } is more in keeping with new.target than Import {} is.

jkrems pushed a commit to jkrems/babylon that referenced this issue May 27, 2017
jkrems pushed a commit to jkrems/babylon that referenced this issue May 27, 2017
@hzoo hzoo added the Has PR label May 27, 2017
@hzoo hzoo closed this as completed in #544 May 30, 2017
hzoo pushed a commit that referenced this issue May 30, 2017
* Add plugin for import.meta proposal

Fixes #539

* Tests for assignment/mutation of import.meta

* Use correct identifier in failure message

* Simpler & more consistent script errors for import.meta
@hzoo
Copy link
Member Author

hzoo commented Jun 7, 2017

@babel-bot move to babel/babel

@hzoo
Copy link
Member Author

hzoo commented Jun 7, 2017

Hey @hzoo! I've moved your issue to the correct repository. Please make sure to keep an eye on the new issue for the latest information.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants