Skip to content

Commit

Permalink
Merge pull request #177 from glimmerjs/application-ts-strict
Browse files Browse the repository at this point in the history
Fix TS strict mode errors in @glimmer/application
  • Loading branch information
rwjblue committed Mar 25, 2019
2 parents af7c10d + ecd7a29 commit b9a0165
Show file tree
Hide file tree
Showing 17 changed files with 45 additions and 43 deletions.
14 changes: 7 additions & 7 deletions packages/@glimmer/application/src/application.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Resolver } from '@glimmer/di';
import { UpdatableReference } from '@glimmer/component';
import { Option, assert } from '@glimmer/util';
import { Option, assert, expect } from '@glimmer/util';
import { DefaultDynamicScope } from '@glimmer/runtime';
import { SimpleDocument } from '@simple-dom/interface';

Expand All @@ -18,7 +18,7 @@ export interface ApplicationOptions {
loader: Loader;
renderer: Renderer;
rootName: string;
resolver?: Resolver;
resolver: Resolver;
document?: SimpleDocument;
}

Expand Down Expand Up @@ -52,15 +52,15 @@ const DEFAULT_DOCUMENT = typeof document === 'object' ? (document as SimpleDocum
*/
export default class Application extends BaseApplication {
public document: SimpleDocument;
public env: Environment;
public env: Environment | null = null;

private _roots: AppRoot[] = [];
private _rootsIndex = 0;

/** @hidden
* The root Reference whose value provides the context of the main template.
*/
private _self: UpdatableReference<{ roots: AppRoot[] }>;
private _self: UpdatableReference<{ roots: AppRoot[] }> | null = null;

protected _rendering = false;
protected _rendered = false;
Expand All @@ -79,7 +79,7 @@ export default class Application extends BaseApplication {
});

assert(options.builder, 'Must provide a Builder that is responsible to building DOM.');
const document = (this.document = options.document || DEFAULT_DOCUMENT);
const document = (this.document = options.document || expect(DEFAULT_DOCUMENT, 'You must pass a document to the Application constructor in non-browser environments.'));
this.builder = options.builder;

this.registerInitializer({
Expand Down Expand Up @@ -149,7 +149,7 @@ export default class Application extends BaseApplication {

/** @internal */
protected async _render(): Promise<void> {
let { env } = this;
const env = expect(this.env, 'Unexpected missing environment during render');

// Create the template context for the root `main` template, which just
// contains the array of component roots. Any property references in that
Expand Down Expand Up @@ -195,7 +195,7 @@ export default class Application extends BaseApplication {
* @internal
*/
protected async _rerender() {
let { env } = this;
const env = expect(this.env, 'Unexpected missing environment during re-render');

try {
env.begin();
Expand Down
6 changes: 3 additions & 3 deletions packages/@glimmer/application/src/base-application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,10 @@ export interface BaseApplicationOptions {
export default abstract class BaseApplication implements Owner {
public rootName: string;
public resolver: Resolver;
readonly document: SimpleDocument;
readonly document!: SimpleDocument;

private _registry: Registry;
private _container: Container;
private _registry!: Registry;
private _container!: Container;
private _initializers: Initializer[] = [];
private _environment: FactoryDefinition<Environment>;

Expand Down
2 changes: 1 addition & 1 deletion packages/@glimmer/application/src/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export interface EnvironmentOptions {
/** @internal */
export default class EnvironmentImpl extends GlimmerEnvironmentImpl implements Environment {
private uselessAnchor: HTMLAnchorElement;
public resolver: RuntimeResolver;
public resolver!: RuntimeResolver;

static create(options: Partial<EnvironmentOptions> = {}) {
options.document = options.document || self.document;
Expand Down
2 changes: 1 addition & 1 deletion packages/@glimmer/application/src/helpers/user-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default function buildUserHelper(helperFunc: UserHelper): GlimmerHelper {
}

export class HelperReference extends CachedReference<unknown> {
public tag: TagWrapper<RevisionTag>;
public tag: TagWrapper<RevisionTag | null>;
private args: CapturedArguments;

constructor(private helper: UserHelper, args: VMArguments) {
Expand Down
6 changes: 3 additions & 3 deletions packages/@glimmer/application/src/iterable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class ArrayIterator implements OpaqueIterator {
return this.array.length === 0;
}

next(): IterationItem<unknown, number> {
next(): IterationItem<unknown, number> | null {
let { position, array, keyFor } = this;

if (position >= array.length) return null;
Expand Down Expand Up @@ -57,7 +57,7 @@ class ObjectKeysIterator implements OpaqueIterator {
return this.keys.length === 0;
}

next(): IterationItem<unknown, string> {
next(): IterationItem<unknown, string> | null {
let { position, keys, values, keyFor } = this;

if (position >= keys.length) return null;
Expand Down Expand Up @@ -94,7 +94,7 @@ export function iterableFor(ref: Reference<unknown>, keyPath: string): OpaqueIte

switch (keyPath) {
case '@index':
keyFor = (_, index: number) => String(index);
keyFor = (_, index: unknown) => String(index);
break;
case '@primitive':
keyFor = (item: unknown) => String(item);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export interface SerializedHeap {
}

export interface Metadata {
[key: string]: number | ProgramSymbolTable;
[key: string]: number | ProgramSymbolTable | undefined;

/** VM handle */
v?: number;
Expand Down
6 changes: 3 additions & 3 deletions packages/@glimmer/application/src/renderers/async-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const DEFAULT_TIMEOUT = 250;
*/
export default class AsyncRenderer implements Renderer {
public timeout: number;
protected result: RenderResult;
protected result: RenderResult | null = null;

constructor(options: AsyncRendererOptions = {}) {
this.timeout = options.timeout || DEFAULT_TIMEOUT;
Expand All @@ -62,14 +62,14 @@ export default class AsyncRenderer implements Renderer {
let timeout = this.timeout;

let tick = (deadline: Deadline) => {
let iteratorResult: IteratorResult<RenderResult>;
let iteratorResult: IteratorResult<RenderResult | null>;

do {
iteratorResult = iterator.next();
} while (!iteratorResult.done && deadline.timeRemaining() > 1);

if (iteratorResult.done) {
this.result = iteratorResult.value;
this.result = iteratorResult.value!;
return resolve();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { RenderResult, TemplateIterator } from '@glimmer/interfaces';
* @public
*/
export default class SyncRenderer implements Renderer {
result: RenderResult;
result: RenderResult | null = null;

render(iterator: TemplateIterator): void {
// Iterate the template iterator, executing the compiled template program
Expand Down
2 changes: 1 addition & 1 deletion packages/@glimmer/application/src/templates/main.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SerializedTemplateWithLazyBlock } from '@glimmer/interfaces';

declare const _default: SerializedTemplateWithLazyBlock;
declare const _default: SerializedTemplateWithLazyBlock<unknown>;

export default _default;
12 changes: 6 additions & 6 deletions packages/@glimmer/application/test/browser/action-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ test('can curry arguments to actions', async function(assert) {
@tracked
name = 'world';

constructor(owner, args) {
constructor(owner: any, args: any) {
super(owner, args);
helloWorldComponent = this;
}
Expand All @@ -41,20 +41,20 @@ test('can curry arguments to actions', async function(assert) {
assert.strictEqual(root.innerText, 'Hello World');

let h1 = root.querySelector('h1');
h1.onclick(fakeEvent);
h1!.onclick!(fakeEvent);

assert.strictEqual(passedMsg1, 'hello');
assert.strictEqual(passedMsg2, 'world');
assert.strictEqual(passedEvent, fakeEvent);
passedEvent = null;

helloWorldComponent.name = 'cruel world';
helloWorldComponent!.name = 'cruel world';
app.scheduleRerender();

await didRender(app);

h1 = root.querySelector('h1');
h1.onclick(fakeEvent);
h1!.onclick!(fakeEvent);

assert.strictEqual(passedMsg1, 'hello');
assert.strictEqual(passedMsg2, 'cruel world');
Expand All @@ -73,7 +73,7 @@ test('actions can be passed and invoked with additional arguments', async functi
class ParentComponent extends Component {
name = 'world';

constructor(owner, args) {
constructor(owner: any, args: any) {
super(owner, args);
parentComponent = this;
}
Expand Down Expand Up @@ -103,7 +103,7 @@ test('actions can be passed and invoked with additional arguments', async functi
let root = app.rootElement as Element;

let h1 = root.querySelector('.grandchild') as HTMLElement;
h1.onclick(fakeEvent);
h1!.onclick!(fakeEvent);

assert.deepEqual(passed, [1, 2, 3, 4, 5, 6, fakeEvent]);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ test('#factoryFor - returns a registered factory', function(assert) {
let app = createApp({ rootName: 'app', resolver: new BlankResolver() });

app.registerInitializer({
initialize(app) {
initialize(app: any) {
app.register('component:/app/components/date-picker', DatePicker);
},
});
Expand Down Expand Up @@ -123,7 +123,7 @@ test('#factoryFor - will use a resolver to locate a factory, even if one is regi

let app = createApp({ rootName: 'app', resolver });
app.registerInitializer({
initialize(app) {
initialize(app: any) {
app.register('foo:/app/foos/bar', Foo);
},
});
Expand All @@ -149,7 +149,7 @@ test('#lookup - returns an instance created by the factory', function(assert) {

let app = createApp({ rootName: 'app', resolver: new BlankResolver() });
app.registerInitializer({
initialize(app) {
initialize(app: any) {
app.register('foo:/app/foos/bar', FooBar);
},
});
Expand All @@ -174,7 +174,7 @@ test('#lookup - caches looked up instances by default', function(assert) {

let app = createApp({ rootName: 'app', resolver: new BlankResolver() });
app.registerInitializer({
initialize(app) {
initialize(app: any) {
app.register('foo:/app/foos/bar', FooBar);
},
});
Expand Down Expand Up @@ -203,7 +203,7 @@ test('#lookup - will not cache lookups specified as non-singletons', function(as

let app = createApp({ rootName: 'app', resolver: new BlankResolver() });
app.registerInitializer({
initialize(app) {
initialize(app: any) {
app.register('foo:/app/foos/bar', FooBar, { singleton: false });
},
});
Expand All @@ -224,7 +224,7 @@ test('#lookup - returns the factory when registrations specify instantiate: fals

let app = createApp({ rootName: 'app', resolver: new BlankResolver() });
app.registerInitializer({
initialize(app) {
initialize(app: any) {
app.register('foo:/app/foos/bar', factory, { instantiate: false });
},
});
Expand Down Expand Up @@ -292,7 +292,7 @@ test('#lookup - injects references registered by name', function(assert) {
let app = createApp({ rootName: 'app', resolver: new BlankResolver() });

app.registerInitializer({
initialize(app) {
initialize(app: any) {
app.register('foo:/app/foos/bar', FooBar);
app.register('router:/app/root/main', Router);
app.registerInjection('foo:/app/foos/bar', 'router', 'router:/app/root/main');
Expand Down Expand Up @@ -330,7 +330,7 @@ test('#lookup - injects references registered by type', function(assert) {
let app = createApp({ rootName: 'app', resolver: new BlankResolver() });

app.registerInitializer({
initialize(app) {
initialize(app: any) {
app.register('foo:/app/foos/bar', FooBar);
app.register('router:/app/root/main', Router);
app.registerInjection('foo:/app/foos/bar', 'router', 'router:/app/root/main');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ test('can be booted with bytecode loader', async function(assert) {
},
pool: result.pool,
table: [],
mainEntry: result.table.vmHandleByModuleLocator.get(locator),
mainEntry: result.table.vmHandleByModuleLocator.get(locator)!,
meta: {
mainTemplate: {
v: result.table.vmHandleByModuleLocator.get(locator),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ test('custom elements are rendered', async function(assert) {

test('components without a template raise an error', async function(assert) {
class HelloWorldComponent extends Component {
debugName: 'HelloWorld';
debugName = 'HelloWorld';
}

let app = await buildApp()
Expand All @@ -141,7 +141,7 @@ test('components without a template raise an error', async function(assert) {

test('components with dasherized names raise an error', function(assert) {
class HelloWorldComponent extends Component {
debugName: 'hello-world';
debugName = 'hello-world';
}

assert.throws(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,11 @@ class RenderComponentTest extends RenderTest {

containerElement.appendChild(nextSibling);

let component;
let component: any;

class HelloWorld extends Component {
@tracked a = 'a';
constructor(owner, args) {
constructor(owner: any, args: any) {
super(owner, args);
component = this;
}
Expand Down
1 change: 1 addition & 0 deletions packages/@glimmer/application/test/initializers-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ test('instance initializers run at initialization', function(assert) {
loader: new RuntimeCompilerLoader(resolver),
builder: new DOMBuilder({ element }),
renderer: new SyncRenderer(),
document: {} as any,
resolver,
});
app.registerInitializer({
Expand Down
4 changes: 2 additions & 2 deletions packages/@glimmer/application/test/iterable-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ test('basic iteration of an array of primitives', function(assert) {
return ['foo', 'bar'];
},
};
let keyFor = (_, i) => i;
let iterable = new Iterable(ref, keyFor);
let keyFor = (_: any, i: number) => i;
let iterable = new Iterable(ref, keyFor as any);
let iterator = iterable.iterate();

assert.deepEqual(iterator.next(), {
Expand Down
3 changes: 2 additions & 1 deletion packages/@glimmer/test-utils/src/resolvers.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Resolver, isSpecifierStringAbsolute } from '@glimmer/di';

export class BlankResolver implements Resolver {
identify(specifier: string, referrer?: string) {
identify(specifier: string, referrer?: string): string {
if (isSpecifierStringAbsolute(specifier)) {
return specifier;
}
throw new Error(`Unexpected non-absolute specifier ${specifier}`);
}
retrieve(specifier: string): any {}
}

0 comments on commit b9a0165

Please sign in to comment.