Skip to content

Commit

Permalink
Merge pull request #6 from NullVoxPopuli/fix-extensions-on-exports
Browse files Browse the repository at this point in the history
Fix extensions on exports
  • Loading branch information
NullVoxPopuli authored Feb 3, 2024
2 parents befb52d + f512812 commit 9c54f3b
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 5 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
*.yaml
*.yml
*.md

declarations/
18 changes: 14 additions & 4 deletions src/fixes/glint.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const j = jscodeshift.withParser('ts');
export function fixGTSExtensions(contents) {
const root = j(contents);

const fixed = root
root
.find(j.ImportDeclaration)
// @ts-expect-error
.filter((path) => path.node.source.value.includes('.gts'))
Expand All @@ -19,10 +19,20 @@ export function fixGTSExtensions(contents) {
// moduleResolution = "bundler"
// @ts-expect-error
path.node.source.value = path.node.source.value.replace(/\.gts$/, '');
})
.toSource();
});

return fixed;
root
.find(j.ExportNamedDeclaration)
// @ts-expect-error
.filter((path) => path.node.source?.value?.includes('.gts'))
.forEach((path) => {
// TODO: this may only be appropriate when
// moduleResolution = "bundler"
// @ts-expect-error
path.node.source.value = path.node.source.value.replace(/\.gts$/, '');
});

return root.toSource();
}

/**
Expand Down
20 changes: 20 additions & 0 deletions src/fixes/glint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,26 @@ describe('fixGTSExtensions', () => {

expect(result).toBe(`import x from "./foo";`);
});

test('works on default exports', () => {
let code = stripIndent`
export x from './foo.gts';
`;

let result = fixGTSExtensions(code);

expect(result).toBe(`export x from "./foo";`);
});

test('works on named exports', () => {
let code = stripIndent`
export { x } from './foo.gts';
`;

let result = fixGTSExtensions(code);

expect(result).toBe(`export { x } from "./foo";`);
});
});

describe('fixOwnReferences', () => {
Expand Down
7 changes: 6 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ export async function fixBadDeclarationOutput(
}

for (let filePath of files) {
await fixFile(filePath, fixesToApply);
try {
await fixFile(filePath, fixesToApply);
} catch (e) {
console.error(`Errored while processing ${filePath}`);
throw e;
}
}
}

0 comments on commit 9c54f3b

Please sign in to comment.