Merge pull request #1976 from cpojer/import-typeof

Add support for `import typeof`, fixes #1975
This commit is contained in:
Sebastian McKenzie 2015-07-11 09:58:49 +01:00
commit 8539182452
7 changed files with 176 additions and 39 deletions

View File

@ -791,16 +791,15 @@ acorn.plugins.flow = function (instance) {
instance.extend("parseImportSpecifiers", function (inner) {
return function (node) {
node.isType = false
if (this.isContextual("type")) {
var start = this.markPosition()
var typeId = this.parseIdent()
if ((this.type === tt.name && this.value !== "from") || this.type === tt.braceL || this.type === tt.star) {
node.isType = true
} else {
node.specifiers.push(this.parseImportSpecifierDefault(typeId, start))
if (this.isContextual("from")) return
this.eat(tt.comma)
node.importKind = "value"
var kind =
(this.type === tt._typeof ? "typeof" :
(this.isContextual("type") ? "type" : null))
if (kind) {
var lh = this.lookahead()
if ((lh.type === tt.name && lh.value !== "from") || lh.type === tt.braceL || lh.type === tt.star) {
this.next()
node.importKind = kind
}
}
inner.call(this, node)

View File

@ -1,4 +1,4 @@
import {has, isArray} from "./util"
import {has} from "./util"
import {SourceLocation} from "./location"
// A second optional argument can be given to further configure
@ -94,11 +94,11 @@ export function getOptions(opts) {
for (let opt in defaultOptions)
options[opt] = opts && has(opts, opt) ? opts[opt] : defaultOptions[opt]
if (isArray(options.onToken)) {
if (Array.isArray(options.onToken)) {
let tokens = options.onToken
options.onToken = (token) => tokens.push(token)
}
if (isArray(options.onComment))
if (Array.isArray(options.onComment))
options.onComment = pushComment(options, options.onComment)
return options

View File

@ -1,7 +1,3 @@
export function isArray(obj) {
return Object.prototype.toString.call(obj) === "[object Array]"
}
// Checks if an object has a property.
export function has(obj, propName) {

View File

@ -90,8 +90,8 @@ function ExportDeclaration(node, print) {
export function ImportDeclaration(node, print) {
this.push("import ");
if (node.isType) {
this.push("type ");
if (node.importKind === "type" || node.importKind === "typeof") {
this.push(node.importKind + " ");
}
var specfiers = node.specifiers;

View File

@ -15,7 +15,7 @@ export var metadata = {
export var visitor = {
ImportDeclaration(node, parent, scope, file) {
// flow type
if (node.isType) return;
if (node.importKind === "type" || node.importKind === "typeof") return;
var nodes = [];

View File

@ -41,7 +41,7 @@ export var visitor = {
},
ImportDeclaration(node) {
if (node.isType) this.dangerouslyRemove();
if (node.importKind === "type" || node.importKind === "typeof") this.dangerouslyRemove();
},
ExportDeclaration() {

View File

@ -6728,13 +6728,49 @@ var fbTestFixture = {
end: { line: 1, column: 26 }
}
},
isType: true,
importKind: 'type',
range: [0, 27],
loc: {
start: { line: 1, column: 0 },
end: { line: 1, column: 27 }
}
},
'import typeof foo from "bar";': {
type: 'ImportDeclaration',
specifiers: [{
type: 'ImportDefaultSpecifier',
local: {
type: 'Identifier',
name: 'foo',
range: [14, 17],
loc: {
start: { line: 1, column: 14 },
end: { line: 1, column: 17 }
}
},
range: [14, 17],
loc: {
start: { line: 1, column: 14 },
end: { line: 1, column: 17 }
}
}],
source: {
type: 'Literal',
value: 'bar',
raw: '"bar"',
range: [23, 28],
loc: {
start: { line: 1, column: 23 },
end: { line: 1, column: 28 }
}
},
importKind: 'typeof',
range: [0, 29],
loc: {
start: { line: 1, column: 0 },
end: { line: 1, column: 29 }
}
},
'import type {foo, bar} from "baz";': {
type: 'ImportDeclaration',
specifiers: [{
@ -6798,17 +6834,26 @@ var fbTestFixture = {
end: { line: 1, column: 33 }
}
},
isType: true,
importKind: 'type',
range: [0, 34],
loc: {
start: { line: 1, column: 0 },
end: { line: 1, column: 34 }
}
},
'import type {foo as bar} from "baz";': {
'import type {foo, bar} from "baz";': {
type: 'ImportDeclaration',
specifiers: [{
type: 'ImportSpecifier',
local: {
type: 'Identifier',
name: 'foo',
range: [13, 16],
loc: {
start: { line: 1, column: 13 },
end: { line: 1, column: 16 }
}
},
imported: {
type: 'Identifier',
name: 'foo',
@ -6818,36 +6863,97 @@ var fbTestFixture = {
end: { line: 1, column: 16 }
}
},
range: [13, 16],
loc: {
start: { line: 1, column: 13 },
end: { line: 1, column: 16 }
}
}, {
type: 'ImportSpecifier',
local: {
type: 'Identifier',
name: 'bar',
range: [20, 23],
range: [18, 21],
loc: {
start: { line: 1, column: 20 },
end: { line: 1, column: 23 }
start: { line: 1, column: 18 },
end: { line: 1, column: 21 }
}
},
range: [13, 23],
imported: {
type: 'Identifier',
name: 'bar',
range: [18, 21],
loc: {
start: { line: 1, column: 13 },
end: { line: 1, column: 23 }
start: { line: 1, column: 18 },
end: { line: 1, column: 21 }
}
},
range: [18, 21],
loc: {
start: { line: 1, column: 18 },
end: { line: 1, column: 21 }
}
}],
source: {
type: 'Literal',
value: 'baz',
raw: '"baz"',
range: [30, 35],
range: [28, 33],
loc: {
start: { line: 1, column: 30 },
end: { line: 1, column: 35 }
start: { line: 1, column: 28 },
end: { line: 1, column: 33 }
}
},
isType: true,
range: [0, 36],
importKind: 'type',
range: [0, 34],
loc: {
start: { line: 1, column: 0 },
end: { line: 1, column: 36 }
end: { line: 1, column: 34 }
}
},
'import typeof {foo as bar} from "baz";': {
type: 'ImportDeclaration',
specifiers: [{
type: 'ImportSpecifier',
imported: {
type: 'Identifier',
name: 'foo',
range: [15, 18],
loc: {
start: { line: 1, column: 15 },
end: { line: 1, column: 18 }
}
},
local: {
type: 'Identifier',
name: 'bar',
range: [22, 25],
loc: {
start: { line: 1, column: 22 },
end: { line: 1, column: 25 }
}
},
range: [15, 25],
loc: {
start: { line: 1, column: 15 },
end: { line: 1, column: 25 }
}
}],
source: {
type: 'Literal',
value: 'baz',
raw: '"baz"',
range: [32, 37],
loc: {
start: { line: 1, column: 32 },
end: { line: 1, column: 37 }
}
},
importKind: 'typeof',
range: [0, 38],
loc: {
start: { line: 1, column: 0 },
end: { line: 1, column: 38 }
}
},
'import type from "foo";': {
@ -6879,7 +6985,7 @@ var fbTestFixture = {
end: { line: 1, column: 22 }
}
},
isType: false,
importKind: 'value',
range: [0, 23],
loc: {
start: { line: 1, column: 0 },
@ -6940,7 +7046,7 @@ var fbTestFixture = {
end: { line: 1, column: 29 }
}
},
isType: false,
importKind: 'value',
range: [0, 30],
loc: {
start: { line: 1, column: 0 },
@ -6976,13 +7082,49 @@ var fbTestFixture = {
end: { line: 1, column: 37 }
}
},
isType: true,
importKind: 'type',
range: [0, 38],
loc: {
start: { line: 1, column: 0 },
end: { line: 1, column: 38 }
}
},
'import typeof * as namespace from "bar";': {
type: 'ImportDeclaration',
specifiers: [{
type: 'ImportNamespaceSpecifier',
local: {
type: 'Identifier',
name: 'namespace',
range: [19, 28],
loc: {
start: { line: 1, column: 19 },
end: { line: 1, column: 28 }
}
},
range: [14, 28],
loc: {
start: { line: 1, column: 14 },
end: { line: 1, column: 28 }
}
}],
source: {
type: 'Literal',
value: 'bar',
raw: '"bar"',
range: [34, 39],
loc: {
start: { line: 1, column: 34 },
end: { line: 1, column: 39 }
}
},
importKind: 'typeof',
range: [0, 40],
loc: {
start: { line: 1, column: 0 },
end: { line: 1, column: 40 }
}
},
},
'Array Types': {
'var a: number[]': {