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

Add plugin for import.meta proposal #544

Merged
merged 4 commits into from
May 30, 2017
Merged

Conversation

jkrems
Copy link
Contributor

@jkrems jkrems commented May 27, 2017

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
Copy link

codecov bot commented May 27, 2017

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.

@jkrems
Copy link
Contributor Author

jkrems commented May 27, 2017

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

@bakkot
Copy link
Contributor

bakkot commented May 27, 2017

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

@jkrems
Copy link
Contributor Author

jkrems commented May 27, 2017

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;
Copy link
Member

@hzoo hzoo May 28, 2017

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

Copy link
Member

@hzoo hzoo left a comment

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

Copy link
Member

@Jessidhia Jessidhia left a comment

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.

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

Copy link
Member

@existentialism existentialism left a comment

Choose a reason for hiding this comment

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

Nice work!

const node = this.startNode();
const id = this.parseIdentifier(true);
this.expect(tt.dot);
if (this.options.sourceType !== "module") {
Copy link
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
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.

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
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'

Copy link
Contributor Author

@jkrems jkrems May 29, 2017

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
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%) ⬇️

@jkrems
Copy link
Contributor Author

jkrems commented May 30, 2017

@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
@jkrems jkrems 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