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

Commit

Permalink
Rerun latest prettier.
Browse files Browse the repository at this point in the history
  • Loading branch information
abierbaum committed Nov 7, 2018
1 parent c3630db commit 724d46d
Showing 1 changed file with 37 additions and 38 deletions.
75 changes: 37 additions & 38 deletions src/rules/orderedImportsRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
isImportEqualsDeclaration,
isModuleDeclaration,
isNamedImports,
isStringLiteral
isStringLiteral,
} from "tsutils";
import * as ts from "typescript";

Expand Down Expand Up @@ -112,54 +112,54 @@ export class Rule extends Lint.Rules.AbstractRule {
type: "object",
properties: {
"grouped-imports": {
type: "boolean"
type: "boolean",
},
groups: {
type: "list",
listType: {
oneOf: [
{
type: "string"
type: "string",
},
{
type: "object",
properties: {
name: { type: "string" },
match: { type: "string" },
order: { type: "number" }
order: { type: "number" },
},
required: ["match", "order"]
}
]
}
required: ["match", "order"],
},
],
},
},
"import-sources-order": {
type: "string",
enum: ["case-insensitive", "lowercase-first", "lowercase-last", "any"]
enum: ["case-insensitive", "lowercase-first", "lowercase-last", "any"],
},
"named-imports-order": {
type: "string",
enum: ["case-insensitive", "lowercase-first", "lowercase-last", "any"]
enum: ["case-insensitive", "lowercase-first", "lowercase-last", "any"],
},
"module-source-path": {
type: "string",
enum: ["full", "basename"]
}
enum: ["full", "basename"],
},
},
additionalProperties: false
additionalProperties: false,
},
optionExamples: [
true,
[
true,
{
"import-sources-order": "lowercase-last",
"named-imports-order": "lowercase-first"
}
]
"named-imports-order": "lowercase-first",
},
],
],
type: "style",
typescriptOnly: false
typescriptOnly: false,
};
/* tslint:enable:object-literal-sort-keys */

Expand All @@ -170,7 +170,7 @@ export class Rule extends Lint.Rules.AbstractRule {

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(
new Walker(sourceFile, this.ruleName, parseOptions(this.ruleArguments))
new Walker(sourceFile, this.ruleName, parseOptions(this.ruleArguments)),
);
}
}
Expand All @@ -196,14 +196,14 @@ const TRANSFORMS = new Map<string, Transform>([
return x;
}
return x.substr(splitIndex + 1);
}
]
},
],
]);

enum ImportType {
LIBRARY_IMPORT = 1,
PARENT_DIRECTORY_IMPORT = 2, // starts with "../"
CURRENT_DIRECTORY_IMPORT = 3 // starts with "./"
CURRENT_DIRECTORY_IMPORT = 3, // starts with "./"
}

interface Options {
Expand Down Expand Up @@ -240,17 +240,16 @@ function parseOptions(ruleArguments: any[]): Options {
const defaultGroups: JsonGroupOption[] = [
{ name: "parent directories", match: "^\\.\\.", order: 20 },
{ name: "current directory", match: "^\\.", order: 30 },
{ name: "libraries", match: ".*", 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 = defaultGroups
} =
optionSet === undefined ? {} : optionSet;
groups = defaultGroups,
} = optionSet === undefined ? {} : optionSet;

// build up list of compiled groups
// - handle case where "group" is just a string pattern
Expand All @@ -262,7 +261,7 @@ function parseOptions(ruleArguments: any[]): Options {
return {
match: new RegExp(g.match),
name: g.name !== undefined ? g.name : g.match,
order: g.order
order: g.order,
};
}
});
Expand All @@ -272,7 +271,7 @@ function parseOptions(ruleArguments: any[]): Options {
groups: compiledGroups,
importSourcesOrderTransform: TRANSFORMS.get(sources)!,
moduleSourceTransform: TRANSFORMS.get(path)!,
namedImportsOrderTransform: TRANSFORMS.get(named)!
namedImportsOrderTransform: TRANSFORMS.get(named)!,
};
}

