Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce @glint-ignore and @glint-expect-error directives #166

Merged
merged 13 commits into from
Apr 16, 2021
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions packages/core/__tests__/cli/check.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,42 @@ describe('CLI: single-pass typechecking', () => {
`);
});

test('reports correct diagnostics given @glint-expect-error and @glint-ignore directives', async () => {
project.write('.glintrc', 'environment: ember-loose\n');

let script = stripIndent`
import Component from '@glint/environment-ember-loose/ember-component';

export default class MyComponent extends Component {}
`;

let template = stripIndent`
{{! @glint-ignore }}
{{@message}},

{{! @glint-expect-error }}
{{this.target}}

{{! @glint-expect-error }}
Hello.
`;

project.write('my-component.ts', script);
project.write('my-component.hbs', template);

let checkResult = await project.check({ reject: false });

expect(checkResult.exitCode).toBe(1);
expect(checkResult.stdout).toEqual('');
expect(stripAnsi(checkResult.stderr)).toMatchInlineSnapshot(`
"my-component.hbs:7:1 - error TS0: Unused '@glint-expect-error' directive.

7 {{! @glint-expect-error }}
~~~~~~~~~~~~~~~~~~~~~~~~~~
"
`);
});

test('honors .glintrc configuration', async () => {
let code = stripIndent`
import Component, { hbs } from '@glint/environment-glimmerx/component';
Expand Down
40 changes: 40 additions & 0 deletions packages/core/__tests__/cli/watch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,44 @@ describe('CLI: watched typechecking', () => {

await watch.terminate();
});

test('reports correct diagnostics given @glint-expect-error and @glint-ignore directives', async () => {
project.write('.glintrc', 'environment: ember-loose\n');

let script = stripIndent`
import Component from '@glint/environment-ember-loose/ember-component';

export default class MyComponent extends Component {
// private target = 'world';
}
`;

let template = stripIndent`
{{! @glint-ignore }}
{{@message}},

{{! @glint-expect-error }}
{{this.target}}
`;

project.write('my-component.ts', script);
project.write('my-component.hbs', template);

let watch = project.watch();

let output = await watch.awaitOutput('Watching for file changes.');
expect(output).toMatch('Found 0 errors.');

project.write('my-component.ts', script.replace('// ', ''));

output = await watch.awaitOutput('Watching for file changes.');
expect(output).toMatch('Found 1 error.');

project.write('my-component.ts', script);

output = await watch.awaitOutput('Watching for file changes.');
expect(output).toMatch('Found 0 errors.');

await watch.terminate();
});
});
123 changes: 107 additions & 16 deletions packages/core/__tests__/language-server/diagnostics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,6 @@ describe('Language Server: Diagnostics', () => {

expect(diagnostics).toMatchInlineSnapshot(`
Array [
Object {
"message": "Property 'startupTimee' does not exist on type 'Application'. Did you mean 'startupTime'?",
"range": Object {
"end": Object {
"character": 43,
"line": 11,
},
"start": Object {
"character": 31,
"line": 11,
},
},
"severity": 1,
"source": "glint:ts(2551)",
"tags": Array [],
},
Object {
"message": "'startupTime' is declared but its value is never read.",
"range": Object {
Expand All @@ -72,6 +56,22 @@ describe('Language Server: Diagnostics', () => {
1,
],
},
Object {
"message": "Property 'startupTimee' does not exist on type 'Application'. Did you mean 'startupTime'?",
"range": Object {
"end": Object {
"character": 43,
"line": 11,
},
"start": Object {
"character": 31,
"line": 11,
},
},
"severity": 1,
"source": "glint:ts(2551)",
"tags": Array [],
},
]
`);

Expand Down Expand Up @@ -160,4 +160,95 @@ describe('Language Server: Diagnostics', () => {
expect(server.getDiagnostics(project.fileURI('index.ts'))).toEqual([]);
expect(server.getDiagnostics(project.fileURI('index.hbs'))).toEqual([]);
});

test('honors @glint-ignore and @glint-expect-error', () => {
let componentA = stripIndent`
import Component, { hbs } from '@glint/environment-glimmerx/component';

export default class ComponentA extends Component {
public static template = hbs\`
{{! @glint-expect-error }}
Welcome to app <code>v{{@version}}</code>.
\`;
}
`;

let componentB = stripIndent`
import Component, { hbs } from '@glint/environment-glimmerx/component';

export default class ComponentB extends Component {
public startupTime = new Date().toISOString();

public static template = hbs\`
{{! @glint-ignore: this looks like a typo but for some reason it isn't }}
The current time is {{this.startupTimee}}.
\`;
}
`;

project.write('component-a.ts', componentA);
project.write('component-b.ts', componentB);

let server = project.startLanguageServer();

expect(server.getDiagnostics(project.fileURI('component-a.ts'))).toEqual([]);
expect(server.getDiagnostics(project.fileURI('component-b.ts'))).toEqual([]);

server.openFile(project.fileURI('component-a.ts'), componentA);
server.updateFile(
project.fileURI('component-a.ts'),
componentA.replace('{{! @glint-expect-error }}', '')
);

expect(server.getDiagnostics(project.fileURI('component-b.ts'))).toEqual([]);
expect(server.getDiagnostics(project.fileURI('component-a.ts'))).toMatchInlineSnapshot(`
Array [
Object {
"message": "Property 'version' does not exist on type 'EmptyObject'.",
"range": Object {
"end": Object {
"character": 36,
"line": 5,
},
"start": Object {
"character": 29,
"line": 5,
},
},
"severity": 1,
"source": "glint:ts(2339)",
"tags": Array [],
},
]
`);

server.updateFile(project.fileURI('component-a.ts'), componentA);

expect(server.getDiagnostics(project.fileURI('component-a.ts'))).toEqual([]);
expect(server.getDiagnostics(project.fileURI('component-b.ts'))).toEqual([]);

server.updateFile(project.fileURI('component-a.ts'), componentA.replace('{{@version}}', ''));

expect(server.getDiagnostics(project.fileURI('component-b.ts'))).toEqual([]);
expect(server.getDiagnostics(project.fileURI('component-a.ts'))).toMatchInlineSnapshot(`
Array [
Object {
"message": "Unused '@glint-expect-error' directive.",
"range": Object {
"end": Object {
"character": 30,
"line": 4,
},
"start": Object {
"character": 4,
"line": 4,
},
},
"severity": 1,
"source": "glint",
"tags": Array [],
},
]
`);
});
});
14 changes: 14 additions & 0 deletions packages/core/src/cli/diagnostics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type ts from 'typescript';

