diff --git a/src/rules/orderedImportsRule.ts b/src/rules/orderedImportsRule.ts index 9e2733c127d..069a7a3f364 100644 --- a/src/rules/orderedImportsRule.ts +++ b/src/rules/orderedImportsRule.ts @@ -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 { @@ -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. @@ -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. @@ -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", @@ -135,7 +148,8 @@ export class Rule extends Lint.Rules.AbstractRule { type Transform = (x: string) => string; const TRANSFORMS = new Map([ ["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], 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 fe75ec2d0f2..11a5c889203 100644 --- a/test/rules/ordered-imports/case-insensitive/test.ts.fix +++ b/test/rules/ordered-imports/case-insensitive/test.ts.fix @@ -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, @@ -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'; \ 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 441d926d90d..ee69fe602f0 100644 --- a/test/rules/ordered-imports/case-insensitive/test.ts.lint +++ b/test/rules/ordered-imports/case-insensitive/test.ts.lint @@ -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, ~~~~~~~ @@ -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. \ No newline at end of file +[ordered-sources]: Import sources within a group must be alphabetized.