Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Change normalisation of ordered-imports #4064

Merged
merged 2 commits into from
Oct 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions src/rules/orderedImportsRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {
isStringLiteral,
} from "tsutils";
import * as ts from "typescript";

import * as Lint from "../index";

export class Rule extends Lint.Rules.AbstractRule {
Expand All @@ -51,6 +50,7 @@ export class Rule extends Lint.Rules.AbstractRule {
Possible values for \`"import-sources-order"\` are:

* \`"case-insensitive'\`: Correct order is \`"Bar"\`, \`"baz"\`, \`"Foo"\`. (This is the default.)
* \`"case-insensitive-legacy'\`: Correct order is \`"Bar"\`, \`"baz"\`, \`"Foo"\`.
* \`"lowercase-first"\`: Correct order is \`"baz"\`, \`"Bar"\`, \`"Foo"\`.
* \`"lowercase-last"\`: Correct order is \`"Bar"\`, \`"Foo"\`, \`"baz"\`.
* \`"any"\`: Allow any order.
Expand All @@ -69,6 +69,7 @@ export class Rule extends Lint.Rules.AbstractRule {
Possible values for \`"named-imports-order"\` are:

* \`"case-insensitive'\`: Correct order is \`{A, b, C}\`. (This is the default.)
* \`"case-insensitive-legacy'\`: Correct order is \`"Bar"\`, \`"baz"\`, \`"Foo"\`.
* \`"lowercase-first"\`: Correct order is \`{b, A, C}\`.
* \`"lowercase-last"\`: Correct order is \`{A, C, b}\`.
* \`"any"\`: Allow any order.
Expand All @@ -90,11 +91,23 @@ export class Rule extends Lint.Rules.AbstractRule {
},
"import-sources-order": {
type: "string",
enum: ["case-insensitive", "lowercase-first", "lowercase-last", "any"],
enum: [
"case-insensitive",
"case-insensitive-legacy",
"lowercase-first",
"lowercase-last",
"any",
],
},
"named-imports-order": {
type: "string",
enum: ["case-insensitive", "lowercase-first", "lowercase-last", "any"],
enum: [
"case-insensitive",
"case-insensitive-legacy",
"lowercase-first",
"lowercase-last",
"any",
],
},
"module-source-path": {
type: "string",
Expand Down Expand Up @@ -135,7 +148,8 @@ export class Rule extends Lint.Rules.AbstractRule {
type Transform = (x: string) => string;
const TRANSFORMS = new Map<string, Transform>([
["any", () => ""],
["case-insensitive", x => x.toLowerCase()],
["case-insensitive", x => x.toUpperCase()],
["case-insensitive-legacy", x => x.toLowerCase()],
["lowercase-first", flipCase],
["lowercase-last", x => x],
["full", x => x],
Expand Down
48 changes: 48 additions & 0 deletions test/rules/ordered-imports/case-insensitive-legacy/test.ts.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Named imports should be alphabetized.
import {A, B} from 'foo';
import {A, B} from 'foo'; // failure

// Case is irrelevant for named import ordering.
import {A, bz, C} from 'foo'; // failure
import {A, b, C} from 'zfoo';

// Underscores come first.
import {_b, A, C, d} from 'zfoo'; // failure
import {_b, A, C, d} from 'zfoo';

import {g} from "y"; // failure
import {
a as d,
b as c,
} from "z";

// Import sources should be alphabetized.
import * as bar from 'bar';
import * as foo from 'foo';

import * as abc from 'abc';
import * as bar from 'bar'; // failure
import * as foo from 'foo';

// ignore quotes
import * as bar from 'bar';
import * as foo from "foo";

import * as bar from "bar";
import * as foo from 'foo';

// Case is irrelevant for source import ordering.
import {A, B} from 'Bar';
import {A, B} from 'baz';
import {A, B} from 'Foo'; // should not fail

// Other styles of import statements.
import someDefault from "module";
import "something";
import someDefault, {nameA, nameBReallyLong as anotherName} from "./wherever";

// do not fix cases where a newline is missing
import * as foo from 'foo'; import * as bar from 'bar';

import * as bar from 'bar';
import * as foo from 'foo';
61 changes: 61 additions & 0 deletions test/rules/ordered-imports/case-insensitive-legacy/test.ts.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Named imports should be alphabetized.
import {A, B} from 'foo';
import {B, A} from 'foo'; // failure
~~~~ [ordered-imports]

// Case is irrelevant for named import ordering.
import {A, b, C} from 'zfoo';
import {bz, A, C} from 'foo'; // failure
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Import sources within a group must be alphabetized.]
~~~~~ [Named imports must be alphabetized.]

// Underscores come first.
import {A, _b, C, d} from 'zfoo'; // failure
~~~~~ [Named imports must be alphabetized.]
import {_b, A, C, d} from 'zfoo';

import {
b as c,
~~~~~~~
a as d,
~~~~~~~~~~ [Named imports must be alphabetized.]
} from "z";
import {g} from "y"; // failure
~~~~~~~~~~~~~~~~~~~~ [Import sources within a group must be alphabetized.]

// Import sources should be alphabetized.
import * as bar from 'bar';
import * as foo from 'foo';

import * as abc from 'abc';
import * as foo from 'foo';
import * as bar from 'bar'; // failure
~~~~~~~~~~~~~~~~~~~~~~~~~~~ [ordered-sources]

// ignore quotes
import * as bar from 'bar';
import * as foo from "foo";

import * as bar from "bar";
import * as foo from 'foo';

// Case is irrelevant for source import ordering.
import {A, B} from 'Bar';
import {A, B} from 'baz';
import {A, B} from 'Foo'; // should not fail

// Other styles of import statements.
import someDefault, {nameA, nameBReallyLong as anotherName} from "./wherever";
import someDefault from "module";
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Import sources within a group must be alphabetized.]
import "something";

// do not fix cases where a newline is missing
import * as foo from 'foo'; import * as bar from 'bar';
~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Import sources within a group must be alphabetized.]

import * as foo from 'foo';
import * as bar from 'bar';
~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Import sources within a group must be alphabetized.]
[ordered-imports]: Named imports must be alphabetized.
[ordered-sources]: Import sources within a group must be alphabetized.
11 changes: 11 additions & 0 deletions test/rules/ordered-imports/case-insensitive-legacy/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"rules": {
"ordered-imports": [
true,
{
"import-sources-order": "case-insensitive-legacy",
"named-imports-order": "case-insensitive-legacy"
}
]
}
}
6 changes: 5 additions & 1 deletion test/rules/ordered-imports/case-insensitive/test.ts.fix
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import {A, B} from 'foo'; // failure
import {A, bz, C} from 'foo'; // failure
import {A, b, C} from 'zfoo';

// Underscores come last.
import {A, C, d, _b} from 'zfoo'; // failure
import {A, C, d, _b} from 'zfoo';

import {g} from "y"; // failure
import {
a as d,
Expand Down Expand Up @@ -40,5 +44,5 @@ import someDefault, {nameA, nameBReallyLong as anotherName} from "./wherever";
// do not fix cases where a newline is missing
import * as foo from 'foo'; import * as bar from 'bar';

import * as bar from 'bar';
import * as foo from 'foo';
import * as bar from 'bar';
7 changes: 6 additions & 1 deletion test/rules/ordered-imports/case-insensitive/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import {bz, A, C} from 'foo'; // failure
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Import sources within a group must be alphabetized.]
~~~~~ [Named imports must be alphabetized.]

// Underscores come last.
import {A, _b, C, d} from 'zfoo'; // failure
~~~~~ [Named imports must be alphabetized.]
import {A, C, d, _b} from 'zfoo';

import {
b as c,
~~~~~~~
Expand Down Expand Up @@ -53,4 +58,4 @@ import * as foo from 'foo';
import * as bar from 'bar';
~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Import sources within a group must be alphabetized.]
[ordered-imports]: Named imports must be alphabetized.
[ordered-sources]: Import sources within a group must be alphabetized.
[ordered-sources]: Import sources within a group must be alphabetized.