export function buildDiagnosticFormatter(
ts: typeof import('typescript')
): (diagnostic: ts.Diagnostic) => string {
const formatDiagnosticHost: ts.FormatDiagnosticsHost = {
getCanonicalFileName: (name) => name,
getCurrentDirectory: ts.sys.getCurrentDirectory,
getNewLine: () => ts.sys.newLine,
};

return (diagnostic) =>
ts.formatDiagnosticsWithColorAndContext([diagnostic], formatDiagnosticHost);
}
11 changes: 7 additions & 4 deletions packages/core/src/cli/perform-check.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type ts from 'typescript';
import TransformManager from '../common/transform-manager';
import { GlintConfig } from '@glint/config';
import { buildDiagnosticFormatter } from './diagnostics';

export function performCheck(
ts: typeof import('typescript'),
Expand All @@ -12,6 +13,7 @@ export function performCheck(
let transformManager = new TransformManager(ts, glintConfig);
let parsedConfig = loadTsconfig(ts, configPath, optionsToExtend);
let compilerHost = createCompilerHost(ts, parsedConfig.options, transformManager);
let formatDiagnostic = buildDiagnosticFormatter(ts);
let program = ts.createProgram({
rootNames: rootNames.length ? rootNames : parsedConfig.fileNames,
options: parsedConfig.options,
Expand All @@ -20,12 +22,13 @@ export function performCheck(

program.emit();

let diagnostics = collectDiagnostics(program, transformManager, parsedConfig.options);
for (let diagnostic of diagnostics) {
console.error(transformManager.formatDiagnostic(diagnostic));
let baselineDiagnostics = collectDiagnostics(program, transformManager, parsedConfig.options);
let fullDiagnostics = transformManager.rewriteDiagnostics(baselineDiagnostics);
for (let diagnostic of fullDiagnostics) {
console.error(formatDiagnostic(diagnostic));
}

process.exit(diagnostics.length ? 1 : 0);
process.exit(fullDiagnostics.length ? 1 : 0);
}

function collectDiagnostics(
Expand Down
12 changes: 10 additions & 2 deletions packages/core/src/cli/perform-watch.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import TransformManager from '../common/transform-manager';
import { GlintConfig } from '@glint/config';
import { buildDiagnosticFormatter } from './diagnostics';

export function performWatch(
ts: typeof import('typescript'),
Expand All @@ -8,12 +9,13 @@ export function performWatch(
optionsToExtend: import('typescript').CompilerOptions
): void {
let transformManager = new TransformManager(ts, glintConfig);
let formatDiagnostic = buildDiagnosticFormatter(ts);
let host = ts.createWatchCompilerHost(
tsconfigPath ?? 'tsconfig.json',
optionsToExtend,
sysForWatchCompilerHost(ts, transformManager),
ts.createSemanticDiagnosticsBuilderProgram,
(diagnostic) => console.error(transformManager.formatDiagnostic(diagnostic))
(diagnostic) => console.error(formatDiagnostic(diagnostic))
);

patchWatchCompilerHost(host, transformManager);
Expand Down Expand Up @@ -44,10 +46,16 @@ function patchWatchCompilerHost(host: WatchCompilerHost, transformManager: Trans
}

function patchProgram(program: Program, transformManager: TransformManager): void {
let { getSyntacticDiagnostics } = program;
let { getSyntacticDiagnostics, getSemanticDiagnostics } = program;

program.getSyntacticDiagnostics = function (sourceFile, cancelationToken) {
let diagnostics = getSyntacticDiagnostics.call(program, sourceFile, cancelationToken);
let transformDiagnostics = transformManager.getTransformDiagnostics(sourceFile?.fileName);
return [...diagnostics, ...transformDiagnostics];
};

program.getSemanticDiagnostics = (sourceFile, cancellationToken) => {
let diagnostics = getSemanticDiagnostics.call(program, sourceFile, cancellationToken);
return transformManager.rewriteDiagnostics(diagnostics, sourceFile?.fileName);
};
}
Loading