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

Add plugin for import.meta proposal - #544

Merged
hzoo merged 4 commits into
babel:masterfrom
hybrist:jk-import-meta
May 30, 2017
Merged

hzoo merged 4 commits into
babel:masterfrom
hybrist:jk-import-meta

Conversation

@hybrist

@hybrist hybrist commented May 27, 2017

Copy link
Copy Markdown
Contributor

Fixes #539

Q A
Bug fix? no
Breaking change? no
New feature? yes
Deprecations? no
Spec compliancy? yes
Tests added/pass? yes
Fixed tickets #539
License MIT

Relatively straight-forward: Parses import.meta as a new MetaProperty. The expression is valid in modules but not in scripts. It can be enabled even when dynamicImport isn't enabled.

@codecov

codecov Bot commented May 27, 2017 •

Copy link
Copy Markdown

Codecov Report

Merging #544 into master will decrease coverage by 1.32%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #544      +/-   ##
==========================================
- Coverage   98.14%   96.82%   -1.33%     
==========================================
  Files          22       22              
  Lines        3607     3617      +10     
  Branches     1007     1010       +3     
==========================================
- Hits         3540     3502      -38     
- Misses         24       58      +34     
- Partials       43       57      +14
Flag Coverage Δ
#babel ?
#babylon 96.82% <100%> (ø) ⬆️
Impacted Files Coverage Δ
src/parser/expression.js 93.48% <100%> (-3.37%) ⬇️
src/parser/statement.js 98.23% <100%> (-0.89%) ⬇️
src/parser/comments.js 87.87% <0%> (-6.07%) ⬇️
src/tokenizer/state.js 97.14% <0%> (-2.86%) ⬇️
src/plugins/flow.js 97.35% <0%> (-1.04%) ⬇️
src/tokenizer/index.js 97.41% <0%> (-1%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f326ef6...aa7baab. Read the comment docs.

@hybrist

hybrist commented May 27, 2017

Copy link
Copy Markdown
Contributor Author

The CI failure seems to be unrelated to this change..?

@bakkot

bakkot commented May 27, 2017

Copy link
Copy Markdown
Contributor

Should make sure to disallow import.meta = val. I don't think it's specced yet, but it will be.

@hybrist

hybrist commented May 27, 2017

Copy link
Copy Markdown
Contributor Author

Added tests to verify that:

  1. import.meta is not a valid assignment target
  2. import.meta.prop is a valid assignment target

const x = import.meta;
const url = import.meta.url;
import.meta;
import.meta.url;

@hzoo hzoo May 28, 2017 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was there a reason this test didn't also have import.meta.couldBeMutable = true;? But the other did?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No deeper reason than "order of copy&paste". ^^ Copied the actual.js/expected.json over again to keep them in sync.

@hzoo hzoo left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work! really fast 😄

cc @domenic

@Jessidhia Jessidhia left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me (but I'm not familiar with the parser at all).

AST format is:

MetaProperty {
  meta: Identifier { name: 'import' }
  property: Identifier { name: 'meta' }
}

Just need to do something about the error message when parsing, say, import.notMeta.

Comment thread src/parser/expression.js
if (this.options.sourceType !== "module") {
this.raise(id.start, "import.meta can only be used in modules");
}
return this.parseMetaProperty(node, id, "meta");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message if the meta property isn't .meta is confusing (it's hardcoded to mention new)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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.

@existentialism existentialism left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work!

Comment thread src/parser/expression.js Outdated
const node = this.startNode();
const id = this.parseIdentifier(true);
this.expect(tt.dot);
if (this.options.sourceType !== "module") {

Copy link
Copy Markdown
Member

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) here

Copy link
Copy Markdown
Contributor Author

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.

Comment thread src/parser/expression.js Outdated
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");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May want to make this consistent with other sourceType errors?

'import.meta' can only be used with 'sourceType: module'

@hybrist hybrist May 29, 2017 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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.

@rwaldron

Copy link
Copy Markdown
Contributor

This might be irrelevant, or possibly a subject to have in a new issue, but I'm curious about the drops in coverage? For example, it says above that six files were impacted, but four of them aren't changed at all:

  • src/parser/comments.js 87.87% <0%> (-6.07%) ⬇️
  • src/tokenizer/state.js 97.14% <0%> (-2.86%) ⬇️
  • src/plugins/flow.js 97.35% <0%> (-1.04%) ⬇️
  • src/tokenizer/index.js 97.41% <0%> (-1%) ⬇️

@hybrist

hybrist commented May 30, 2017

Copy link
Copy Markdown
Contributor Author

@rwaldron Yeah, I was wondering about the same thing. I tried rebasing on master in case it was using some weird merge head but afaict that wasn't it.

@hzoo
hzoo merged commit d4e842d into babel:master May 30, 2017
@hybrist
hybrist deleted the jk-import-meta branch May 30, 2017 23:30
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

import.meta: Stage 2

6 participants