Skip to content

Commit

Permalink
Add support for TypeScript 3.8
Browse files Browse the repository at this point in the history
Closes #35. Fixes #39.
  • Loading branch information
lydell committed Mar 11, 2020
1 parent f390620 commit 8bb04a0
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 10 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -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:

Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion examples/readme-order.js
Expand Up @@ -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:
Expand Down
12 changes: 6 additions & 6 deletions src/sort.js
Expand Up @@ -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`
Expand All @@ -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) ||
Expand Down Expand Up @@ -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 }), "{")
);
}
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion test/__snapshots__/examples.test.js.snap
Expand Up @@ -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:
Expand Down
6 changes: 6 additions & 0 deletions test/sort.test.js
Expand Up @@ -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<T, K extends keyof T>(o: T, names: K[]): T[K][] {
| return names.map(n => o[n]);
Expand All @@ -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";
|
Expand Down

0 comments on commit 8bb04a0

Please sign in to comment.