From c0ec9f3a0512edc572f73b5b73bf8e37165c45d2 Mon Sep 17 00:00:00 2001 From: Andrew Boyton Date: Wed, 10 Oct 2018 07:55:26 +1100 Subject: [PATCH] Add a legacy option for ordered imports Not sure this is necessary since we have a `--fix` but if people really want the old ordering they now can. --- src/rules/orderedImportsRule.ts | 19 +++++- .../case-insensitive-legacy/test.ts.fix | 48 +++++++++++++++ .../case-insensitive-legacy/test.ts.lint | 61 +++++++++++++++++++ .../case-insensitive-legacy/tslint.json | 11 ++++ .../case-insensitive/test.ts.fix | 6 +- .../case-insensitive/test.ts.lint | 6 +- 6 files changed, 143 insertions(+), 8 deletions(-) create mode 100644 test/rules/ordered-imports/case-insensitive-legacy/test.ts.fix create mode 100644 test/rules/ordered-imports/case-insensitive-legacy/test.ts.lint create mode 100644 test/rules/ordered-imports/case-insensitive-legacy/tslint.json diff --git a/src/rules/orderedImportsRule.ts b/src/rules/orderedImportsRule.ts index fbd42a99fc1..069a7a3f364 100644 --- a/src/rules/orderedImportsRule.ts +++ b/src/rules/orderedImportsRule.ts @@ -50,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. @@ -68,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. @@ -89,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", @@ -135,6 +149,7 @@ type Transform = (x: string) => string; const TRANSFORMS = new Map([ ["any", () => ""], ["case-insensitive", x => x.toUpperCase()], + ["case-insensitive-legacy", x => x.toLowerCase()], ["lowercase-first", flipCase], ["lowercase-last", x => x], ["full", x => x], diff --git a/test/rules/ordered-imports/case-insensitive-legacy/test.ts.fix b/test/rules/ordered-imports/case-insensitive-legacy/test.ts.fix new file mode 100644 index 00000000000..31b9a3fddf0 --- /dev/null +++ b/test/rules/ordered-imports/case-insensitive-legacy/test.ts.fix @@ -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'; diff --git a/test/rules/ordered-imports/case-insensitive-legacy/test.ts.lint b/test/rules/ordered-imports/case-insensitive-legacy/test.ts.lint new file mode 100644 index 00000000000..1ab7bbe5114 --- /dev/null +++ b/test/rules/ordered-imports/case-insensitive-legacy/test.ts.lint @@ -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. diff --git a/test/rules/ordered-imports/case-insensitive-legacy/tslint.json b/test/rules/ordered-imports/case-insensitive-legacy/tslint.json new file mode 100644 index 00000000000..611601029ac --- /dev/null +++ b/test/rules/ordered-imports/case-insensitive-legacy/tslint.json @@ -0,0 +1,11 @@ +{ + "rules": { + "ordered-imports": [ + true, + { + "import-sources-order": "case-insensitive-legacy", + "named-imports-order": "case-insensitive-legacy" + } + ] + } +} diff --git a/test/rules/ordered-imports/case-insensitive/test.ts.fix b/test/rules/ordered-imports/case-insensitive/test.ts.fix index 2bbfa13056a..11a5c889203 100644 --- a/test/rules/ordered-imports/case-insensitive/test.ts.fix +++ b/test/rules/ordered-imports/case-insensitive/test.ts.fix @@ -7,8 +7,8 @@ import {A, bz, C} from 'foo'; // failure import {A, b, C} from 'zfoo'; // Underscores come last. -import {A, C, _b} from 'zfoo'; // failure -import {A, C, _b} from 'zfoo'; +import {A, C, d, _b} from 'zfoo'; // failure +import {A, C, d, _b} from 'zfoo'; import {g} from "y"; // failure import { @@ -44,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'; \ No newline at end of file diff --git a/test/rules/ordered-imports/case-insensitive/test.ts.lint b/test/rules/ordered-imports/case-insensitive/test.ts.lint index b2f83c9ccb1..ee69fe602f0 100644 --- a/test/rules/ordered-imports/case-insensitive/test.ts.lint +++ b/test/rules/ordered-imports/case-insensitive/test.ts.lint @@ -10,9 +10,9 @@ import {bz, A, C} from 'foo'; // failure ~~~~~ [Named imports must be alphabetized.] // Underscores come last. -import {A, _b, C} from 'zfoo'; // failure +import {A, _b, C, d} from 'zfoo'; // failure ~~~~~ [Named imports must be alphabetized.] -import {A, C, _b} from 'zfoo'; +import {A, C, d, _b} from 'zfoo'; import { b as c, @@ -58,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. \ No newline at end of file +[ordered-sources]: Import sources within a group must be alphabetized.