Skip to content

Commit

Permalink
feat: add support for index jsx/tsx files in modules when using "." a…
Browse files Browse the repository at this point in the history
…nd "./" module imports (#121)

* feat: add support for index jsx/tsx files in modules

* add test for tsxjsx modules
ts/js taking priority over tsx/jsx modules with index files

* chore: update skott version
  • Loading branch information
robertoyoc committed Oct 6, 2023
1 parent 6560f43 commit 06674a0
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .changeset/long-hats-retire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"skott": patch
---

Add support for tsx/jsx index modules.
index.tsx or index.jsx file resolution for modules
16 changes: 16 additions & 0 deletions packages/skott/src/modules/resolvers/base-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,11 @@ export function resolveImportedModulePath(
* Example: `require("./component")` will resolve to `./component.jsx`.
*/
JSX_MODULE: module.concat(".jsx"),
/**
* or a JSX module targetted through a directory
* import e.g: require("./lib") which will eventually resolve to "lib/index.jsx".
*/
JSX_INDEX_MODULE: path.join(module, "index.jsx"),
/**
* In case of TypeScript modules, the module can be targetted through a directory
* import e.g: import "./lib" which will eventually resolve to "lib/index.ts".
Expand All @@ -219,6 +224,11 @@ export function resolveImportedModulePath(
* Example: `require("./component")` will resolve to `./component.tsx`.
*/
TSX_MODULE: module.concat(".tsx"),
/**
* or a TSX module targetted through a directory
* import e.g: require("./lib") which will eventually resolve to "lib/index.tsx".
*/
TSX_INDEX_MODULE: path.join(module, "index.tsx"),
/**
* In case of TypeScript modules but when targetting ECMAScript modules,
* modules are suffixed with ".js" but should resolve to their corresponding
Expand Down Expand Up @@ -248,9 +258,15 @@ export function resolveImportedModulePath(
Effect.orElse(() =>
resolveToModuleIfExists(ecmaScriptModuleCombinations.TSX_MODULE)
),
Effect.orElse(() =>
resolveToModuleIfExists(ecmaScriptModuleCombinations.TSX_INDEX_MODULE)
),
Effect.orElse(() =>
resolveToModuleIfExists(ecmaScriptModuleCombinations.JSX_MODULE)
),
Effect.orElse(() =>
resolveToModuleIfExists(ecmaScriptModuleCombinations.JSX_INDEX_MODULE)
),
Effect.orElse(() =>
resolveToModuleIfExists(ecmaScriptModuleCombinations.JS_INDEX_MODULE)
)
Expand Down
97 changes: 97 additions & 0 deletions packages/skott/test/unit/ecmascript/jsx-and-tsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,103 @@ export function makeTestSuiteForJsxOrTsx(rawLanguage: "ts" | "js"): void {
});
});
});

describe(`When ${jsxOrTsx} index module is imported via "./" or "." module declaration`, () => {
describe(`When there is only a ${jsxOrTsx} index module`, () => {
it(`it should resolve to index.${jsxOrTsx}`, async () => {
mountFakeFileSystem({
[`index.${rawLanguage}`]: `
import "./component_1";
import "./component_2";
`,
[`component_1/index.${jsxOrTsx}`]: `
export default function Component() {
return <div>Hello World</div>;
}
`,
[`component_2/index.${jsxOrTsx}`]: `
export default function Component() {
return <div>Hello World</div>;
}
`
});

const { graph } = await buildSkottProjectUsingInMemoryFileExplorer({
entrypoint: `index.${rawLanguage}`
});

expect(graph).to.be.deep.equal({
[`index.${rawLanguage}`]: {
adjacentTo: [`component_1/index.${jsxOrTsx}`, `component_2/index.${jsxOrTsx}`],
id: `index.${rawLanguage}`,
body: fakeNodeBody
},
[`component_1/index.${jsxOrTsx}`]: {
adjacentTo: [],
id: `component_1/index.${jsxOrTsx}`,
body: fakeNodeBody
},
[`component_2/index.${jsxOrTsx}`]: {
adjacentTo: [],
id: `component_2/index.${jsxOrTsx}`,
body: fakeNodeBody
}
});
});
});
describe(`When there is both ${jsxOrTsx} and ${rawLanguage} index modules`, () => {
it(`it should resolve to index.${rawLanguage} in priority over index.${jsxOrTsx}`, async () => {
mountFakeFileSystem({
[`index.${rawLanguage}`]: `
import "./component_1";
import "./component_2";
`,
[`component_1/index.${rawLanguage}`]: `
export default function Component() {
return <div>Hello World</div>;
}
`,
[`component_1/index.${jsxOrTsx}`]: `
export default function Component() {
return <div>Hello World</div>;
}
`,
[`component_2/index.${rawLanguage}`]: `
export default function Component() {
return <div>Hello World</div>;
}
`,
[`component_2/index.${jsxOrTsx}`]: `
export default function Component() {
return <div>Hello World</div>;
}
`
});

const { graph } = await buildSkottProjectUsingInMemoryFileExplorer({
entrypoint: `index.${rawLanguage}`
});

expect(graph).to.be.deep.equal({
[`index.${rawLanguage}`]: {
adjacentTo: [`component_1/index.${rawLanguage}`, `component_2/index.${rawLanguage}`],
id: `index.${rawLanguage}`,
body: fakeNodeBody
},
[`component_1/index.${rawLanguage}`]: {
adjacentTo: [],
id: `component_1/index.${rawLanguage}`,
body: fakeNodeBody
},
[`component_2/index.${rawLanguage}`]: {
adjacentTo: [],
id: `component_2/index.${rawLanguage}`,
body: fakeNodeBody
}
});
});
});
});
}

describe("When some TypeScript code is not TSX compliant", () => {
Expand Down

0 comments on commit 06674a0

Please sign in to comment.