diff --git a/scripts/flow_tests_whitelist.txt b/scripts/flow_tests_whitelist.txt index 53bb38651a..cf981ff080 100644 --- a/scripts/flow_tests_whitelist.txt +++ b/scripts/flow_tests_whitelist.txt @@ -58,15 +58,4 @@ types/parameter_defaults/migrated_0031.js types/parameter_defaults/migrated_0032.js types/string_literal_invalid/migrated_0000.js types/typecasts_invalid/migrated_0001.js -types/import_type_shorthand/reserved_type.js -types/import_type_shorthand/type_as_as.js -types/import_type_shorthand/typeof_reserved_type.js -types/import_type_shorthand/typeof_reserved_value.js -types/import_types/default_reserved_type.js -types/import_types/default_reserved_value.js -types/import_types/named_reserved_type.js -types/import_types/named_reserved_value.js -types/import_types/namespace_reserved_type.js -types/import_types/namespace_reserved_value.js -types/import_types/typeof_default_reserved_type.js -types/import_types/typeof_namespace_reserved_type.js +types/import_types/typeof_named_reserved_type.js diff --git a/src/parser/statement.js b/src/parser/statement.js index 05d33368e8..3f175b8a02 100644 --- a/src/parser/statement.js +++ b/src/parser/statement.js @@ -5,7 +5,6 @@ import * as N from "../types"; import { types as tt, type TokenType } from "../tokenizer/types"; import ExpressionParser from "./expression"; -import type { Position } from "../util/location"; import { lineBreak } from "../util/whitespace"; // Reused empty array added for node fields that are always empty. @@ -1579,21 +1578,34 @@ export default class StatementParser extends ExpressionParser { return this.finishNode(node, "ImportDeclaration"); } - // Parses a comma-separated list of module imports. + // eslint-disable-next-line no-unused-vars + shouldParseDefaultImport(node: N.ImportDeclaration): boolean { + return this.match(tt.name); + } + parseImportSpecifierLocal( + node: N.ImportDeclaration, + specifier: N.Node, + type: string, + contextDescription: string, + ): void { + specifier.local = this.parseIdentifier(); + this.checkLVal(specifier.local, true, undefined, contextDescription); + node.specifiers.push(this.finishNode(specifier, type)); + } + + // Parses a comma-separated list of module imports. parseImportSpecifiers(node: N.ImportDeclaration): void { let first = true; - if (this.match(tt.name)) { + if (this.shouldParseDefaultImport(node)) { // import defaultObj, { x, y as z } from '...' - const startPos = this.state.start; - const startLoc = this.state.startLoc; - node.specifiers.push( - this.parseImportSpecifierDefault( - this.parseIdentifier(), - startPos, - startLoc, - ), + this.parseImportSpecifierLocal( + node, + this.startNode(), + "ImportDefaultSpecifier", + "default import specifier", ); + if (!this.eat(tt.comma)) return; } @@ -1601,16 +1613,14 @@ export default class StatementParser extends ExpressionParser { const specifier = this.startNode(); this.next(); this.expectContextual("as"); - specifier.local = this.parseIdentifier(); - this.checkLVal( - specifier.local, - true, - undefined, + + this.parseImportSpecifierLocal( + node, + specifier, + "ImportNamespaceSpecifier", "import namespace specifier", ); - node.specifiers.push( - this.finishNode(specifier, "ImportNamespaceSpecifier"), - ); + return; } @@ -1652,15 +1662,4 @@ export default class StatementParser extends ExpressionParser { this.checkLVal(specifier.local, true, undefined, "import specifier"); node.specifiers.push(this.finishNode(specifier, "ImportSpecifier")); } - - parseImportSpecifierDefault( - id: N.Identifier, - startPos: number, - startLoc: Position, - ): N.ImportDefaultSpecifier { - const node = this.startNodeAt(startPos, startLoc); - node.local = id; - this.checkLVal(node.local, true, undefined, "default import specifier"); - return this.finishNode(node, "ImportDefaultSpecifier"); - } } diff --git a/src/parser/util.js b/src/parser/util.js index 03a2b764ec..7828e5fbdc 100644 --- a/src/parser/util.js +++ b/src/parser/util.js @@ -49,6 +49,11 @@ export default class UtilParser extends Tokenizer { return this.match(tt.name) && this.state.value === name; } + isLookaheadContextual(name: string): boolean { + const l = this.lookahead(); + return l.type === tt.name && l.value === name; + } + // Consumes contextual keyword if possible. eatContextual(name: string): boolean { diff --git a/src/plugins/flow.js b/src/plugins/flow.js index 3351461786..b3b04ecfbd 100644 --- a/src/plugins/flow.js +++ b/src/plugins/flow.js @@ -6,17 +6,22 @@ import type Parser from "../parser"; import { types as tt, type TokenType } from "../tokenizer/types"; import * as N from "../types"; import type { Pos, Position } from "../util/location"; +import type State from "../tokenizer/state"; const primitiveTypes = [ "any", - "mixed", - "empty", "bool", "boolean", - "number", - "string", - "void", + "empty", + "false", + "mixed", "null", + "number", + "static", + "string", + "true", + "typeof", + "void", ]; function isEsModuleType(bodyElement: N.Node): boolean { @@ -29,6 +34,16 @@ function isEsModuleType(bodyElement: N.Node): boolean { ); } +function hasTypeImportKind(node: N.Node): boolean { + return node.importKind === "type" || node.importKind === "typeof"; +} + +function isMaybeDefaultImport(state: State): boolean { + return ( + (state.type === tt.name || !!state.type.keyword) && state.value !== "from" + ); +} + const exportSuggestions = { const: "declare export var", let: "declare export var", @@ -420,14 +435,14 @@ export default (superClass: Class): Class => return this.finishNode(node, "InterfaceDeclaration"); } - flowParseRestrictedIdentifier(liberal?: boolean): N.Identifier { - if (primitiveTypes.indexOf(this.state.value) > -1) { - this.raise( - this.state.start, - `Cannot overwrite primitive type ${this.state.value}`, - ); + checkReservedType(word: string, startLoc: number) { + if (primitiveTypes.indexOf(word) > -1) { + this.raise(startLoc, `Cannot overwrite primitive type ${word}`); } + } + flowParseRestrictedIdentifier(liberal?: boolean): N.Identifier { + this.checkReservedType(this.state.value, this.state.start); return this.parseIdentifier(liberal); } @@ -1860,6 +1875,28 @@ export default (superClass: Class): Class => return node; } + shouldParseDefaultImport(node: N.ImportDeclaration): boolean { + if (!hasTypeImportKind(node)) { + return super.shouldParseDefaultImport(node); + } + + return isMaybeDefaultImport(this.state); + } + + parseImportSpecifierLocal( + node: N.ImportDeclaration, + specifier: N.Node, + type: string, + contextDescription: string, + ): void { + specifier.local = hasTypeImportKind(node) + ? this.flowParseRestrictedIdentifier(true) + : this.parseIdentifier(); + + this.checkLVal(specifier.local, true, undefined, contextDescription); + node.specifiers.push(this.finishNode(specifier, type)); + } + // parse typeof and type imports parseImportSpecifiers(node: N.ImportDeclaration): void { node.importKind = "value"; @@ -1873,7 +1910,7 @@ export default (superClass: Class): Class => if (kind) { const lh = this.lookahead(); if ( - (lh.type === tt.name && lh.value !== "from") || + isMaybeDefaultImport(lh) || lh.type === tt.braceL || lh.type === tt.star ) { @@ -1899,7 +1936,7 @@ export default (superClass: Class): Class => } let isBinding = false; - if (this.isContextual("as")) { + if (this.isContextual("as") && !this.isLookaheadContextual("as")) { const as_ident = this.parseIdentifier(true); if ( specifierTypeKind !== null && @@ -1936,23 +1973,28 @@ export default (superClass: Class): Class => specifier.local = specifier.imported.__clone(); } - if ( - (node.importKind === "type" || node.importKind === "typeof") && - (specifier.importKind === "type" || specifier.importKind === "typeof") - ) { + const nodeIsTypeImport = hasTypeImportKind(node); + const specifierIsTypeImport = hasTypeImportKind(specifier); + + if (nodeIsTypeImport && specifierIsTypeImport) { this.raise( firstIdentLoc, - "`The `type` and `typeof` keywords on named imports can only be used on regular `import` statements. It cannot be used with `import type` or `import typeof` statements`", + "The `type` and `typeof` keywords on named imports can only be used on regular `import` statements. It cannot be used with `import type` or `import typeof` statements", ); } - if (isBinding) + if (nodeIsTypeImport || specifierIsTypeImport) { + this.checkReservedType(specifier.local.name, specifier.local.start); + } + + if (isBinding && !nodeIsTypeImport && !specifierIsTypeImport) { this.checkReservedWord( specifier.local.name, specifier.start, true, true, ); + } this.checkLVal(specifier.local, true, undefined, "import specifier"); node.specifiers.push(this.finishNode(specifier, "ImportSpecifier")); diff --git a/src/plugins/typescript.js b/src/plugins/typescript.js index 6483205d03..47078f7e11 100644 --- a/src/plugins/typescript.js +++ b/src/plugins/typescript.js @@ -1030,7 +1030,7 @@ export default (superClass: Class): Class => /* optionalId */ false, ); case tt._const: - if (this.match(tt._const) && this.lookaheadIsContextual("enum")) { + if (this.match(tt._const) && this.isLookaheadContextual("enum")) { // `const enum = 0;` not allowed because "enum" is a strict mode reserved word. this.expect(tt._const); this.expectContextual("enum"); @@ -1051,11 +1051,6 @@ export default (superClass: Class): Class => } } - lookaheadIsContextual(name: string): boolean { - const l = this.lookahead(); - return l.type === tt.name && l.value === name; - } - // Note: this won't be called unless the keyword is allowed in `shouldParseExportDeclaration`. tsTryParseExportDeclaration(): ?N.Declaration { return this.tsParseDeclaration( diff --git a/test/fixtures/flow/type-imports/import-type-shorthand/actual.js b/test/fixtures/flow/type-imports/import-type-shorthand/actual.js index fbb21d87cf..43d6942549 100644 --- a/test/fixtures/flow/type-imports/import-type-shorthand/actual.js +++ b/test/fixtures/flow/type-imports/import-type-shorthand/actual.js @@ -1,8 +1,11 @@ import {type} from "foo"; import {type t} from "foo"; import {type as} from "foo"; +import {type as as foo} from "foo"; import {type t as u} from "foo"; +import {type switch} from "foo"; import {typeof t} from "foo"; import {typeof as} from "foo"; import {typeof t as u} from "foo"; +import {typeof switch} from "foo"; diff --git a/test/fixtures/flow/type-imports/import-type-shorthand/expected.json b/test/fixtures/flow/type-imports/import-type-shorthand/expected.json index 6c0fc4995b..515e0330c3 100644 --- a/test/fixtures/flow/type-imports/import-type-shorthand/expected.json +++ b/test/fixtures/flow/type-imports/import-type-shorthand/expected.json @@ -1,28 +1,28 @@ { "type": "File", "start": 0, - "end": 212, + "end": 316, "loc": { "start": { "line": 1, "column": 0 }, "end": { - "line": 8, + "line": 11, "column": 34 } }, "program": { "type": "Program", "start": 0, - "end": 212, + "end": 316, "loc": { "start": { "line": 1, "column": 0 }, "end": { - "line": 8, + "line": 11, "column": 34 } }, @@ -295,7 +295,7 @@ { "type": "ImportDeclaration", "start": 83, - "end": 115, + "end": 118, "loc": { "start": { "line": 4, @@ -303,14 +303,14 @@ }, "end": { "line": 4, - "column": 32 + "column": 35 } }, "specifiers": [ { "type": "ImportSpecifier", "start": 91, - "end": 102, + "end": 105, "loc": { "start": { "line": 4, @@ -318,13 +318,13 @@ }, "end": { "line": 4, - "column": 19 + "column": 22 } }, "imported": { "type": "Identifier", "start": 96, - "end": 97, + "end": 98, "loc": { "start": { "line": 4, @@ -332,6 +332,94 @@ }, "end": { "line": 4, + "column": 15 + }, + "identifierName": "as" + }, + "name": "as" + }, + "importKind": "type", + "local": { + "type": "Identifier", + "start": 102, + "end": 105, + "loc": { + "start": { + "line": 4, + "column": 19 + }, + "end": { + "line": 4, + "column": 22 + }, + "identifierName": "foo" + }, + "name": "foo" + } + } + ], + "importKind": "value", + "source": { + "type": "StringLiteral", + "start": 112, + "end": 117, + "loc": { + "start": { + "line": 4, + "column": 29 + }, + "end": { + "line": 4, + "column": 34 + } + }, + "extra": { + "rawValue": "foo", + "raw": "\"foo\"" + }, + "value": "foo" + } + }, + { + "type": "ImportDeclaration", + "start": 119, + "end": 151, + "loc": { + "start": { + "line": 5, + "column": 0 + }, + "end": { + "line": 5, + "column": 32 + } + }, + "specifiers": [ + { + "type": "ImportSpecifier", + "start": 127, + "end": 138, + "loc": { + "start": { + "line": 5, + "column": 8 + }, + "end": { + "line": 5, + "column": 19 + } + }, + "imported": { + "type": "Identifier", + "start": 132, + "end": 133, + "loc": { + "start": { + "line": 5, + "column": 13 + }, + "end": { + "line": 5, "column": 14 }, "identifierName": "t" @@ -341,15 +429,15 @@ "importKind": "type", "local": { "type": "Identifier", - "start": 101, - "end": 102, + "start": 137, + "end": 138, "loc": { "start": { - "line": 4, + "line": 5, "column": 18 }, "end": { - "line": 4, + "line": 5, "column": 19 }, "identifierName": "u" @@ -361,15 +449,15 @@ "importKind": "value", "source": { "type": "StringLiteral", - "start": 109, - "end": 114, + "start": 145, + "end": 150, "loc": { "start": { - "line": 4, + "line": 5, "column": 26 }, "end": { - "line": 4, + "line": 5, "column": 31 } }, @@ -382,8 +470,8 @@ }, { "type": "ImportDeclaration", - "start": 117, - "end": 146, + "start": 152, + "end": 184, "loc": { "start": { "line": 6, @@ -391,14 +479,14 @@ }, "end": { "line": 6, - "column": 29 + "column": 32 } }, "specifiers": [ { "type": "ImportSpecifier", - "start": 125, - "end": 133, + "start": 160, + "end": 171, "loc": { "start": { "line": 6, @@ -406,20 +494,108 @@ }, "end": { "line": 6, + "column": 19 + } + }, + "imported": { + "type": "Identifier", + "start": 165, + "end": 171, + "loc": { + "start": { + "line": 6, + "column": 13 + }, + "end": { + "line": 6, + "column": 19 + }, + "identifierName": "switch" + }, + "name": "switch" + }, + "importKind": "type", + "local": { + "type": "Identifier", + "start": 165, + "end": 171, + "loc": { + "start": { + "line": 6, + "column": 13 + }, + "end": { + "line": 6, + "column": 19 + }, + "identifierName": "switch" + }, + "name": "switch" + } + } + ], + "importKind": "value", + "source": { + "type": "StringLiteral", + "start": 178, + "end": 183, + "loc": { + "start": { + "line": 6, + "column": 26 + }, + "end": { + "line": 6, + "column": 31 + } + }, + "extra": { + "rawValue": "foo", + "raw": "\"foo\"" + }, + "value": "foo" + } + }, + { + "type": "ImportDeclaration", + "start": 186, + "end": 215, + "loc": { + "start": { + "line": 8, + "column": 0 + }, + "end": { + "line": 8, + "column": 29 + } + }, + "specifiers": [ + { + "type": "ImportSpecifier", + "start": 194, + "end": 202, + "loc": { + "start": { + "line": 8, + "column": 8 + }, + "end": { + "line": 8, "column": 16 } }, "imported": { "type": "Identifier", - "start": 132, - "end": 133, + "start": 201, + "end": 202, "loc": { "start": { - "line": 6, + "line": 8, "column": 15 }, "end": { - "line": 6, + "line": 8, "column": 16 }, "identifierName": "t" @@ -429,15 +605,15 @@ "importKind": "typeof", "local": { "type": "Identifier", - "start": 132, - "end": 133, + "start": 201, + "end": 202, "loc": { "start": { - "line": 6, + "line": 8, "column": 15 }, "end": { - "line": 6, + "line": 8, "column": 16 }, "identifierName": "t" @@ -449,15 +625,15 @@ "importKind": "value", "source": { "type": "StringLiteral", - "start": 140, - "end": 145, + "start": 209, + "end": 214, "loc": { "start": { - "line": 6, + "line": 8, "column": 23 }, "end": { - "line": 6, + "line": 8, "column": 28 } }, @@ -470,44 +646,44 @@ }, { "type": "ImportDeclaration", - "start": 147, - "end": 177, + "start": 216, + "end": 246, "loc": { "start": { - "line": 7, + "line": 9, "column": 0 }, "end": { - "line": 7, + "line": 9, "column": 30 } }, "specifiers": [ { "type": "ImportSpecifier", - "start": 155, - "end": 164, + "start": 224, + "end": 233, "loc": { "start": { - "line": 7, + "line": 9, "column": 8 }, "end": { - "line": 7, + "line": 9, "column": 17 } }, "imported": { "type": "Identifier", - "start": 162, - "end": 164, + "start": 231, + "end": 233, "loc": { "start": { - "line": 7, + "line": 9, "column": 15 }, "end": { - "line": 7, + "line": 9, "column": 17 }, "identifierName": "as" @@ -517,15 +693,15 @@ "importKind": "typeof", "local": { "type": "Identifier", - "start": 162, - "end": 164, + "start": 231, + "end": 233, "loc": { "start": { - "line": 7, + "line": 9, "column": 15 }, "end": { - "line": 7, + "line": 9, "column": 17 }, "identifierName": "as" @@ -537,15 +713,15 @@ "importKind": "value", "source": { "type": "StringLiteral", - "start": 171, - "end": 176, + "start": 240, + "end": 245, "loc": { "start": { - "line": 7, + "line": 9, "column": 24 }, "end": { - "line": 7, + "line": 9, "column": 29 } }, @@ -558,44 +734,44 @@ }, { "type": "ImportDeclaration", - "start": 178, - "end": 212, + "start": 247, + "end": 281, "loc": { "start": { - "line": 8, + "line": 10, "column": 0 }, "end": { - "line": 8, + "line": 10, "column": 34 } }, "specifiers": [ { "type": "ImportSpecifier", - "start": 186, - "end": 199, + "start": 255, + "end": 268, "loc": { "start": { - "line": 8, + "line": 10, "column": 8 }, "end": { - "line": 8, + "line": 10, "column": 21 } }, "imported": { "type": "Identifier", - "start": 193, - "end": 194, + "start": 262, + "end": 263, "loc": { "start": { - "line": 8, + "line": 10, "column": 15 }, "end": { - "line": 8, + "line": 10, "column": 16 }, "identifierName": "t" @@ -605,15 +781,15 @@ "importKind": "typeof", "local": { "type": "Identifier", - "start": 198, - "end": 199, + "start": 267, + "end": 268, "loc": { "start": { - "line": 8, + "line": 10, "column": 20 }, "end": { - "line": 8, + "line": 10, "column": 21 }, "identifierName": "u" @@ -625,15 +801,103 @@ "importKind": "value", "source": { "type": "StringLiteral", - "start": 206, - "end": 211, + "start": 275, + "end": 280, "loc": { "start": { - "line": 8, + "line": 10, "column": 28 }, "end": { - "line": 8, + "line": 10, + "column": 33 + } + }, + "extra": { + "rawValue": "foo", + "raw": "\"foo\"" + }, + "value": "foo" + } + }, + { + "type": "ImportDeclaration", + "start": 282, + "end": 316, + "loc": { + "start": { + "line": 11, + "column": 0 + }, + "end": { + "line": 11, + "column": 34 + } + }, + "specifiers": [ + { + "type": "ImportSpecifier", + "start": 290, + "end": 303, + "loc": { + "start": { + "line": 11, + "column": 8 + }, + "end": { + "line": 11, + "column": 21 + } + }, + "imported": { + "type": "Identifier", + "start": 297, + "end": 303, + "loc": { + "start": { + "line": 11, + "column": 15 + }, + "end": { + "line": 11, + "column": 21 + }, + "identifierName": "switch" + }, + "name": "switch" + }, + "importKind": "typeof", + "local": { + "type": "Identifier", + "start": 297, + "end": 303, + "loc": { + "start": { + "line": 11, + "column": 15 + }, + "end": { + "line": 11, + "column": 21 + }, + "identifierName": "switch" + }, + "name": "switch" + } + } + ], + "importKind": "value", + "source": { + "type": "StringLiteral", + "start": 310, + "end": 315, + "loc": { + "start": { + "line": 11, + "column": 28 + }, + "end": { + "line": 11, "column": 33 } }, diff --git a/test/fixtures/flow/type-imports/import-type/actual.js b/test/fixtures/flow/type-imports/import-type/actual.js index 7dae1e81dc..061648d235 100644 --- a/test/fixtures/flow/type-imports/import-type/actual.js +++ b/test/fixtures/flow/type-imports/import-type/actual.js @@ -1,3 +1,9 @@ -import type Def from "M"; -import type {named} from "M"; -import type Def, {named} from "M"; +import type Def from "foo"; +import type {named} from "foo"; +import type Def, {named} from "foo"; +import type switch from "foo"; +import type { switch } from "foo"; +import typeof switch from "foo"; +import typeof { switch } from "foo"; +import type * as ns from "foo"; +import type * as switch from "foo"; diff --git a/test/fixtures/flow/type-imports/import-type/expected.json b/test/fixtures/flow/type-imports/import-type/expected.json index af41bafe4d..b455ce4bba 100644 --- a/test/fixtures/flow/type-imports/import-type/expected.json +++ b/test/fixtures/flow/type-imports/import-type/expected.json @@ -1,29 +1,29 @@ { "type": "File", "start": 0, - "end": 90, + "end": 300, "loc": { "start": { "line": 1, "column": 0 }, "end": { - "line": 3, - "column": 34 + "line": 9, + "column": 35 } }, "program": { "type": "Program", "start": 0, - "end": 90, + "end": 300, "loc": { "start": { "line": 1, "column": 0 }, "end": { - "line": 3, - "column": 34 + "line": 9, + "column": 35 } }, "sourceType": "module", @@ -31,7 +31,7 @@ { "type": "ImportDeclaration", "start": 0, - "end": 25, + "end": 27, "loc": { "start": { "line": 1, @@ -39,7 +39,7 @@ }, "end": { "line": 1, - "column": 25 + "column": 27 } }, "specifiers": [ @@ -80,7 +80,7 @@ "source": { "type": "StringLiteral", "start": 21, - "end": 24, + "end": 26, "loc": { "start": { "line": 1, @@ -88,20 +88,20 @@ }, "end": { "line": 1, - "column": 24 + "column": 26 } }, "extra": { - "rawValue": "M", - "raw": "\"M\"" + "rawValue": "foo", + "raw": "\"foo\"" }, - "value": "M" + "value": "foo" } }, { "type": "ImportDeclaration", - "start": 26, - "end": 55, + "start": 28, + "end": 59, "loc": { "start": { "line": 2, @@ -109,14 +109,14 @@ }, "end": { "line": 2, - "column": 29 + "column": 31 } }, "specifiers": [ { "type": "ImportSpecifier", - "start": 39, - "end": 44, + "start": 41, + "end": 46, "loc": { "start": { "line": 2, @@ -129,8 +129,8 @@ }, "imported": { "type": "Identifier", - "start": 39, - "end": 44, + "start": 41, + "end": 46, "loc": { "start": { "line": 2, @@ -147,8 +147,8 @@ "importKind": null, "local": { "type": "Identifier", - "start": 39, - "end": 44, + "start": 41, + "end": 46, "loc": { "start": { "line": 2, @@ -167,8 +167,8 @@ "importKind": "type", "source": { "type": "StringLiteral", - "start": 51, - "end": 54, + "start": 53, + "end": 58, "loc": { "start": { "line": 2, @@ -176,20 +176,20 @@ }, "end": { "line": 2, - "column": 28 + "column": 30 } }, "extra": { - "rawValue": "M", - "raw": "\"M\"" + "rawValue": "foo", + "raw": "\"foo\"" }, - "value": "M" + "value": "foo" } }, { "type": "ImportDeclaration", - "start": 56, - "end": 90, + "start": 60, + "end": 96, "loc": { "start": { "line": 3, @@ -197,14 +197,14 @@ }, "end": { "line": 3, - "column": 34 + "column": 36 } }, "specifiers": [ { "type": "ImportDefaultSpecifier", - "start": 68, - "end": 71, + "start": 72, + "end": 75, "loc": { "start": { "line": 3, @@ -217,8 +217,8 @@ }, "local": { "type": "Identifier", - "start": 68, - "end": 71, + "start": 72, + "end": 75, "loc": { "start": { "line": 3, @@ -235,8 +235,8 @@ }, { "type": "ImportSpecifier", - "start": 74, - "end": 79, + "start": 78, + "end": 83, "loc": { "start": { "line": 3, @@ -249,8 +249,8 @@ }, "imported": { "type": "Identifier", - "start": 74, - "end": 79, + "start": 78, + "end": 83, "loc": { "start": { "line": 3, @@ -267,8 +267,8 @@ "importKind": null, "local": { "type": "Identifier", - "start": 74, - "end": 79, + "start": 78, + "end": 83, "loc": { "start": { "line": 3, @@ -287,8 +287,8 @@ "importKind": "type", "source": { "type": "StringLiteral", - "start": 86, - "end": 89, + "start": 90, + "end": 95, "loc": { "start": { "line": 3, @@ -296,14 +296,470 @@ }, "end": { "line": 3, + "column": 35 + } + }, + "extra": { + "rawValue": "foo", + "raw": "\"foo\"" + }, + "value": "foo" + } + }, + { + "type": "ImportDeclaration", + "start": 97, + "end": 127, + "loc": { + "start": { + "line": 4, + "column": 0 + }, + "end": { + "line": 4, + "column": 30 + } + }, + "specifiers": [ + { + "type": "ImportDefaultSpecifier", + "start": 109, + "end": 115, + "loc": { + "start": { + "line": 4, + "column": 12 + }, + "end": { + "line": 4, + "column": 18 + } + }, + "local": { + "type": "Identifier", + "start": 109, + "end": 115, + "loc": { + "start": { + "line": 4, + "column": 12 + }, + "end": { + "line": 4, + "column": 18 + }, + "identifierName": "switch" + }, + "name": "switch" + } + } + ], + "importKind": "type", + "source": { + "type": "StringLiteral", + "start": 121, + "end": 126, + "loc": { + "start": { + "line": 4, + "column": 24 + }, + "end": { + "line": 4, + "column": 29 + } + }, + "extra": { + "rawValue": "foo", + "raw": "\"foo\"" + }, + "value": "foo" + } + }, + { + "type": "ImportDeclaration", + "start": 128, + "end": 162, + "loc": { + "start": { + "line": 5, + "column": 0 + }, + "end": { + "line": 5, + "column": 34 + } + }, + "specifiers": [ + { + "type": "ImportSpecifier", + "start": 142, + "end": 148, + "loc": { + "start": { + "line": 5, + "column": 14 + }, + "end": { + "line": 5, + "column": 20 + } + }, + "imported": { + "type": "Identifier", + "start": 142, + "end": 148, + "loc": { + "start": { + "line": 5, + "column": 14 + }, + "end": { + "line": 5, + "column": 20 + }, + "identifierName": "switch" + }, + "name": "switch" + }, + "importKind": null, + "local": { + "type": "Identifier", + "start": 142, + "end": 148, + "loc": { + "start": { + "line": 5, + "column": 14 + }, + "end": { + "line": 5, + "column": 20 + }, + "identifierName": "switch" + }, + "name": "switch" + } + } + ], + "importKind": "type", + "source": { + "type": "StringLiteral", + "start": 156, + "end": 161, + "loc": { + "start": { + "line": 5, + "column": 28 + }, + "end": { + "line": 5, "column": 33 } }, "extra": { - "rawValue": "M", - "raw": "\"M\"" + "rawValue": "foo", + "raw": "\"foo\"" }, - "value": "M" + "value": "foo" + } + }, + { + "type": "ImportDeclaration", + "start": 163, + "end": 195, + "loc": { + "start": { + "line": 6, + "column": 0 + }, + "end": { + "line": 6, + "column": 32 + } + }, + "specifiers": [ + { + "type": "ImportDefaultSpecifier", + "start": 177, + "end": 183, + "loc": { + "start": { + "line": 6, + "column": 14 + }, + "end": { + "line": 6, + "column": 20 + } + }, + "local": { + "type": "Identifier", + "start": 177, + "end": 183, + "loc": { + "start": { + "line": 6, + "column": 14 + }, + "end": { + "line": 6, + "column": 20 + }, + "identifierName": "switch" + }, + "name": "switch" + } + } + ], + "importKind": "typeof", + "source": { + "type": "StringLiteral", + "start": 189, + "end": 194, + "loc": { + "start": { + "line": 6, + "column": 26 + }, + "end": { + "line": 6, + "column": 31 + } + }, + "extra": { + "rawValue": "foo", + "raw": "\"foo\"" + }, + "value": "foo" + } + }, + { + "type": "ImportDeclaration", + "start": 196, + "end": 232, + "loc": { + "start": { + "line": 7, + "column": 0 + }, + "end": { + "line": 7, + "column": 36 + } + }, + "specifiers": [ + { + "type": "ImportSpecifier", + "start": 212, + "end": 218, + "loc": { + "start": { + "line": 7, + "column": 16 + }, + "end": { + "line": 7, + "column": 22 + } + }, + "imported": { + "type": "Identifier", + "start": 212, + "end": 218, + "loc": { + "start": { + "line": 7, + "column": 16 + }, + "end": { + "line": 7, + "column": 22 + }, + "identifierName": "switch" + }, + "name": "switch" + }, + "importKind": null, + "local": { + "type": "Identifier", + "start": 212, + "end": 218, + "loc": { + "start": { + "line": 7, + "column": 16 + }, + "end": { + "line": 7, + "column": 22 + }, + "identifierName": "switch" + }, + "name": "switch" + } + } + ], + "importKind": "typeof", + "source": { + "type": "StringLiteral", + "start": 226, + "end": 231, + "loc": { + "start": { + "line": 7, + "column": 30 + }, + "end": { + "line": 7, + "column": 35 + } + }, + "extra": { + "rawValue": "foo", + "raw": "\"foo\"" + }, + "value": "foo" + } + }, + { + "type": "ImportDeclaration", + "start": 233, + "end": 264, + "loc": { + "start": { + "line": 8, + "column": 0 + }, + "end": { + "line": 8, + "column": 31 + } + }, + "specifiers": [ + { + "type": "ImportNamespaceSpecifier", + "start": 245, + "end": 252, + "loc": { + "start": { + "line": 8, + "column": 12 + }, + "end": { + "line": 8, + "column": 19 + } + }, + "local": { + "type": "Identifier", + "start": 250, + "end": 252, + "loc": { + "start": { + "line": 8, + "column": 17 + }, + "end": { + "line": 8, + "column": 19 + }, + "identifierName": "ns" + }, + "name": "ns" + } + } + ], + "importKind": "type", + "source": { + "type": "StringLiteral", + "start": 258, + "end": 263, + "loc": { + "start": { + "line": 8, + "column": 25 + }, + "end": { + "line": 8, + "column": 30 + } + }, + "extra": { + "rawValue": "foo", + "raw": "\"foo\"" + }, + "value": "foo" + } + }, + { + "type": "ImportDeclaration", + "start": 265, + "end": 300, + "loc": { + "start": { + "line": 9, + "column": 0 + }, + "end": { + "line": 9, + "column": 35 + } + }, + "specifiers": [ + { + "type": "ImportNamespaceSpecifier", + "start": 277, + "end": 288, + "loc": { + "start": { + "line": 9, + "column": 12 + }, + "end": { + "line": 9, + "column": 23 + } + }, + "local": { + "type": "Identifier", + "start": 282, + "end": 288, + "loc": { + "start": { + "line": 9, + "column": 17 + }, + "end": { + "line": 9, + "column": 23 + }, + "identifierName": "switch" + }, + "name": "switch" + } + } + ], + "importKind": "type", + "source": { + "type": "StringLiteral", + "start": 294, + "end": 299, + "loc": { + "start": { + "line": 9, + "column": 29 + }, + "end": { + "line": 9, + "column": 34 + } + }, + "extra": { + "rawValue": "foo", + "raw": "\"foo\"" + }, + "value": "foo" } } ], diff --git a/test/fixtures/flow/type-imports/invalid-import-type-2/actual.js b/test/fixtures/flow/type-imports/invalid-import-type-2/actual.js new file mode 100644 index 0000000000..12305eceeb --- /dev/null +++ b/test/fixtures/flow/type-imports/invalid-import-type-2/actual.js @@ -0,0 +1 @@ +import type { string } from "foo"; diff --git a/test/fixtures/flow/type-imports/invalid-import-type-2/options.json b/test/fixtures/flow/type-imports/invalid-import-type-2/options.json new file mode 100644 index 0000000000..7f4fe7d13e --- /dev/null +++ b/test/fixtures/flow/type-imports/invalid-import-type-2/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Cannot overwrite primitive type string (1:14)" +} diff --git a/test/fixtures/flow/type-imports/invalid-import-type-3/actual.js b/test/fixtures/flow/type-imports/invalid-import-type-3/actual.js new file mode 100644 index 0000000000..082b16ae25 --- /dev/null +++ b/test/fixtures/flow/type-imports/invalid-import-type-3/actual.js @@ -0,0 +1 @@ +import typeof string from "foo"; diff --git a/test/fixtures/flow/type-imports/invalid-import-type-3/options.json b/test/fixtures/flow/type-imports/invalid-import-type-3/options.json new file mode 100644 index 0000000000..7f4fe7d13e --- /dev/null +++ b/test/fixtures/flow/type-imports/invalid-import-type-3/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Cannot overwrite primitive type string (1:14)" +} diff --git a/test/fixtures/flow/type-imports/invalid-import-type-4/actual.js b/test/fixtures/flow/type-imports/invalid-import-type-4/actual.js new file mode 100644 index 0000000000..ff834e60b4 --- /dev/null +++ b/test/fixtures/flow/type-imports/invalid-import-type-4/actual.js @@ -0,0 +1 @@ +import typeof * as string from "foo"; diff --git a/test/fixtures/flow/type-imports/invalid-import-type-4/options.json b/test/fixtures/flow/type-imports/invalid-import-type-4/options.json new file mode 100644 index 0000000000..39481cdad4 --- /dev/null +++ b/test/fixtures/flow/type-imports/invalid-import-type-4/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Cannot overwrite primitive type string (1:19)" +} diff --git a/test/fixtures/flow/type-imports/invalid-import-type-shorthand-2/options.json b/test/fixtures/flow/type-imports/invalid-import-type-shorthand-2/options.json index fcf8bc0a65..036229c5b9 100644 --- a/test/fixtures/flow/type-imports/invalid-import-type-shorthand-2/options.json +++ b/test/fixtures/flow/type-imports/invalid-import-type-shorthand-2/options.json @@ -1,3 +1,3 @@ { - "throws": "`The `type` and `typeof` keywords on named imports can only be used on regular `import` statements. It cannot be used with `import type` or `import typeof` statements` (1:13)" + "throws": "The `type` and `typeof` keywords on named imports can only be used on regular `import` statements. It cannot be used with `import type` or `import typeof` statements (1:13)" } diff --git a/test/fixtures/flow/type-imports/invalid-import-type-shorthand-3/actual.js b/test/fixtures/flow/type-imports/invalid-import-type-shorthand-3/actual.js new file mode 100644 index 0000000000..4f663e19a8 --- /dev/null +++ b/test/fixtures/flow/type-imports/invalid-import-type-shorthand-3/actual.js @@ -0,0 +1 @@ +import { typeof string } from "foo"; diff --git a/test/fixtures/flow/type-imports/invalid-import-type-shorthand-3/options.json b/test/fixtures/flow/type-imports/invalid-import-type-shorthand-3/options.json new file mode 100644 index 0000000000..bb13e8023f --- /dev/null +++ b/test/fixtures/flow/type-imports/invalid-import-type-shorthand-3/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Cannot overwrite primitive type string (1:16)" +} diff --git a/test/fixtures/flow/type-imports/invalid-import-type-shorthand-4/actual.js b/test/fixtures/flow/type-imports/invalid-import-type-shorthand-4/actual.js new file mode 100644 index 0000000000..7c55e29c6f --- /dev/null +++ b/test/fixtures/flow/type-imports/invalid-import-type-shorthand-4/actual.js @@ -0,0 +1 @@ +import { type string } from "foo"; diff --git a/test/fixtures/flow/type-imports/invalid-import-type-shorthand-4/options.json b/test/fixtures/flow/type-imports/invalid-import-type-shorthand-4/options.json new file mode 100644 index 0000000000..7f4fe7d13e --- /dev/null +++ b/test/fixtures/flow/type-imports/invalid-import-type-shorthand-4/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Cannot overwrite primitive type string (1:14)" +} diff --git a/test/fixtures/flow/type-imports/invalid-import-type-shorthand/options.json b/test/fixtures/flow/type-imports/invalid-import-type-shorthand/options.json index d516d5fb7b..7aa4274c71 100644 --- a/test/fixtures/flow/type-imports/invalid-import-type-shorthand/options.json +++ b/test/fixtures/flow/type-imports/invalid-import-type-shorthand/options.json @@ -1,3 +1,3 @@ { - "throws": "`The `type` and `typeof` keywords on named imports can only be used on regular `import` statements. It cannot be used with `import type` or `import typeof` statements` (1:15)" + "throws": "The `type` and `typeof` keywords on named imports can only be used on regular `import` statements. It cannot be used with `import type` or `import typeof` statements (1:15)" } diff --git a/test/fixtures/flow/type-imports/invalid-import-type/actual.js b/test/fixtures/flow/type-imports/invalid-import-type/actual.js index b8ee08bd2d..154e8b8455 100644 --- a/test/fixtures/flow/type-imports/invalid-import-type/actual.js +++ b/test/fixtures/flow/type-imports/invalid-import-type/actual.js @@ -1 +1 @@ -import { type debugger } from "foo"; +import type string from "foo"; diff --git a/test/fixtures/flow/type-imports/invalid-import-type/options.json b/test/fixtures/flow/type-imports/invalid-import-type/options.json index 96dff35646..6bf62df4a9 100644 --- a/test/fixtures/flow/type-imports/invalid-import-type/options.json +++ b/test/fixtures/flow/type-imports/invalid-import-type/options.json @@ -1,3 +1,3 @@ { - "throws": "debugger is a reserved word (1:9)" + "throws": "Cannot overwrite primitive type string (1:12)" }