-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Useful changes from my setup which I think may be beneficial to others #506
Changes from 16 commits
6246b9a
e713147
b063c3f
3b4e10e
1a7ee44
2c2f69e
0fd3a6d
e3a4b11
52f9e0f
9d3567e
7bceb5b
124a792
60fc13f
f64afde
89f5e4b
6b8657c
ed94946
4689499
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
import sqlite3 from 'sqlite3'; | ||
|
||
import lastUpdate from './lastUpdate'; | ||
|
||
const MAX_CHUNK_SIZE = 100; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -148,11 +148,8 @@ export default class ImportStatement { | |
/** | ||
* @return {Array} an array that can be used in `sort` and `uniq` | ||
*/ | ||
toNormalized(): Array<string> { | ||
if (!this.defaultImport && !this.hasNamedImports() && this.hasSideEffects) { | ||
return [this.path]; | ||
} | ||
return [this.defaultImport || '', ...this.localNames()]; | ||
toNormalized(): Array<any> { | ||
return [this.defaultImport !== 'React', this.path]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you want There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because this is the standard everywhere React is used. This way it won't interfere with other people on your project importing themselves (React is the first thing they import). You will omit unnecessary changes to a file. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure I understand. I haven't come across any project where React has to be at the top. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess I was wrong - it is only standard in the firm I work in. Therefore I left it to [this.path] only. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you help me understand why this change is made (including the type change)? It seems weird to have React hard-coded in this project. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I reverted the change. Now it is only [this.path] |
||
} | ||
|
||
localNames(): Array<string> { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -243,14 +243,15 @@ export default class ImportStatements { | |
importsArray, | ||
(importStatement: ImportStatement): boolean => | ||
!importStatement.isParsedAndUntouched(), | ||
); | ||
).reverse(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will have to go too. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. #448 |
||
|
||
result = flattenDeep(result); | ||
|
||
if (this.config.get('sortImports')) { | ||
result = sortBy(result, (is: ImportStatement): Array<string> => is.toNormalized()); | ||
result = sortBy(result, (is: ImportStatement): Array<any> => is.toNormalized()); | ||
} | ||
|
||
result = uniqBy(result, (is: ImportStatement): Array<string> => | ||
result = uniqBy(result, (is: ImportStatement): Array<any> => | ||
is.toNormalized()); | ||
|
||
if (!this.config.get('groupImports')) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,6 +58,13 @@ it('knows about dynamic keys', () => { | |
`))).toEqual(new Set(['bar'])); | ||
}); | ||
|
||
it('knows about aliases in destructured objects', () => { | ||
expect(findUndefinedIdentifiers(parse(` | ||
const foo = { 'theValueOfMyConst': 123 }; | ||
const { [MY_CONST]: someAlias } = foo; | ||
`))).toEqual(new Set(['MY_CONST'])); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice. |
||
|
||
it('knows about jsx', () => { | ||
expect(findUndefinedIdentifiers(parse(` | ||
export default <FooBar foo={bar} />; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
import FileUtils from './FileUtils'; | ||
|
||
const isWinDriveRoot = /^[A-Z]:\\$/; | ||
|
||
|
@@ -11,7 +12,12 @@ function findRecursive(directory) { | |
const pathToPackageJson = path.join(directory, 'package.json'); | ||
|
||
if (fs.existsSync(pathToPackageJson)) { | ||
return directory; | ||
const packageDescription = FileUtils.readJsonFile(pathToPackageJson); | ||
const { isRoot = true } = Object(packageDescription)['import-js'] || {}; | ||
|
||
if (isRoot) { | ||
return directory; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks useful, and I'm assuming it's a continuation of #462? An update to |
||
} | ||
|
||
return findRecursive(path.dirname(directory)); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
const KEYS_USED_FOR_ASSIGNMENT = new Set(['id', 'imported', 'local', 'params']); | ||
const KEYS_USED_IN_REFERENCE_TO_OBJECTS = new Set(['property']); | ||
|
||
function normalizeNode(node, context) { | ||
const { key, parent } = context; | ||
|
@@ -54,7 +53,7 @@ function normalizeNode(node, context) { | |
} | ||
|
||
const isAssignment = KEYS_USED_FOR_ASSIGNMENT.has(key) || | ||
(key === 'key' && parent.parent.type === 'ObjectPattern') || | ||
(key === 'key' && !parent.computed && parent.parent.type === 'ObjectPattern') || | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you come up with a test case demonstrating what this is supposed to fix? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added tests |
||
(key === 'left' && parent.type === 'AssignmentPattern') || | ||
(key === 'elements' && parent.type === 'ArrayPattern') || | ||
(key === 'value' && | ||
|
@@ -64,7 +63,7 @@ function normalizeNode(node, context) { | |
context.definedInScope.add(node.name); | ||
} | ||
|
||
const isReference = KEYS_USED_IN_REFERENCE_TO_OBJECTS.has(key) || | ||
const isReference = (key === 'property' && !parent.computed && parent.parent.type !== 'ObjectPattern') || | ||
(key === 'key' && !parent.computed && parent.parent.type !== 'ObjectPattern'); | ||
|
||
return { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because most of the configuration for import JS is in the import JS configuration file, I think it would be helpful to make it clearer that this snippet is showing
package.json
. Maybe all we need to do is add "...not root like this inpackage.json
:" or somethingThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done