Skip to content

Commit

Permalink
Wire module resolution updates into the language server
Browse files Browse the repository at this point in the history
  • Loading branch information
dfreeman committed Sep 24, 2023
1 parent 9e3166e commit 863ba0d
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
57 changes: 57 additions & 0 deletions packages/core/__tests__/language-server/custom-extensions.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Project } from 'glint-monorepo-test-utils';
import { describe, beforeEach, afterEach, test, expect } from 'vitest';
import { stripIndent } from 'common-tags';
import typescript from 'typescript';
import semver from 'semver';

describe('Language Server: custom file extensions', () => {
let project!: Project;
Expand Down Expand Up @@ -254,4 +256,59 @@ describe('Language Server: custom file extensions', () => {
]);
});
});

describe('module resolution with explicit extensions', () => {
beforeEach(() => {
project.setGlintConfig({ environment: 'ember-template-imports' });
project.write({
'index.gts': stripIndent`
import Greeting from './Greeting.gts';
<template><Greeting /></template>
`,
'Greeting.gts': stripIndent`
<template>Hello!</template>
`,
});
});

test('is illegal by default', async () => {
let server = project.startLanguageServer();

expect(server.getDiagnostics(project.fileURI('index.gts'))).toMatchInlineSnapshot(`
[
{
"code": 2307,
"message": "Cannot find module './Greeting.gts' or its corresponding type declarations.",
"range": {
"end": {
"character": 37,
"line": 0,
},
"start": {
"character": 21,
"line": 0,
},
},
"severity": 1,
"source": "glint",
"tags": [],
},
]
`);
});

test.runIf(semver.gte(typescript.version, '5.0.0'))(
'works with `allowImportingTsExtensions: true`',
async () => {
project.updateTsconfig((config) => {
config.compilerOptions ??= {};
config.compilerOptions['allowImportingTsExtensions'] = true;
});

let server = project.startLanguageServer();

expect(server.getDiagnostics(project.fileURI('index.gts'))).toEqual([]);
}
);
});
});
10 changes: 10 additions & 0 deletions packages/core/src/language-server/glint-language-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ export default class GlintLanguageServer {
fileExists: this.transformManager.fileExists,
readFile: this.transformManager.readTransformedFile,
readDirectory: this.transformManager.readDirectory,
// @ts-ignore: This hook was added in TS5, and is safely irrelevant in earlier versions. Once we drop support for 4.x, we can also remove this @ts-ignore comment.
resolveModuleNameLiterals: this.transformManager.resolveModuleNameLiterals,
getCompilationSettings: () => parsedConfig.options,
// Yes, this looks like a mismatch, but built-in lib declarations don't resolve
// correctly otherwise, and this is what the TS wiki uses in their code snippet.
Expand Down Expand Up @@ -119,6 +121,10 @@ export default class GlintLanguageServer {
if (filePath.startsWith(this.glintConfig.rootDir)) {
this.rootFileNames.add(this.transformManager.getScriptPathForTS(filePath));
}

// Adding or removing a file invalidates pretty much everything we might think we know
// about module resolution.
this.transformManager.moduleResolutionCache.clear();
}

public watchedFileDidChange(uri: string): void {
Expand All @@ -136,6 +142,10 @@ export default class GlintLanguageServer {
if (!companionPath || this.glintConfig.getSynthesizedScriptPathForTS(companionPath) !== path) {
this.rootFileNames.delete(this.glintConfig.getSynthesizedScriptPathForTS(path));
}

// Adding or removing a file invalidates pretty much everything we might think we know
// about module resolution.
this.transformManager.moduleResolutionCache.clear();
}

public getDiagnostics(uri: string): Array<Diagnostic> {
Expand Down

0 comments on commit 863ba0d

Please sign in to comment.