Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
rrogowski authored and ericanderson committed Dec 12, 2018
1 parent 412604b commit f6044a5
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 15 deletions.
30 changes: 15 additions & 15 deletions src/rules/orderedImportsRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,7 @@ class Walker extends Lint.AbstractWalker<Options> {
return;
}

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

const { importClause } = node;
Expand Down Expand Up @@ -277,14 +275,16 @@ class Walker extends Lint.AbstractWalker<Options> {
return;
}

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

private checkSource(source: string, node: ImportDeclaration["node"]) {
private checkSource(originalSource: string, node: ImportDeclaration["node"]) {
const type = getImportType(originalSource);
const source = this.options.importSourcesOrderTransform(originalSource);
const currentSource = this.options.moduleSourcePath(source);
const previousSource = this.currentImportsBlock.getLastImportSource();
this.currentImportsBlock.addImportDeclaration(this.sourceFile, node, currentSource);
this.currentImportsBlock.addImportDeclaration(this.sourceFile, node, currentSource, type);

if (previousSource !== null && compare(currentSource, previousSource) === -1) {
this.lastFix = [];
Expand Down Expand Up @@ -454,11 +454,11 @@ class ImportsBlock {
sourceFile: ts.SourceFile,
node: ImportDeclaration["node"],
sourcePath: string,
type: ImportType,
) {
const start = this.getStartOffset(node);
const end = this.getEndOffset(sourceFile, node);
const text = sourceFile.text.substring(start, end);
const type = this.getImportType(sourcePath);

if (start > node.getStart() || end === 0) {
// skip block if any statements don't end with a newline to simplify implementation
Expand Down Expand Up @@ -532,17 +532,17 @@ class ImportsBlock {
private getLastImportDeclaration(): ImportDeclaration | undefined {
return this.importDeclarations[this.importDeclarations.length - 1];
}
}

private getImportType(sourcePath: string): ImportType {
if (sourcePath.charAt(0) === ".") {
if (sourcePath.charAt(1) === ".") {
return ImportType.PARENT_DIRECTORY_IMPORT;
} else {
return ImportType.CURRENT_DIRECTORY_IMPORT;
}
function getImportType(sourcePath: string): ImportType {
if (sourcePath.charAt(0) === ".") {
if (sourcePath.charAt(1) === ".") {
return ImportType.PARENT_DIRECTORY_IMPORT;
} else {
return ImportType.LIBRARY_IMPORT;
return ImportType.CURRENT_DIRECTORY_IMPORT;
}
} else {
return ImportType.LIBRARY_IMPORT;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { getOr } from 'lodash/fp'
import { Request } from 'express'

import { getStatusCode } from './errors'

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { getOr } from 'lodash/fp'

import { Request } from 'express'
import { getStatusCode } from './errors'
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [grouped-imports]

[grouped-imports]: Import sources of different groups must be sorted by: libraries, parent directories, current directory.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"ordered-imports": [true, {"import-sources-order": "any", "grouped-imports": true}]
}
}

0 comments on commit f6044a5

Please sign in to comment.