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

Implement fixer for ordering import declarations #1657

Merged
merged 5 commits into from
Nov 5, 2016
Merged

Conversation

nchen63
Copy link
Contributor

@nchen63 nchen63 commented Oct 28, 2016

PR checklist

need a place to list fixers in docs

What changes did you make?

Auto-fixer for import declarations, and the named declarations inside of them.

Is there anything you'd like reviewers to focus on?

(optional)

@nchen63 nchen63 force-pushed the import-order-fixer branch from 6e79fac to 333ac08 Compare October 28, 2016 01:28
Copy link
Contributor

@jkillian jkillian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tbh, it's hard to review all the logic going on here (not that it's that complicated, just hard to do in my head haha.) I'd recommend doing the following to make sure it works well:

Grab a few large TS codebases with unordered imports. Run this on them and make sure there aren't any runtime errors and that the results look good. If they do look good, we should be able to go ahead and merge this. If we find any errors, we can add those as test cases and get them fixed

@@ -109,6 +109,19 @@ function findUnsortedPair(xs: ts.Node[], transform: (x: string) => string): [ts.
return null;
}

function sortByKey<T>(xs: T[], getSortKeyCallback: (x: T) => string): T[] {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'd recommend just calling this getSortKey, since it returns a string and not a function

} else if (transformedA < transformedB) {
return -1;
}
return 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trivia: all these comparisons can be replaced with return transformedA.localeCompare(transformedB). it is slower though, and mainly only important if you're comparing non-english strings

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I used localeCompare, but it ignores casing, so it changes the behavior from what it was before

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In hindsight, beyond performance concerns, you might be better off doing things this way anyways. I believe localeCompare could sort strings differently depending on the users locale and then we wouldn't have a deterministic way of sorting things, which would be bad

node: ts.ImportDeclaration;
nodeStartOffset: number; // start position of node within source file
text: string;
sourceFile: string;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd recommend calling this something like sourcePath or just source, ad it doesn't always refer to a file and getSourceFile/SourceFile is used by TS with a different meaning in TSLint

Noah Chen added 2 commits November 1, 2016 12:19
Strip outer quotes before sorting
Fix replacement of import statement if length changes
@nchen63 nchen63 merged commit 533ad88 into master Nov 5, 2016
@nchen63 nchen63 deleted the import-order-fixer branch November 6, 2016 00:28
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants