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

Fix [ordered-imports] "import-sources-order": any invalidate "grouped-imports" #4374

Merged
merged 2 commits into from
Dec 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions src/rules/orderedImportsRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,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 @@ -275,14 +273,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 @@ -432,11 +432,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 @@ -510,17 +510,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}]
}
}