Skip to content

Commit

Permalink
getAssociatedScript working-ish
Browse files Browse the repository at this point in the history
  • Loading branch information
machty committed Jul 10, 2024
1 parent b79e20a commit 66b53a3
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/volar/gts-language-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ export function createGtsLanguagePlugin<T extends URI | string>(
},

// 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -17,7 +18,6 @@ interface EmbeddedCodeWithDirectives extends VirtualCode {
* components are only supported when using `ember-loose` environment.
*/
export class LooseModeBackingComponentClassVirtualCode implements VirtualCode {

embeddedCodes: EmbeddedCodeWithDirectives[] = [];

/**
Expand All @@ -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);
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 66b53a3

Please sign in to comment.