From 3ee048ecbf47c539b98905b529b29c2ec94674c0 Mon Sep 17 00:00:00 2001 From: machty Date: Tue, 9 Jul 2024 10:59:26 -0400 Subject: [PATCH] wip --- packages/core/__tests__/cli/check.test.ts | 2 +- .../src/transform/template/inlining/companion-file.ts | 8 ++++++++ .../core/src/transform/template/map-template-contents.ts | 3 ++- packages/core/src/volar/handlebars-virtual-code.ts | 2 +- 4 files changed, 12 insertions(+), 3 deletions(-) diff --git a/packages/core/__tests__/cli/check.test.ts b/packages/core/__tests__/cli/check.test.ts index a6fe77ec4..ba135128c 100644 --- a/packages/core/__tests__/cli/check.test.ts +++ b/packages/core/__tests__/cli/check.test.ts @@ -176,7 +176,7 @@ describe('CLI: single-pass typechecking', () => { `); }); - test.only('reports diagnostics for a companion template type error', async () => { + test('reports diagnostics for a companion template type error', async () => { project.setGlintConfig({ environment: 'ember-loose' }); let script = stripIndent` diff --git a/packages/core/src/transform/template/inlining/companion-file.ts b/packages/core/src/transform/template/inlining/companion-file.ts index 4417b84e1..6ba43c182 100644 --- a/packages/core/src/transform/template/inlining/companion-file.ts +++ b/packages/core/src/transform/template/inlining/companion-file.ts @@ -49,6 +49,8 @@ export function calculateCompanionTemplateSpans( suffix: '}\n', }); } else { + // TODO: when does this get called? + throw new Error("ALEX not class like"); let backingValue: string | undefined; if (targetNode) { let moduleName = path.basename(script.filename, path.extname(script.filename)); @@ -143,6 +145,9 @@ export function calculateCompanionTemplateSpans( } } +/** + * Find and return the TS AST node which can serve as a proper insertion point + */ function findCompanionTemplateTarget( ts: TSLib, sourceFile: ts.SourceFile, @@ -155,6 +160,7 @@ function findCompanionTemplateTarget( mods?.some((mod) => mod.kind === ts.SyntaxKind.DefaultKeyword) && mods.some((mod) => mod.kind === ts.SyntaxKind.ExportKeyword) ) { + // We've found a `export default class` statement; return it. return statement; } @@ -164,6 +170,8 @@ function findCompanionTemplateTarget( } } + // We didn't find a default export, but maybe there is a named export that + // matches one of the class statements we found above. for (let statement of sourceFile.statements) { if (ts.isExportAssignment(statement) && !statement.isExportEquals) { if (ts.isIdentifier(statement.expression) && statement.expression.text in classes) { diff --git a/packages/core/src/transform/template/map-template-contents.ts b/packages/core/src/transform/template/map-template-contents.ts index 10aebdb85..da5bef578 100644 --- a/packages/core/src/transform/template/map-template-contents.ts +++ b/packages/core/src/transform/template/map-template-contents.ts @@ -121,7 +121,8 @@ export type MapTemplateContentsOptions = { }; /** - * Given the text of an embedded template, invokes the given callback + * Given the text of a handlebars template (either standalone .hbs file, or the contents + * of an embedded `` within a .gts file), invokes the given callback * with a set of tools to emit mapped contents corresponding to * that template, tracking the text emitted in order to provide * a mapping of ranges in the input to ranges in the output. diff --git a/packages/core/src/volar/handlebars-virtual-code.ts b/packages/core/src/volar/handlebars-virtual-code.ts index 543ef422d..9bf9fdee2 100644 --- a/packages/core/src/volar/handlebars-virtual-code.ts +++ b/packages/core/src/volar/handlebars-virtual-code.ts @@ -70,7 +70,7 @@ export class VirtualHandlebarsCode implements VirtualCode { // Here we are assembling the args to pass into rewriteModule, which wants both the .ts script // and the template file. For .gts template is undefined but here we need to pass in the contents. // Let's see how rewriteModule attempts to transform this shit. - let script = { filename: 'disregard.ts', contents: 'export {}; let a = 123;' }; + let script = { filename: 'disregard.ts', contents: 'export default class MyComponent {}' }; let template = { filename: 'disregard.hbs', contents,