diff --git a/README.md b/README.md index acb9276..4e89a96 100644 --- a/README.md +++ b/README.md @@ -248,7 +248,7 @@ addition to the alphabetical rule: `"."` and `".."` are treated as `"./"` and `"../"`. If both `import type` _and_ regular imports are used for the same source, the -[Flow type imports] come first. +type imports come first. Example: @@ -281,7 +281,7 @@ import i from "./styles"; // Regardless of group, imported items are sorted like this: import { - // First, Flow type imports. + // First, type imports. type x, typeof y, // Numbers are sorted by their numeric value: diff --git a/examples/readme-order.js b/examples/readme-order.js index 8b62b54..5a528b9 100644 --- a/examples/readme-order.js +++ b/examples/readme-order.js @@ -25,7 +25,7 @@ import i from "./styles"; // Regardless of group, imported items are sorted like this: import { - // First, Flow type imports. + // First, type imports. type x, typeof y, // Then everything else, alphabetically: diff --git a/src/sort.js b/src/sort.js index bc14724..d0c5028 100644 --- a/src/sort.js +++ b/src/sort.js @@ -787,7 +787,7 @@ function sortImportItems(items) { // The `.source` has been slightly tweaked. To stay fully deterministic, // also sort on the original value. compare(itemA.source.originalSource, itemB.source.originalSource) || - // Then put Flow type imports before regular ones. + // Then put type imports before regular ones. compare(itemA.source.importKind, itemB.source.importKind) || // Keep the original order if the sources are the same. It's not worth // trying to compare anything else, and you can use `import/no-duplicates` @@ -799,7 +799,7 @@ function sortImportItems(items) { function sortSpecifierItems(items) { return items.slice().sort( (itemA, itemB) => - // Put Flow type imports before regular ones. + // Put type imports before regular ones. compare(getImportKind(itemA.node), getImportKind(itemB.node)) || // Then compare by name. compare(itemA.node.imported.name, itemB.node.imported.name) || @@ -839,7 +839,7 @@ function isImportSpecifier(node) { function isSideEffectImport(importNode, sourceCode) { return ( importNode.specifiers.length === 0 && - !importNode.importKind && + (!importNode.importKind || importNode.importKind === "value") && !isPunctuator(sourceCode.getFirstToken(importNode, { skip: 1 }), "{") ); } @@ -887,9 +887,9 @@ function getSource(importNode) { } function getImportKind(importNode) { - // Flow `type` and `typeof` imports. Default to "\uffff" to make regular - // imports come after the type imports. - return importNode.importKind || "\uffff"; + // `type` and `typeof` imports. Default to "value" (like TypeScript) to make + // regular imports come after the type imports. + return importNode.importKind || "value"; } // Like `Array.prototype.findIndex`, but searches from the end. diff --git a/test/__snapshots__/examples.test.js.snap b/test/__snapshots__/examples.test.js.snap index fc8b457..51ae463 100644 --- a/test/__snapshots__/examples.test.js.snap +++ b/test/__snapshots__/examples.test.js.snap @@ -295,7 +295,7 @@ import h from "./constants"; import i from "./styles"; // Regardless of group, imported items are sorted like this: import { - // First, Flow type imports. + // First, type imports. type x, typeof y, // Numbers are sorted by their numeric value: diff --git a/test/sort.test.js b/test/sort.test.js index a2e0853..5d329f6 100644 --- a/test/sort.test.js +++ b/test/sort.test.js @@ -1814,13 +1814,16 @@ const typescriptTests = { code: input` |import React from "react"; |import Button from "../Button"; + |import type {target, type as tipe, Button} from "../Button"; | |import styles from "./styles.css"; |import { getUser } from "../../api"; | |import PropTypes from "prop-types"; + |import { /* X */ } from "prop-types"; |import classnames from "classnames"; |import { truncate, formatNumber } from "../../utils"; + |import type X from "../Button"; | |function pluck(o: T, names: K[]): T[K][] { | return names.map(n => o[n]); @@ -1830,10 +1833,13 @@ const typescriptTests = { expect(actual).toMatchInlineSnapshot(` |import classnames from "classnames"; |import PropTypes from "prop-types"; + |import { /* X */ } from "prop-types"; |import React from "react"; | |import { getUser } from "../../api"; |import { formatNumber,truncate } from "../../utils"; + |import type {Button,target, type as tipe} from "../Button"; + |import type X from "../Button"; |import Button from "../Button"; |import styles from "./styles.css"; |