Expand All @@ -285,7 +284,7 @@ class Walker extends Lint.AbstractWalker<Options> {
private readonly defaultGroup: GroupOption = {
match: /.*/,
name: "unmatched",
order: Number.MAX_SAFE_INTEGER
order: Number.MAX_SAFE_INTEGER,
};

private get currentImportsBlock(): ImportsBlock {
Expand All @@ -308,8 +307,8 @@ class Walker extends Lint.AbstractWalker<Options> {
/\r?\n\r?\n/.test(
this.sourceFile.text.slice(
statement.getFullStart(),
statement.getStart(this.sourceFile)
)
statement.getStart(this.sourceFile),
),
)
) {
this.endBlock();
Expand Down Expand Up @@ -337,7 +336,7 @@ class Walker extends Lint.AbstractWalker<Options> {
}

const source = this.options.importSourcesOrderTransform(
removeQuotes(node.moduleSpecifier.text)
removeQuotes(node.moduleSpecifier.text),
);
this.checkSource(source, node);

Expand Down Expand Up @@ -378,7 +377,7 @@ class Walker extends Lint.AbstractWalker<Options> {
this.sourceFile,
node,
currentSource,
matchingGroup
matchingGroup,
);

if (previousSource !== null && compare(currentSource, previousSource) === -1) {
Expand All @@ -405,7 +404,7 @@ class Walker extends Lint.AbstractWalker<Options> {
if (pair !== undefined) {
const [a, b] = pair;
const sortedDeclarations = sortByKey(imports, x =>
this.options.namedImportsOrderTransform(x.getText())
this.options.namedImportsOrderTransform(x.getText()),
).map(x => x.getText());
// replace in reverse order to preserve earlier offsets
for (let i = imports.length - 1; i >= 0; i--) {
Expand Down Expand Up @@ -490,7 +489,7 @@ class Walker extends Lint.AbstractWalker<Options> {
const startOffset =
allImportDeclarations.length === 0 ? 0 : allImportDeclarations[0].nodeStartOffset;
replacements.push(
Lint.Replacement.appendText(startOffset, this.getGroupedImports(allImportDeclarations))
Lint.Replacement.appendText(startOffset, this.getGroupedImports(allImportDeclarations)),
);
return replacements;
}
Expand All @@ -499,7 +498,7 @@ class Walker extends Lint.AbstractWalker<Options> {
* Get set of replacements that delete all existing imports.
*/
private getGroupRemovalReplacements(
groupedDeclarations: ImportDeclaration[][]
groupedDeclarations: ImportDeclaration[][],
): Lint.Replacement[] {
return groupedDeclarations.map((items, index) => {
let start = items[0].nodeStartOffset;
Expand Down Expand Up @@ -572,7 +571,7 @@ class ImportsBlock {
sourceFile: ts.SourceFile,
node: ImportDeclaration["node"],
sourcePath: string,
group: GroupOption
group: GroupOption,
) {
const start = this.getStartOffset(node);
const end = this.getEndOffset(sourceFile, node);
Expand All @@ -592,7 +591,7 @@ class ImportsBlock {
nodeStartOffset: start,
sourcePath,
text,
type
type,
});
}

Expand Down Expand Up @@ -684,7 +683,7 @@ function flipCase(str: string): string {
// If not, return the pair of nodes which are out of order.
function findUnsortedPair(
xs: ReadonlyArray<ts.Node>,
transform: (x: string) => string
transform: (x: string) => string,
): [ts.Node, ts.Node] | undefined {
for (let i = 1; i < xs.length; i++) {
if (transform(xs[i].getText()) < transform(xs[i - 1].getText())) {
Expand Down

0 comments on commit 724d46d

Please sign in to comment.