Skip to content

Commit

Permalink
fix(utils): fix exports finder for enum that were transpilled with tsc (
Browse files Browse the repository at this point in the history
  • Loading branch information
Anber committed Sep 19, 2023
1 parent f3a4f2a commit 6390233
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .changeset/orange-islands-juggle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@linaria/testkit': patch
'@linaria/utils': patch
---

The exports finder didn't support enums that were transpiled to esm by tsc. Fixed.
9 changes: 9 additions & 0 deletions packages/testkit/src/collectExportsAndImports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ function typescriptCommonJS(source: string): string {
return result.outputText;
}

function typescriptES2022(source: string): string {
const result = ts.transpileModule(source, {
compilerOptions: { module: ts.ModuleKind.ES2022 },
});

return result.outputText;
}

const withoutLocal = <T extends { local: NodePath }>({
local,
...obj
Expand Down Expand Up @@ -103,6 +111,7 @@ const compilers: [name: string, compiler: (code: string) => string][] = [
['swcCommonJSes5', swcCommonJS('es5')],
['swcCommonJSes2015', swcCommonJS('es2015')],
['typescriptCommonJS', typescriptCommonJS],
['typescriptES2022', typescriptES2022],
];

function runWithCompiler(
Expand Down
41 changes: 39 additions & 2 deletions packages/utils/src/collectExportsAndImports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,14 +246,51 @@ function importFromVariableDeclarator(
return [];
}

const findIIFE = (path: NodePath): NodePath<CallExpression> | null => {
if (path.isCallExpression() && path.get('callee').isFunctionExpression()) {
return path;
}

if (!path.parentPath) {
return null;
}

return findIIFE(path.parentPath);
};

function exportFromVariableDeclarator(
path: NodePath<VariableDeclarator>
): Exports {
const id = path.get('id');
const init = path.get('init');

// If there is no init expression, we can ignore this export
if (!init || !init.isExpression()) return {};
// If there is no init and id is an identifier, we should find IIFE
if (!init.node && id.isIdentifier()) {
const binding = getScope(path).getBinding(id.node.name);
if (!binding) {
return {};
}

const iife = [
...binding.referencePaths,
...binding.constantViolations,
binding.path,
]
.map(findIIFE)
.find(isNotNull);

if (!iife) {
return {};
}

return {
[id.node.name]: iife,
};
}

if (!init || !init.isExpression()) {
return {};
}

if (id.isIdentifier()) {
// It is `export const a = 1;`
Expand Down

0 comments on commit 6390233

Please sign in to comment.