From 66b53a35adb38ae6fa68f9a52b47ef10e9e28d8a Mon Sep 17 00:00:00 2001 From: machty Date: Tue, 9 Jul 2024 21:36:00 -0400 Subject: [PATCH] getAssociatedScript working-ish --- .../template/inlining/companion-file.ts | 2 +- .../core/src/volar/gts-language-plugin.ts | 8 +++---- ...de-backing-component-class-virtual-code.ts | 21 ++++++++++++------- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/packages/core/src/transform/template/inlining/companion-file.ts b/packages/core/src/transform/template/inlining/companion-file.ts index 6ba43c18..173a5e59 100644 --- a/packages/core/src/transform/template/inlining/companion-file.ts +++ b/packages/core/src/transform/template/inlining/companion-file.ts @@ -50,7 +50,7 @@ export function calculateCompanionTemplateSpans( }); } else { // TODO: when does this get called? - throw new Error("ALEX not class like"); + // throw new Error("ALEX not class like"); let backingValue: string | undefined; if (targetNode) { let moduleName = path.basename(script.filename, path.extname(script.filename)); diff --git a/packages/core/src/volar/gts-language-plugin.ts b/packages/core/src/volar/gts-language-plugin.ts index b7b1ac46..baba2063 100644 --- a/packages/core/src/volar/gts-language-plugin.ts +++ b/packages/core/src/volar/gts-language-plugin.ts @@ -39,14 +39,14 @@ export function createGtsLanguagePlugin( }, // When does this get called? - createVirtualCode(uri, languageId, snapshot, codegenContext) { - const scriptId = String(uri); + createVirtualCode(scriptId: URI | string, languageId, snapshot, codegenContext) { + const scriptIdStr = String(scriptId); // See: https://github.com/JetBrains/intellij-plugins/blob/11a9149e20f4d4ba2c1600da9f2b81ff88bd7c97/Angular/src/angular-service/src/index.ts#L31 if ( languageId === 'typescript' && - !scriptId.endsWith('.d.ts') && - scriptId.indexOf('/node_modules/') < 0 + !scriptIdStr.endsWith('.d.ts') && + scriptIdStr.indexOf('/node_modules/') < 0 ) { // NOTE: scriptId might not be a path when we convert this plugin: // https://github.com/withastro/language-tools/blob/eb7215cc0ab3a8f614455528cd71b81ea994cf68/packages/ts-plugin/src/language.ts#L19 diff --git a/packages/core/src/volar/loose-mode-backing-component-class-virtual-code.ts b/packages/core/src/volar/loose-mode-backing-component-class-virtual-code.ts index c6ae0aff..b4768856 100644 --- a/packages/core/src/volar/loose-mode-backing-component-class-virtual-code.ts +++ b/packages/core/src/volar/loose-mode-backing-component-class-virtual-code.ts @@ -5,6 +5,7 @@ import type ts from 'typescript'; import { Directive, rewriteModule } from '../transform/index.js'; import { GlintConfig } from '../index.js'; import { CodegenContext, SourceScript } from '@volar/language-core/lib/types.js'; +import { URI } from 'vscode-uri'; export type TS = typeof ts; interface EmbeddedCodeWithDirectives extends VirtualCode { @@ -17,7 +18,6 @@ interface EmbeddedCodeWithDirectives extends VirtualCode { * components are only supported when using `ember-loose` environment. */ export class LooseModeBackingComponentClassVirtualCode implements VirtualCode { - embeddedCodes: EmbeddedCodeWithDirectives[] = []; /** @@ -36,7 +36,7 @@ export class LooseModeBackingComponentClassVirtualCode implements VirtualCode { constructor( private glintConfig: GlintConfig, public snapshot: IScriptSnapshot, - public fileUri: string, + public fileId: URI | string, public codegenContext: CodegenContext, ) { this.update(snapshot); @@ -67,22 +67,27 @@ export class LooseModeBackingComponentClassVirtualCode implements VirtualCode { const contents = snapshot.getText(0, length); const templatePathCandidate = this.glintConfig.environment.getPossibleTemplatePaths( - this.fileUri, + String(this.fileId), )[0]; if (!templatePathCandidate) { // TODO: this probably shouldn't be an error; just trying to fail fast for tests for now - throw new Error(`Could not find a template file candidate for ${this.fileUri}`); + throw new Error(`Could not find a template file candidate for ${this.fileId}`); } - const hbsSourceScript = this.codegenContext.getAssociatedScript( - templatePathCandidate.path, - )?.snapshot; + const associatedScriptFileId = + typeof this.fileId == 'string' + ? templatePathCandidate.path + : this.fileId.with({ path: templatePathCandidate.path }); + const hbsSourceScript = + this.codegenContext.getAssociatedScript(associatedScriptFileId)?.snapshot; if (!hbsSourceScript) { // TODO: this probably shouldn't be an error; just trying to fail fast for tests for now // TODO: why does this sometimes fail to find the source script? Race condition, where .ts loads before .hbs? - throw new Error(`Could not find a source script for ${templatePathCandidate.path}`); + let msg = `Could not find a source script for ${templatePathCandidate.path}`; + // throw new Error(msg); + return; } const hbsLength = hbsSourceScript.getLength();