forked from palantir/tslint
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ordered imports grouping (palantir#4134)
- Loading branch information
Showing
21 changed files
with
524 additions
and
127 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,18 @@ | ||
#!/usr/bin/env node | ||
/* Test the case where import should be grouped using default grouping. */ | ||
|
||
import {bar} from '../bar'; | ||
|
||
import {foo, afoo} from 'foo'; | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Import sources of different groups must be sorted by: libraries, parent directories, current directory.] | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Imports from this module are not allowed in this group. The expected groups (in order) are: libraries, parent directories, current directory.] | ||
~~~~~~~~~ [Named imports must be alphabetized.] | ||
|
||
import './baz'; // required | ||
import './baa'; | ||
~~~~~~~~~~~~~~~ [Import sources within a group must be alphabetized.] | ||
|
||
import './caa'; | ||
~~~~~~~~~~~~~~~ [Import sources of the same type (package, same folder, different folder) must be grouped together.] | ||
|
||
import x = require('y'); | ||
~~~~~~~~~~~~~~~~~~~~~~~~ [Import sources of the same type (package, same folder, different folder) must be grouped together.] | ||
|
||
export class Test {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,10 @@ | ||
{ | ||
"rules": { | ||
"ordered-imports": [true, {"import-sources-order": "case-insensitive", "named-imports-order": "case-insensitive", "grouped-imports": true}] | ||
"ordered-imports": [ | ||
true, { | ||
"import-sources-order": "case-insensitive", | ||
"named-imports-order": "case-insensitive", | ||
"grouped-imports": true | ||
}] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#!/usr/bin/env node | ||
/* Test a more complex set of split up groups. */ | ||
|
||
// comment outside of imports | ||
import {afoo, foo} from 'foo'; | ||
import x = require('y'); | ||
|
||
import {app_b} from 'app/bar'; | ||
import {app_f} from 'app/foo'; | ||
|
||
// comment pkg/bar | ||
import {a} from '@pkg/bar'; | ||
import {x} from '@pkg/foo'; | ||
|
||
import './baa'; | ||
import './baz'; // required | ||
|
||
import {bar} from '../bar'; | ||
import {xbar} from '../xbar'; | ||
|
||
|
||
export class Test {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#!/usr/bin/env node | ||
/* Test a more complex set of split up groups. */ | ||
|
||
// comment outside of imports | ||
import {app_f} from 'app/foo'; | ||
import {x} from '@pkg/foo'; | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Imports from this module are not allowed in this group. The expected groups (in order) are: extra, /^app/, /^@pkg/, current dir, parent_dir.] | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Import sources within a group must be alphabetized.] | ||
|
||
import {app_b} from 'app/bar'; | ||
// comment pkg/bar | ||
import {a} from '@pkg/bar'; | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Import sources within a group must be alphabetized.] | ||
|
||
import {xbar} from '../xbar'; | ||
|
||
import {bar} from '../bar'; | ||
|
||
import {foo, afoo} from 'foo'; | ||
~~~~~~~~~ [Named imports must be alphabetized.] | ||
|
||
import x = require('y'); | ||
|
||
import './baz'; // required | ||
import './baa'; | ||
~~~~~~~~~~~~~~~ [Import sources within a group must be alphabetized.] | ||
|
||
|
||
export class Test {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"rules": { | ||
"ordered-imports": [ | ||
true, | ||
{ | ||
"import-sources-order": "case-insensitive", | ||
"named-imports-order": "case-insensitive", | ||
"grouped-imports": true, | ||
"groups": [ | ||
{ "match": "^app", "order": 20 }, | ||
{ "match": "^@pkg", "order": 30 }, | ||
{ "name": "parent_dir", "match": "^\\.\\.", "order": 50 }, | ||
{ "name": "current dir", "match": "^\\.", "order": 40 }, | ||
{ "name": "extra", "match": ".*", "order": 5 } | ||
] | ||
} | ||
] | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
test/rules/ordered-imports/groups-shared-order/test.ts.fix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#!/usr/bin/env node | ||
/* Test the case where multiple matches have the same order value. */ | ||
|
||
import {y} from 'app_a/bar'; | ||
import {x} from 'app_a/foo'; | ||
import {f} from 'app_b/foo'; | ||
|
||
import {b} from '@pkg/bar'; | ||
import {a} from '@pkg/foo'; | ||
|
||
import {o} from 'other/bar'; | ||
|
||
export class Test {} |
18 changes: 18 additions & 0 deletions
18
test/rules/ordered-imports/groups-shared-order/test.ts.lint
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#!/usr/bin/env node | ||
/* Test the case where multiple matches have the same order value. */ | ||
|
||
import {f} from 'app_b/foo'; | ||
import {x} from 'app_a/foo'; | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Import sources within a group must be alphabetized.] | ||
import {y} from 'app_a/bar'; | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Import sources within a group must be alphabetized.] | ||
|
||
|
||
import {a} from '@pkg/foo'; | ||
|
||
import {b} from '@pkg/bar'; | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Imports from this module are not allowed in this group. The expected groups (in order) are: /^app_b/, /^app_a/, /^@pkg/.] | ||
|
||
import {o} from 'other/bar'; | ||
|
||
export class Test {} |
17 changes: 17 additions & 0 deletions
17
test/rules/ordered-imports/groups-shared-order/tslint.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"rules": { | ||
"ordered-imports": [ | ||
true, | ||
{ | ||
"import-sources-order": "case-insensitive", | ||
"named-imports-order": "case-insensitive", | ||
"grouped-imports": true, | ||
"groups": [ | ||
{ "match": "^app_b", "order": 10 }, | ||
{ "match": "^app_a", "order": 10 }, | ||
{ "match": "^@pkg", "order": 20 } | ||
] | ||
} | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#!/usr/bin/env node | ||
/* Test import grouping where only a list or regex strings is passed to config. */ | ||
|
||
import { app_b } from "app/bar"; | ||
import { app_f } from "app/foo"; | ||
|
||
import { a } from "@pkg/bar"; | ||
import { x } from "@pkg/foo"; | ||
|
||
import { bar } from "../bar"; | ||
import { xbar } from "../xbar"; | ||
|
||
import "./baa"; | ||
import "./baz"; | ||
|
||
import { foo } from "foo"; | ||
|
||
export class Test {} |
20 changes: 20 additions & 0 deletions
20
test/rules/ordered-imports/groups-string-list/test.ts.lint
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#!/usr/bin/env node | ||
/* Test import grouping where only a list or regex strings is passed to config. */ | ||
|
||
import { x } from "@pkg/foo"; | ||
import { app_f } from "app/foo"; | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Imports from this module are not allowed in this group. The expected groups (in order) are: /^app/, /^@pkg/, /^\.\./, /^\./.] | ||
|
||
import { a } from "@pkg/bar"; | ||
import { app_b } from "app/bar"; | ||
|
||
import { xbar } from "../xbar"; | ||
|
||
import { bar } from "../bar"; | ||
|
||
import { foo } from "foo"; | ||
|
||
import "./baa"; | ||
import "./baz"; | ||
|
||
export class Test {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"rules": { | ||
"ordered-imports": [ | ||
true, | ||
{ | ||
"import-sources-order": "case-insensitive", | ||
"named-imports-order": "case-insensitive", | ||
"grouped-imports": true, | ||
"groups": ["^app", "^@pkg", "^\\.\\.", "^\\."] | ||
} | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#!/usr/bin/env node | ||
/* Test to check that all unmatched imports end up in unmatched group at end.*/ | ||
|
||
import {app_b} from 'app/bar'; | ||
import {app_f} from 'app/foo'; | ||
|
||
import {a} from '@pkg/bar'; | ||
import {x} from '@pkg/foo'; | ||
|
||
import {foo} from 'foo'; | ||
import {xbar} from '../xbar'; | ||
import {b} from './ybar'; | ||
|
||
export class Test {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#!/usr/bin/env node | ||
/* Test to check that all unmatched imports end up in unmatched group at end.*/ | ||
|
||
import {app_f} from 'app/foo'; | ||
|
||
import {x} from '@pkg/foo'; | ||
|
||
import {app_b} from 'app/bar'; | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Imports from this module are not allowed in this group. The expected groups (in order) are: /^app/, /^@pkg/.] | ||
|
||
import {a} from '@pkg/bar'; | ||
|
||
import {xbar} from '../xbar'; | ||
|
||
import {foo} from 'foo'; | ||
import {b} from './ybar'; | ||
|
||
export class Test {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"rules": { | ||
"ordered-imports": [ | ||
true, | ||
{ | ||
"import-sources-order": "case-insensitive", | ||
"named-imports-order": "case-insensitive", | ||
"grouped-imports": true, | ||
"groups": [{ "match": "^app", "order": 20 }, { "match": "^@pkg", "order": 30 }] | ||
} | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,3 @@ import { getOr } from 'lodash/fp' | |
import { Request } from 'express' | ||
|
||
import { getStatusCode } from './errors' | ||
|
6 changes: 1 addition & 5 deletions
6
test/rules/ordered-imports/import-sources-any/grouped-imports/test.ts.lint
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,5 @@ | ||
import { getOr } from 'lodash/fp' | ||
|
||
import { Request } from 'express' | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [1] | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Imports from this module are not allowed in this group. The expected groups (in order) are: libraries, parent directories, current directory.] | ||
import { getStatusCode } from './errors' | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [grouped-imports] | ||
|
||
[grouped-imports]: Import sources of different groups must be sorted by: libraries, parent directories, current directory. | ||
[1]: Import sources of the same type (package, same folder, different folder) must be grouped together. |
10 changes: 10 additions & 0 deletions
10
test/rules/ordered-imports/standalone-grouped-import/test.ts.fix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#!/usr/bin/env node | ||
|
||
import {a} from 'foo'; | ||
import x = require('y'); | ||
|
||
import './baa'; | ||
import './baz'; | ||
import './caa'; | ||
|
||
export class Test {} |
13 changes: 13 additions & 0 deletions
13
test/rules/ordered-imports/standalone-grouped-import/test.ts.lint
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#!/usr/bin/env node | ||
|
||
import {a} from 'foo'; | ||
|
||
import './baa'; | ||
import './baz'; | ||
|
||
import './caa'; | ||
~~~~~~~~~~~~~~~ [Imports from this module are not allowed in this group. The expected groups (in order) are: libraries, parent directories, current directory.] | ||
|
||
import x = require('y'); | ||
|
||
export class Test {} |
10 changes: 10 additions & 0 deletions
10
test/rules/ordered-imports/standalone-grouped-import/tslint.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"rules": { | ||
"ordered-imports": [ | ||
true, { | ||
"import-sources-order": "case-insensitive", | ||
"named-imports-order": "case-insensitive", | ||
"grouped-imports": true | ||
}] | ||
} | ||
} |