Skip to content

Commit

Permalink
Ordered imports grouping (palantir#4134)
Browse files Browse the repository at this point in the history
  • Loading branch information
abierbaum committed Jan 29, 2019
1 parent 593dc94 commit 0143484
Show file tree
Hide file tree
Showing 21 changed files with 524 additions and 127 deletions.
384 changes: 267 additions & 117 deletions src/rules/orderedImportsRule.ts

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions test/rules/ordered-imports/grouped-imports/test.ts.fix
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env node
/* Test the case where import should be grouped using default grouping. */

import {afoo, foo} from 'foo';
import x = require('y');
Expand Down
5 changes: 2 additions & 3 deletions test/rules/ordered-imports/grouped-imports/test.ts.lint
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 {}
7 changes: 6 additions & 1 deletion test/rules/ordered-imports/grouped-imports/tslint.json
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
}]
}
}
22 changes: 22 additions & 0 deletions test/rules/ordered-imports/groups-complex/test.ts.fix
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 {}
29 changes: 29 additions & 0 deletions test/rules/ordered-imports/groups-complex/test.ts.lint
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 {}
19 changes: 19 additions & 0 deletions test/rules/ordered-imports/groups-complex/tslint.json
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 test/rules/ordered-imports/groups-shared-order/test.ts.fix
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 test/rules/ordered-imports/groups-shared-order/test.ts.lint
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 test/rules/ordered-imports/groups-shared-order/tslint.json
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 }
]
}
]
}
}
18 changes: 18 additions & 0 deletions test/rules/ordered-imports/groups-string-list/test.ts.fix
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 test/rules/ordered-imports/groups-string-list/test.ts.lint
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 {}
13 changes: 13 additions & 0 deletions test/rules/ordered-imports/groups-string-list/tslint.json
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", "^\\.\\.", "^\\."]
}
]
}
}
14 changes: 14 additions & 0 deletions test/rules/ordered-imports/groups-unmatched/test.ts.fix
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 {}
18 changes: 18 additions & 0 deletions test/rules/ordered-imports/groups-unmatched/test.ts.lint
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 {}
13 changes: 13 additions & 0 deletions test/rules/ordered-imports/groups-unmatched/tslint.json
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 }]
}
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ import { getOr } from 'lodash/fp'
import { Request } from 'express'

import { getStatusCode } from './errors'

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 test/rules/ordered-imports/standalone-grouped-import/test.ts.fix
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 test/rules/ordered-imports/standalone-grouped-import/test.ts.lint
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 test/rules/ordered-imports/standalone-grouped-import/tslint.json
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
}]
}
}

0 comments on commit 0143484

Please sign in to comment.