-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Read .babelrc (or similar) if file is passed to import-sort - Parse file as TypeScript if given file has .ts or .tsx extension
- Loading branch information
Showing
14 changed files
with
549 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
packages/import-sort-parser-babylon/test/flow-babelrc/.babelrc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"presets": ["@babel/preset-flow"] | ||
} |
37 changes: 37 additions & 0 deletions
37
packages/import-sort-parser-babylon/test/flow-babelrc/flow.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import "mocha"; | ||
|
||
import {assert} from "chai"; | ||
import {IImport} from "import-sort-parser"; | ||
|
||
import {formatImport, parseImports} from "../../lib"; | ||
|
||
const parseFlowImports = code => { | ||
// Pass (fake) file name to the parser so it can read .babelrc | ||
return parseImports(code, {file: __dirname + "/flow.js"}); | ||
}; | ||
|
||
describe("parseImports (Flow, with @babel/preset-flow)", () => { | ||
it("should return default type import", () => { | ||
const imports = parseFlowImports( | ||
` | ||
import type p from 'q'; | ||
`.trim(), | ||
); | ||
|
||
assert.equal(imports[0].type, "import-type"); | ||
assert.equal(imports[0].start, 0); | ||
assert.equal(imports[0].end, imports[0].end); | ||
assert.equal(imports[0].moduleName, "q"); | ||
assert.equal(imports[0].defaultMember, "p"); | ||
}); | ||
|
||
it("should include type information for named type imports", () => { | ||
const imports = parseFlowImports( | ||
` | ||
import {type a} from "x"; | ||
`.trim(), | ||
); | ||
|
||
assert.equal(imports[0].namedMembers[0].type, true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import "mocha"; | ||
|
||
import {assert} from "chai"; | ||
import {IImport} from "import-sort-parser"; | ||
|
||
import {formatImport, parseImports} from "../../lib"; | ||
|
||
const parseFlowImports = code => { | ||
// No file name is passed to the parser here | ||
return parseImports(code); | ||
}; | ||
|
||
describe("parseImports (Flow, without @babel/preset-flow)", () => { | ||
it("should return default type import", () => { | ||
const imports = parseFlowImports( | ||
` | ||
import type p from 'q'; | ||
`.trim(), | ||
); | ||
|
||
assert.equal(imports[0].type, "import-type"); | ||
assert.equal(imports[0].start, 0); | ||
assert.equal(imports[0].end, imports[0].end); | ||
assert.equal(imports[0].moduleName, "q"); | ||
assert.equal(imports[0].defaultMember, "p"); | ||
}); | ||
|
||
it("should include type information for named type imports", () => { | ||
const imports = parseFlowImports( | ||
` | ||
import {type a} from "x"; | ||
`.trim(), | ||
); | ||
|
||
assert.equal(imports[0].namedMembers[0].type, true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
packages/import-sort-parser-babylon/test/typescript-babelrc/.babelrc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"presets": ["@babel/preset-typescript"] | ||
} |
79 changes: 79 additions & 0 deletions
79
packages/import-sort-parser-babylon/test/typescript-babelrc/typescript.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import "mocha"; | ||
|
||
import {assert} from "chai"; | ||
|
||
import {parseImports} from "../../lib"; | ||
|
||
const parseTypeScriptImports = code => { | ||
// Pass (fake) file name to the parser so it can read .babelrc | ||
return parseImports(code, {file: __dirname + "/typescript.ts"}); | ||
}; | ||
|
||
describe("parseImports (TypeScript, with @babel/preset-typescript)", () => { | ||
it("should return imports", () => { | ||
const imports = parseTypeScriptImports( | ||
` | ||
import "a"; | ||
import b from "b"; | ||
import {c} from "c"; | ||
import d, {e} from "f"; | ||
import g, {h as hh} from "i"; | ||
import * as j from "k"; | ||
import l, * as m from "o"; | ||
// Random TypeScript syntax (that is not Flow syntax) | ||
const a: number = "123" as any; | ||
`.trim(), | ||
); | ||
|
||
assert.equal(imports.length, 7); | ||
|
||
imports.forEach(imported => { | ||
assert.equal(imported.type, "import"); | ||
}); | ||
|
||
// import "a"; | ||
assert.equal(imports[0].start, 0); | ||
assert.equal(imports[0].end, 11); | ||
assert.equal(imports[0].moduleName, "a"); | ||
|
||
// import b from "b"; | ||
assert.equal(imports[1].start, imports[0].end + 1); | ||
assert.equal(imports[1].end, imports[0].end + 1 + 18); | ||
assert.equal(imports[1].moduleName, "b"); | ||
assert.equal(imports[1].defaultMember, "b"); | ||
|
||
// import {c} from "c"; | ||
assert.equal(imports[2].start, imports[1].end + 1); | ||
assert.equal(imports[2].end, imports[1].end + 1 + 20); | ||
assert.equal(imports[2].moduleName, "c"); | ||
assert.deepEqual(imports[2].namedMembers![0], {name: "c", alias: "c"}); | ||
|
||
// import d, {e} from "f"; | ||
assert.equal(imports[3].start, imports[2].end + 1); | ||
assert.equal(imports[3].end, imports[2].end + 1 + 23); | ||
assert.equal(imports[3].moduleName, "f"); | ||
assert.equal(imports[3].defaultMember, "d"); | ||
assert.deepEqual(imports[3].namedMembers![0], {name: "e", alias: "e"}); | ||
|
||
// import g, {h as hh} from "i"; | ||
assert.equal(imports[4].start, imports[3].end + 1); | ||
assert.equal(imports[4].end, imports[3].end + 1 + 29); | ||
assert.equal(imports[4].moduleName, "i"); | ||
assert.equal(imports[4].defaultMember, "g"); | ||
assert.deepEqual(imports[4].namedMembers![0], {name: "h", alias: "hh"}); | ||
|
||
// import * as j from "k"; | ||
assert.equal(imports[5].start, imports[4].end + 1); | ||
assert.equal(imports[5].end, imports[4].end + 1 + 23); | ||
assert.equal(imports[5].moduleName, "k"); | ||
assert.equal(imports[5].namespaceMember, "j"); | ||
|
||
// import l, * as m from "o"; | ||
assert.equal(imports[6].start, imports[5].end + 1); | ||
assert.equal(imports[6].end, imports[5].end + 1 + 26); | ||
assert.equal(imports[6].moduleName, "o"); | ||
assert.equal(imports[6].defaultMember, "l"); | ||
assert.equal(imports[6].namespaceMember, "m"); | ||
}); | ||
}); |
79 changes: 79 additions & 0 deletions
79
packages/import-sort-parser-babylon/test/typescript/typescript.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import "mocha"; | ||
|
||
import {assert} from "chai"; | ||
|
||
import {parseImports} from "../../lib"; | ||
|
||
const parseTypeScriptImports = code => { | ||
// No file name is passed to the parser here | ||
return parseImports(code, {file: __dirname + "/typescript.ts"}); | ||
}; | ||
|
||
describe("parseImports (TypeScript, without @babel/preset-typescript)", () => { | ||
it("should return imports", () => { | ||
const imports = parseTypeScriptImports( | ||
` | ||
import "a"; | ||
import b from "b"; | ||
import {c} from "c"; | ||
import d, {e} from "f"; | ||
import g, {h as hh} from "i"; | ||
import * as j from "k"; | ||
import l, * as m from "o"; | ||
// Random TypeScript syntax (that is not Flow syntax) | ||
const a: number = "123" as any; | ||
`.trim(), | ||
); | ||
|
||
assert.equal(imports.length, 7); | ||
|
||
imports.forEach(imported => { | ||
assert.equal(imported.type, "import"); | ||
}); | ||
|
||
// import "a"; | ||
assert.equal(imports[0].start, 0); | ||
assert.equal(imports[0].end, 11); | ||
assert.equal(imports[0].moduleName, "a"); | ||
|
||
// import b from "b"; | ||
assert.equal(imports[1].start, imports[0].end + 1); | ||
assert.equal(imports[1].end, imports[0].end + 1 + 18); | ||
assert.equal(imports[1].moduleName, "b"); | ||
assert.equal(imports[1].defaultMember, "b"); | ||
|
||
// import {c} from "c"; | ||
assert.equal(imports[2].start, imports[1].end + 1); | ||
assert.equal(imports[2].end, imports[1].end + 1 + 20); | ||
assert.equal(imports[2].moduleName, "c"); | ||
assert.deepEqual(imports[2].namedMembers![0], {name: "c", alias: "c"}); | ||
|
||
// import d, {e} from "f"; | ||
assert.equal(imports[3].start, imports[2].end + 1); | ||
assert.equal(imports[3].end, imports[2].end + 1 + 23); | ||
assert.equal(imports[3].moduleName, "f"); | ||
assert.equal(imports[3].defaultMember, "d"); | ||
assert.deepEqual(imports[3].namedMembers![0], {name: "e", alias: "e"}); | ||
|
||
// import g, {h as hh} from "i"; | ||
assert.equal(imports[4].start, imports[3].end + 1); | ||
assert.equal(imports[4].end, imports[3].end + 1 + 29); | ||
assert.equal(imports[4].moduleName, "i"); | ||
assert.equal(imports[4].defaultMember, "g"); | ||
assert.deepEqual(imports[4].namedMembers![0], {name: "h", alias: "hh"}); | ||
|
||
// import * as j from "k"; | ||
assert.equal(imports[5].start, imports[4].end + 1); | ||
assert.equal(imports[5].end, imports[4].end + 1 + 23); | ||
assert.equal(imports[5].moduleName, "k"); | ||
assert.equal(imports[5].namespaceMember, "j"); | ||
|
||
// import l, * as m from "o"; | ||
assert.equal(imports[6].start, imports[5].end + 1); | ||
assert.equal(imports[6].end, imports[5].end + 1 + 26); | ||
assert.equal(imports[6].moduleName, "o"); | ||
assert.equal(imports[6].defaultMember, "l"); | ||
assert.equal(imports[6].namespaceMember, "m"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.