Add support for import typeof, fixes #1975

This commit is contained in:
cpojer 2015-07-10 23:35:13 -07:00
parent 950d3d2280
commit 18380d2aee
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) { instance.extend("parseImportSpecifiers", function (inner) {
return function (node) { return function (node) {
node.isType = false node.importKind = "value"
if (this.isContextual("type")) { var kind =
var start = this.markPosition() (this.type === tt._typeof ? "typeof" :
var typeId = this.parseIdent() (this.isContextual("type") ? "type" : null))
if ((this.type === tt.name && this.value !== "from") || this.type === tt.braceL || this.type === tt.star) { if (kind) {
node.isType = true var lh = this.lookahead()
} else { if ((lh.type === tt.name && lh.value !== "from") || lh.type === tt.braceL || lh.type === tt.star) {
node.specifiers.push(this.parseImportSpecifierDefault(typeId, start)) this.next()
if (this.isContextual("from")) return node.importKind = kind
this.eat(tt.comma)
} }
} }
inner.call(this, node) inner.call(this, node)

View File

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

View File

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

View File

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

View File

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

View File

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