From b2e9279fb0cab717c97c1d95b205a0a8b9d636e0 Mon Sep 17 00:00:00 2001
From: NullVoxPopuli <199018+NullVoxPopuli@users.noreply.github.com>
Date: Thu, 14 Dec 2023 17:47:56 -0500
Subject: [PATCH 1/2] Try this
---
.../__tests__/cli/custom-extensions.test.ts | 59 +++++++++++++++++++
packages/core/src/cli/perform-check.ts | 12 ++++
2 files changed, 71 insertions(+)
diff --git a/packages/core/__tests__/cli/custom-extensions.test.ts b/packages/core/__tests__/cli/custom-extensions.test.ts
index ec01eabd1..9c84905e3 100644
--- a/packages/core/__tests__/cli/custom-extensions.test.ts
+++ b/packages/core/__tests__/cli/custom-extensions.test.ts
@@ -135,6 +135,15 @@ describe('CLI: custom extensions', () => {
'Greeting.gts': stripIndent`
Hello!
`,
+ 're-export.gts': stripIndent`
+ export { default as Greeting } from './Greeting.gts';
+ `,
+ 'vanilla.ts': 'export const two = 2;',
+ 'barrel.ts': stripIndent`
+ export { default as Greeting } from './Greeting.gts';
+ export { Greeting as Greeting2 } from './re-export.gts';
+ export { two } from './vanilla.ts';
+ `,
});
});
@@ -147,6 +156,26 @@ describe('CLI: custom extensions', () => {
1 import Greeting from './Greeting.gts';
~~~~~~~~~~~~~~~~
+
+ barrel.ts:1:37 - error TS2307: Cannot find module './Greeting.gts' or its corresponding type declarations.
+
+ 1 export { default as Greeting } from './Greeting.gts';
+ ~~~~~~~~~~~~~~~~
+
+ barrel.ts:2:39 - error TS2307: Cannot find module './re-export.gts' or its corresponding type declarations.
+
+ 2 export { Greeting as Greeting2 } from './re-export.gts';
+ ~~~~~~~~~~~~~~~~~
+
+ barrel.ts:3:21 - error TS5097: An import path can only end with a '.ts' extension when 'allowImportingTsExtensions' is enabled.
+
+ 3 export { two } from './vanilla.ts';
+ ~~~~~~~~~~~~~~
+
+ re-export.gts:1:37 - error TS2307: Cannot find module './Greeting.gts' or its corresponding type declarations.
+
+ 1 export { default as Greeting } from './Greeting.gts';
+ ~~~~~~~~~~~~~~~~
"
`);
});
@@ -165,5 +194,35 @@ describe('CLI: custom extensions', () => {
expect(result.stderr).toBe('');
}
);
+
+ test.runIf(semver.gte(typescript.version, '5.0.0'))(
+ 'declarations work with `allowImportingTsExtensions: true`',
+ async () => {
+ project.updateTsconfig((config) => {
+ config.compilerOptions ??= {};
+ config.compilerOptions['allowImportingTsExtensions'] = true;
+ });
+
+ let emitResult = await project.check({ flags: ['--declaration'] });
+
+ expect(emitResult.exitCode).toBe(0);
+
+ expect(project.read('re-export.d.ts')).toMatchInlineSnapshot(`
+ "export { default as Greeting } from './Greeting';
+ "
+ `);
+ expect(project.read('barrel.d.ts')).toMatchInlineSnapshot(`
+ "export { default as Greeting } from './Greeting';
+ export { Greeting as Greeting2 } from './re-export';
+ export { two } from './vanilla';
+ "
+ `);
+ expect(project.read('./Greeting.d.ts')).toMatchInlineSnapshot(`
+ "declare const _default: import(\\"@ember/component/template-only\\").TemplateOnlyComponent & (abstract new () => import(\\"@glint/template/-private/integration\\").InvokableInstance<() => import(\\"@glint/template/-private/integration\\").ComponentReturn<{}>> & import(\\"@glint/template/-private/integration\\").HasContext>);
+ export default _default;
+ "
+ `);
+ }
+ );
});
});
diff --git a/packages/core/src/cli/perform-check.ts b/packages/core/src/cli/perform-check.ts
index 3de364b76..e3b542ed9 100644
--- a/packages/core/src/cli/perform-check.ts
+++ b/packages/core/src/cli/perform-check.ts
@@ -69,6 +69,18 @@ function createCompilerHost(
host.readFile = transformManager.readTransformedFile;
host.readDirectory = transformManager.readDirectory;
+ // Postprocess .d.ts files to temporarily patch '.gts' to '.ts'
+ // https://github.com/typed-ember/glint/issues/628
+ const tsWriteFile = host.writeFile;
+ const matchGtsImport = /\.gts';/gm;
+ host.writeFile = (fileName, data, writeByteOrderMark, onError) => {
+ const isDts = fileName.endsWith('.d.ts');
+ if (isDts && matchGtsImport.test(data)) {
+ data = data.replace(matchGtsImport, ".ts';");
+ }
+ tsWriteFile(fileName, data, writeByteOrderMark, onError);
+ };
+
return host;
}
From 5aaa6277012aac8c8332dee2d268a2b5045eb101 Mon Sep 17 00:00:00 2001
From: NullVoxPopuli <199018+NullVoxPopuli@users.noreply.github.com>
Date: Fri, 15 Dec 2023 11:54:55 -0500
Subject: [PATCH 2/2] Update snapshot
---
packages/core/__tests__/cli/custom-extensions.test.ts | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/packages/core/__tests__/cli/custom-extensions.test.ts b/packages/core/__tests__/cli/custom-extensions.test.ts
index 9c84905e3..ef84115ed 100644
--- a/packages/core/__tests__/cli/custom-extensions.test.ts
+++ b/packages/core/__tests__/cli/custom-extensions.test.ts
@@ -167,7 +167,7 @@ describe('CLI: custom extensions', () => {
2 export { Greeting as Greeting2 } from './re-export.gts';
~~~~~~~~~~~~~~~~~
- barrel.ts:3:21 - error TS5097: An import path can only end with a '.ts' extension when 'allowImportingTsExtensions' is enabled.
+ barrel.ts:3:21 - error TS2691: An import path cannot end with a '.ts' extension. Consider importing './vanilla.js' instead.
3 export { two } from './vanilla.ts';
~~~~~~~~~~~~~~