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

Commit

Permalink
sq: misc fixes and change to no null match.
Browse files Browse the repository at this point in the history
  • Loading branch information
abierbaum committed Aug 29, 2018
1 parent daab755 commit b19b7d2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 18 deletions.
30 changes: 14 additions & 16 deletions src/rules/orderedImportsRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ export class Rule extends Lint.Rules.AbstractRule {
- Import sources must be alphabetized within groups, i.e.:
import * as foo from "a";
import * as bar from "b";
- Groups of imports are delineated by blank lines. You can use these to group imports
however you like, e.g. by first- vs. third-party or thematically or can you can
enforce a group order and content.`,
- Groups of imports are delineated by blank lines. You can use this rule to group
imports however you like, e.g. by first- vs. third-party or thematically or
you can define groups based upon patterns in import path names.`,
hasFix: true,
optionsDescription: Lint.Utils.dedent`
You may set the \`"import-sources-order"\` option to control the ordering of source
Expand Down Expand Up @@ -78,10 +78,8 @@ export class Rule extends Lint.Rules.AbstractRule {
The first rule in the list to match a given import is the group that is used.
If no rule in matched then the import will be put in an \`unmatched\` group
at the end of all groups. One group rule can have a \`match\` value of \`null\`
which means to match anything that has not matched a previous rule. The groups
must be ordered based upon the sequential value of the \`order\` value.
(ie. order 0 is first)
at the end of all groups. The groups must be ordered based upon the sequential
value of the \`order\` value. (ie. order 0 is first)
If no \`"groups"\` options is set, a default grouping is used of third-party,
parent directories and the current directory. (\`"bar"\`, \`"../baz"\`, \`"./foo"\`.)
Expand Down Expand Up @@ -206,7 +204,7 @@ interface Options {

interface GroupOption {
name: string;
match: RegExp | null;
match: RegExp;
order: number;
}

Expand All @@ -220,7 +218,7 @@ interface JsonOptions {

interface JsonGroupOption {
name?: string;
match: string | null;
match: string;
order: number;
}

Expand All @@ -230,22 +228,22 @@ function parseOptions(ruleArguments: any[]): Options {
const defaultGroups: JsonGroupOption[] = [
{ name: "parent directories", match: "^[.][.]", order: 20 },
{ name: "current directory", match: "^[.]", order: 30 },
{ name: "libraries", match: null, order: 10 }
{ name: "libraries", match: ".*", order: 10 }
];

const {
"grouped-imports": isGrouped = false,
"import-sources-order": sources = "case-insensitive",
"named-imports-order": named = "case-insensitive",
"module-source-path": path = "full",
groups: groups = defaultGroups
groups = defaultGroups
} =
optionSet === undefined ? {} : optionSet;

// build list of compiled group rules
const compiledGroups = groups.map(g => ({
match: g.match === null ? null : new RegExp(g.match),
name: g.name !== undefined ? g.name : g.match !== null ? g.match : "default",
match: new RegExp(g.match),
name: g.name !== undefined ? g.name : g.match,
order: g.order
}));

Expand All @@ -265,7 +263,7 @@ class Walker extends Lint.AbstractWalker<Options> {

// group to use when no other group matches
private readonly defaultGroup: GroupOption = {
match: null,
match: /.*/,
name: "unmatched",
order: Number.MAX_SAFE_INTEGER
};
Expand Down Expand Up @@ -404,7 +402,7 @@ class Walker extends Lint.AbstractWalker<Options> {
}

/**
* Check all import blogs stopping at the first failure.
* Check all import blocks stopping at the first failure.
*/
private checkBlocksGrouping(): void {
let prevBlockOrder = Number.MIN_SAFE_INTEGER;
Expand Down Expand Up @@ -450,7 +448,7 @@ class Walker extends Lint.AbstractWalker<Options> {
private getMatchingGroup(importPath: string): GroupOption {
// find the first matching group.
for (const group of this.options.groups) {
if (group.match === null || group.match.test(importPath)) {
if (group.match.test(importPath)) {
return group;
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/rules/ordered-imports/groups-complex/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// comment outside of imports
import {app_f} from 'app/foo';
import {x} from '@pkg/foo';
~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Import sources of different groups must be sorted by: default, ^app, ^@pkg, current dir, parent_dir.]
~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Import sources of different groups must be sorted by: extra, ^app, ^@pkg, current dir, parent_dir.]
~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Import sources within a group must be alphabetized.]

import {app_b} from 'app/bar';
Expand Down
2 changes: 1 addition & 1 deletion test/rules/ordered-imports/groups-complex/tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
{ "match": "^@pkg", "order": 30 },
{ "name": "parent_dir", "match": "^[.][.]", "order": 50 },
{ "name": "current dir", "match": "^[.]", "order": 40 },
{ "match": null, "order": 5 }
{ "name": "extra", "match": ".*", "order": 5 }
]
}
]
Expand Down

0 comments on commit b19b7d2

Please sign in to comment.