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

Validate assignability rules for ComponentLike and friends #470

Merged
merged 4 commits into from
Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,7 @@ describe('Language Server: Diagnostic Augmentation', () => {
},
{
"message": "The {{component}} helper can't be used to directly invoke a component under Glint. Consider first binding the result to a variable, e.g. '{{#let (component 'component-name') as |ComponentName|}}' and then invoking it as '<ComponentName @arg={{value}} />'.
Argument of type 'Invokable<(named?: PrebindArgs<{ message?: string | undefined; }, \\"message\\"> | undefined) => ComponentReturn<FlattenBlockParams<{ default: { Params: { Positional: []; }; }; }>, null>>' is not assignable to parameter of type 'ContentValue'.",
Argument of type 'Invokable<(named?: PrebindArgs<{ message?: string | undefined; }, \\"message\\"> | undefined) => ComponentReturn<FlattenBlockParams<{ default: { Params: { Positional: []; }; }; }>, unknown>>' is not assignable to parameter of type 'ContentValue'.",
"range": {
"end": {
"character": 41,
Expand Down Expand Up @@ -827,7 +827,7 @@ describe('Language Server: Diagnostic Augmentation', () => {
},
{
"message": "The {{component}} helper can't be used to directly invoke a component under Glint. Consider first binding the result to a variable, e.g. '{{#let (component 'component-name') as |ComponentName|}}' and then invoking it as '<ComponentName @arg={{value}}>...</ComponentName>'.
Argument of type 'Invokable<(named?: PrebindArgs<{ message?: string | undefined; }, \\"message\\"> | undefined) => ComponentReturn<FlattenBlockParams<{ default: { Params: { Positional: []; }; }; }>, null>>' is not assignable to parameter of type 'ComponentReturn<any, any>'.",
Argument of type 'Invokable<(named?: PrebindArgs<{ message?: string | undefined; }, \\"message\\"> | undefined) => ComponentReturn<FlattenBlockParams<{ default: { Params: { Positional: []; }; }; }>, unknown>>' is not assignable to parameter of type 'ComponentReturn<any, any>'.",
"range": {
"end": {
"character": 56,
Expand Down
8 changes: 4 additions & 4 deletions packages/core/__tests__/language-server/diagnostics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe('Language Server: Diagnostics', () => {
expect(templateDiagnostics).toMatchInlineSnapshot(`
[
{
"message": "Property 'missingArg' does not exist on type 'EmptyObject'.",
"message": "Property 'missingArg' does not exist on type '{}'.",
"range": {
"end": {
"character": 13,
Expand Down Expand Up @@ -118,7 +118,7 @@ describe('Language Server: Diagnostics', () => {

expect(diagnostics).toMatchObject([
{
message: "Property 'foo' does not exist on type 'EmptyObject'.",
message: "Property 'foo' does not exist on type '{}'.",
source: 'glint:ts(2339)',
},
]);
Expand Down Expand Up @@ -159,7 +159,7 @@ describe('Language Server: Diagnostics', () => {

expect(diagnostics).toMatchObject([
{
message: "Property 'foo' does not exist on type 'EmptyObject'.",
message: "Property 'foo' does not exist on type '{}'.",
source: 'glint:ts(2339)',
},
]);
Expand Down Expand Up @@ -358,7 +358,7 @@ describe('Language Server: Diagnostics', () => {
expect(server.getDiagnostics(project.fileURI('component-a.ts'))).toMatchInlineSnapshot(`
[
{
"message": "Property 'version' does not exist on type 'EmptyObject'.",
"message": "Property 'version' does not exist on type '{}'.",
"range": {
"end": {
"character": 36,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import { ComponentLike, HelperLike, ModifierLike } from '@glint/template';
import {
Context,
EmptyObject,
FlattenBlockParams,
HasContext,
TemplateContext,
Expand Down Expand Up @@ -85,15 +84,15 @@ declare module '@ember/routing/route' {
[Context]: TemplateContext<
Controller & ModelField<ModelForRoute<this>>,
ModelField<ModelForRoute<this>>,
EmptyObject,
{},
null
>;
}
}

declare module '@ember/controller' {
export default interface Controller {
[Context]: TemplateContext<this, ModelField<this['model']>, EmptyObject, null>;
[Context]: TemplateContext<this, ModelField<this['model']>, {}, null>;
}
}

Expand All @@ -103,9 +102,7 @@ declare module '@ember/controller' {
import '@ember/test-helpers';
import 'ember-cli-htmlbars';

type TestTemplate<T> = abstract new () => HasContext<
TemplateContext<T, EmptyObject, EmptyObject, void>
>;
type TestTemplate<T> = abstract new () => HasContext<TemplateContext<T, {}, {}, void>>;

declare module '@ember/test-helpers' {
export function render<T>(template: TestTemplate<T>): Promise<void>;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { WithBoundArgs } from '@glint/template';
import {
ComponentReturn,
EmptyObject,
DirectInvokable,
InvokableInstance,
Invokable,
Expand All @@ -14,8 +13,8 @@ import {
type ComponentNamedArgs<Component> = Component extends Invokable<(...args: infer Args) => any>
? Args extends [...positional: infer _, named?: infer Named]
? UnwrapNamedArgs<Named>
: EmptyObject
: EmptyObject;
: {}
: {};

type PartiallyAppliedComponent<Component, Args> = Component extends Invokable<AnyFunction>
? WithBoundArgs<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
NamedArgsMarker,
} from '@glint/environment-ember-loose/-private/dsl';
import { expectTypeOf } from 'expect-type';
import { EmptyObject } from '@glint/template/-private/integration';
import type { ComponentLike } from '@glint/template';

{
Expand All @@ -19,12 +18,6 @@ import type { ComponentLike } from '@glint/template';
}
}

resolve(NoArgsComponent)({
// @ts-expect-error: extra named arg
chriskrycho marked this conversation as resolved.
Show resolved Hide resolved
foo: 'bar',
...NamedArgsMarker,
});

resolve(NoArgsComponent)(
// @ts-expect-error: extra positional arg
'oops'
Expand All @@ -50,7 +43,7 @@ import type { ComponentLike } from '@glint/template';
templateForBackingValue(this, function* (𝚪) {
expectTypeOf(𝚪.this.foo).toEqualTypeOf<string>();
expectTypeOf(𝚪.this).toEqualTypeOf<StatefulComponent>();
expectTypeOf(𝚪.args).toEqualTypeOf<EmptyObject>();
expectTypeOf(𝚪.args).toEqualTypeOf<{}>();
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,12 @@ import {
emitComponent,
NamedArgsMarker,
} from '@glint/environment-ember-loose/-private/dsl';
import { EmptyObject } from '@glint/template/-private/integration';
import { expectTypeOf } from 'expect-type';
import { ComponentLike } from '@glint/template';

{
class NoArgsComponent extends Component {}

resolve(NoArgsComponent)({
// @ts-expect-error: extra named arg
chriskrycho marked this conversation as resolved.
Show resolved Hide resolved
foo: 'bar',
...NamedArgsMarker,
});

resolve(NoArgsComponent)(
// @ts-expect-error: extra positional arg
'oops'
Expand All @@ -44,7 +37,7 @@ import { ComponentLike } from '@glint/template';
templateForBackingValue(this, function* (𝚪) {
expectTypeOf(𝚪.this.foo).toEqualTypeOf<string>();
expectTypeOf(𝚪.this).toEqualTypeOf<StatefulComponent>();
expectTypeOf(𝚪.args).toEqualTypeOf<EmptyObject>();
expectTypeOf(𝚪.args).toEqualTypeOf<{}>();
});
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import Helper, { helper, EmptyObject } from '@ember/component/helper';
import Helper, { helper } from '@ember/component/helper';
import { resolve } from '@glint/environment-ember-loose/-private/dsl';
import {
NamedArgsMarker,
resolve as resolveWithoutFunctionResolution,
} from '@glint/environment-ember-loose/-private/dsl/without-function-resolution';
import { expectTypeOf } from 'expect-type';
import { HelperLike } from '@glint/template';
import { EmptyObject as GlintEmptyObject, NamedArgs } from '@glint/template/-private/integration';
import { NamedArgs } from '@glint/template/-private/integration';

// Functional helper: fixed signature params
{
Expand Down Expand Up @@ -56,7 +56,9 @@ import { EmptyObject as GlintEmptyObject, NamedArgs } from '@glint/template/-pri
let definition = helper(<T, U>([a, b]: [T, U]) => a || b);
let or = resolve(definition);

expectTypeOf(or).toEqualTypeOf<{ <T, U>(t: T, u: U, named?: NamedArgs<EmptyObject>): T | U }>();
// Using `toMatch` rather than `toEqual` because helper resolution (currently)
chriskrycho marked this conversation as resolved.
Show resolved Hide resolved
// uses a special `EmptyObject` type to represent empty named args.
expectTypeOf(or).toMatchTypeOf<{ <T, U>(t: T, u: U, named?: NamedArgs<{}>): T | U }>();

or('a', 'b', {
// @ts-expect-error: extra named arg
Expand Down Expand Up @@ -158,12 +160,12 @@ import { EmptyObject as GlintEmptyObject, NamedArgs } from '@glint/template/-pri
let repeat = resolve(RepeatHelper);

expectTypeOf(repeat).toEqualTypeOf<{
<T>(value: T, count?: number | undefined, args?: NamedArgs<GlintEmptyObject>): Array<T>;
<T>(value: T, count?: number | undefined): Array<T>;
dfreeman marked this conversation as resolved.
Show resolved Hide resolved
}>();

repeat(
'hello',
// @ts-expect-error: extra named arg
// @ts-expect-error: unexpected named args
{ word: 'hi', ...NamedArgsMarker }
);

Expand All @@ -190,9 +192,7 @@ import { EmptyObject as GlintEmptyObject, NamedArgs } from '@glint/template/-pri

let maybeString = resolve(MaybeStringHelper);

expectTypeOf(maybeString).toEqualTypeOf<
(args?: NamedArgs<GlintEmptyObject>) => string | undefined
>();
expectTypeOf(maybeString).toEqualTypeOf<() => string | undefined>();
}

// Helpers are `HelperLike`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ expectTypeOf(fn(mut('hello'))).toEqualTypeOf<(value: string) => void>();
// @ts-expect-error: missing value
mut();

// @ts-expect-error: unexpected named args
chriskrycho marked this conversation as resolved.
Show resolved Hide resolved
mut('hello', {
// @ts-expect-error: invalid named arg
hello: 'hi',
...NamedArgsMarker,
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ expectTypeOf(outlet('outlet-name')).toEqualTypeOf<void>();
// Nameless main outlet
outlet();

// @ts-expect-error: unexpected named args
outlet('outlet-name', {
// @ts-expect-error: invalid named arg
hello: 'hi',
...NamedArgsMarker,
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ expectTypeOf(unbound(123)).toEqualTypeOf<number>();
// @ts-expect-error: missing value
unbound();

// @ts-expect-error: unexpected named args
unbound('hello', {
// @ts-expect-error: invalid named arg
hello: 'hi',
...NamedArgsMarker,
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ let uniqueId = resolve(Globals['unique-id']);
// Basic plumbing
expectTypeOf(uniqueId()).toEqualTypeOf<string>();

// @ts-expect-error: unexpected named args
uniqueId({
// @ts-expect-error: invalid named arg
hello: 'hi',
...NamedArgsMarker,
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Route from '@ember/routing/route';
import Controller from '@ember/controller';
import { expectTypeOf } from 'expect-type';
import { EmptyObject } from '@glint/template/-private/integration';
import { templateForBackingValue } from '../../-private/dsl';

class TestRoute extends Route {
Expand All @@ -14,7 +13,7 @@ templateForBackingValue(TestRoute, function (routeContext) {
expectTypeOf(routeContext.args).toEqualTypeOf<{ model: { message: string } }>();
expectTypeOf(routeContext.element).toBeNull();
expectTypeOf(routeContext.this).toEqualTypeOf<Controller & { model: { message: string } }>();
expectTypeOf(routeContext.blocks).toEqualTypeOf<EmptyObject>();
expectTypeOf(routeContext.blocks).toEqualTypeOf<{}>();
});

class TestController extends Controller {
Expand All @@ -29,5 +28,5 @@ templateForBackingValue(TestController, function (controllerContext) {
expectTypeOf(controllerContext.args).toEqualTypeOf<{ model: { name: string; age: number } }>();
expectTypeOf(controllerContext.element).toBeNull();
expectTypeOf(controllerContext.this).toEqualTypeOf<TestController>();
expectTypeOf(controllerContext.blocks).toEqualTypeOf<EmptyObject>();
expectTypeOf(controllerContext.blocks).toEqualTypeOf<{}>();
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,15 @@ import {
emitComponent,
NamedArgsMarker,
} from '@glint/environment-ember-loose/-private/dsl';
import { ComponentReturn, EmptyObject, NamedArgs } from '@glint/template/-private/integration';
import { ComponentReturn, NamedArgs } from '@glint/template/-private/integration';
import { expectTypeOf } from 'expect-type';
import { ComponentKeyword } from '../../-private/intrinsics/component';
import { ComponentLike, WithBoundArgs } from '@glint/template';

{
const NoArgsComponent = templateOnlyComponent();

resolve(NoArgsComponent)({
// @ts-expect-error: extra named arg
foo: 'bar',
...NamedArgsMarker,
});

resolve(NoArgsComponent)(
{ ...NamedArgsMarker },
// @ts-expect-error: extra positional arg
'oops'
);
Expand All @@ -38,9 +31,9 @@ import { ComponentLike, WithBoundArgs } from '@glint/template';

templateForBackingValue(NoArgsComponent, function (𝚪) {
expectTypeOf(𝚪.this).toBeNull();
expectTypeOf(𝚪.args).toEqualTypeOf<EmptyObject>();
expectTypeOf(𝚪.element).toBeNull();
expectTypeOf(𝚪.blocks).toEqualTypeOf<EmptyObject>();
expectTypeOf(𝚪.args).toEqualTypeOf<{}>();
expectTypeOf(𝚪.element).toBeUnknown();
expectTypeOf(𝚪.blocks).toEqualTypeOf<{}>();
});
}

Expand Down Expand Up @@ -124,15 +117,15 @@ import { ComponentLike, WithBoundArgs } from '@glint/template';

const CurriedWithNothing = resolve(componentKeyword)('curried-component');
expectTypeOf(resolve(CurriedWithNothing)).toEqualTypeOf<
(args: NamedArgs<{ a: string; b: number }>) => ComponentReturn<EmptyObject>
(args: NamedArgs<{ a: string; b: number }>) => ComponentReturn<{}>
>();

const CurriedWithA = resolve(componentKeyword)('curried-component', {
a: 'hi',
...NamedArgsMarker,
});
expectTypeOf(resolve(CurriedWithA)).toEqualTypeOf<
(args: NamedArgs<{ a?: string; b: number }>) => ComponentReturn<EmptyObject>
(args: NamedArgs<{ a?: string; b: number }>) => ComponentReturn<{}>
>();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
AnyContext,
AnyFunction,
DirectInvokable,
EmptyObject,
HasContext,
InvokableInstance,
Invoke,
Expand Down Expand Up @@ -38,8 +37,8 @@ export declare const resolveOrReturn: ResolveOrReturn<typeof resolve>;
import { TemplateOnlyComponent } from '@ember/component/template-only';

export declare function templateExpression<
Signature extends AnyFunction = () => ComponentReturn<EmptyObject>,
Context extends AnyContext = TemplateContext<void, EmptyObject, EmptyObject, void>
Signature extends AnyFunction = () => ComponentReturn<{}>,
Context extends AnyContext = TemplateContext<void, {}, {}, void>
>(
f: (𝚪: Context, χ: never) => void
): TemplateOnlyComponent<never> &
Expand Down
5 changes: 2 additions & 3 deletions packages/environment-glimmerx/-private/dsl/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import {
AnyContext,
AnyFunction,
DirectInvokable,
EmptyObject,
HasContext,
InvokableInstance,
Invoke,
Expand All @@ -54,8 +53,8 @@ export declare const resolveOrReturn: ResolveOrReturn<typeof resolve>;
import { TemplateComponentInstance } from '@glimmerx/component';

export declare function templateExpression<
Signature extends AnyFunction = () => ComponentReturn<EmptyObject>,
Context extends AnyContext = TemplateContext<void, EmptyObject, EmptyObject, void>
Signature extends AnyFunction = () => ComponentReturn<{}>,
Context extends AnyContext = TemplateContext<void, {}, {}, void>
>(
f: (𝚪: Context, χ: never) => void
): abstract new () => TemplateComponentInstance<never> &
Expand Down
Loading