From 946ee43715d2dd4f1b139c213a357701da5286ae Mon Sep 17 00:00:00 2001 From: Peter Burns Date: Sat, 2 Mar 2019 12:02:11 -0800 Subject: [PATCH 1/2] Remove unnecessary type assertions This is the result of turning on the no-unnecessary-type-assertion tslint pass and running `--fix`. There was one place where I disabled this pass where we needed `!` to note that a variable was definitely assigned to before it was read. I like this lint pass because it makes the code a bit easier to read and understand. I will admit though that I'm proposing this change because we use this lint pass downstream, and it's annoying seeing the lint warnings. --- src/lib/decorators.ts | 8 ++++---- src/lib/updating-element.ts | 5 +++-- src/lit-element.ts | 8 ++++---- src/test/lib/decorators_test.ts | 2 +- src/test/lit-element_styling_test.ts | 8 ++++---- tslint.json | 1 + 6 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/lib/decorators.ts b/src/lib/decorators.ts index 47ce28b9..49203561 100644 --- a/src/lib/decorators.ts +++ b/src/lib/decorators.ts @@ -72,8 +72,8 @@ export const customElement = (tagName: string) => (classOrDescriptor: Constructor|ClassDescriptor) => (typeof classOrDescriptor === 'function') ? legacyCustomElement( - tagName, classOrDescriptor as Constructor) : - standardCustomElement(tagName, classOrDescriptor as ClassDescriptor); + tagName, classOrDescriptor) : + standardCustomElement(tagName, classOrDescriptor); const standardProperty = (options: PropertyDeclaration, element: ClassElement) => { @@ -109,7 +109,7 @@ const standardProperty = // tslint:disable-next-line:no-any decorator initializer(this: any) { if (typeof element.initializer === 'function') { - this[element.key] = element.initializer!.call(this); + this[element.key] = element.initializer.call(this); } }, finisher(clazz: typeof UpdatingElement) { @@ -122,7 +122,7 @@ const standardProperty = const legacyProperty = (options: PropertyDeclaration, proto: Object, name: PropertyKey) => { (proto.constructor as typeof UpdatingElement) - .createProperty(name!, options); + .createProperty(name, options); }; /** diff --git a/src/lib/updating-element.ts b/src/lib/updating-element.ts index cd8ac8bc..abb2fd93 100644 --- a/src/lib/updating-element.ts +++ b/src/lib/updating-element.ts @@ -296,8 +296,7 @@ export abstract class UpdatingElement extends HTMLElement { Object.defineProperty(this.prototype, name, { // tslint:disable-next-line:no-any no symbol in index get(): any { - // tslint:disable-next-line:no-any no symbol in index - return (this as any)[key]; + return this[key]; }, set(this: UpdatingElement, value: unknown) { // tslint:disable-next-line:no-any no symbol in index @@ -632,6 +631,8 @@ export abstract class UpdatingElement extends HTMLElement { typeof (result as PromiseLike).then === 'function') { await result; } + // TypeScript can't tell that we've initialized resolve. + // tslint:disable-next-line:no-unnecessary-type-assertion resolve!(!this._hasRequestedUpdate); } diff --git a/src/lit-element.ts b/src/lit-element.ts index 8fccb438..194616ca 100644 --- a/src/lit-element.ts +++ b/src/lit-element.ts @@ -117,7 +117,7 @@ export class LitElement extends UpdatingElement { return set; }, new Set()); // Array.from does not work on Set in IE - styleSet.forEach((v) => styles!.unshift(v)); + styleSet.forEach((v) => styles.unshift(v)); } else if (userStyles) { styles.push(userStyles); } @@ -214,8 +214,8 @@ export class LitElement extends UpdatingElement { (this.constructor as typeof LitElement) .render( templateResult, - this.renderRoot!, - {scopeName: this.localName!, eventContext: this}); + this.renderRoot, + {scopeName: this.localName, eventContext: this}); } // When native Shadow DOM is used but adoptedStyles are not supported, // insert styling after rendering to ensure adoptedStyles have highest @@ -225,7 +225,7 @@ export class LitElement extends UpdatingElement { (this.constructor as typeof LitElement)._styles!.forEach((s) => { const style = document.createElement('style'); style.textContent = s.cssText; - this.renderRoot!.appendChild(style); + this.renderRoot.appendChild(style); }); } } diff --git a/src/test/lib/decorators_test.ts b/src/test/lib/decorators_test.ts index 9ecbfcff..64d5cb35 100644 --- a/src/test/lib/decorators_test.ts +++ b/src/test/lib/decorators_test.ts @@ -372,7 +372,7 @@ suite('decorators', () => { const c = new C(); container.appendChild(c); await c.updateComplete; - const divs = c.divs!; + const divs = c.divs; // This is not true in ShadyDOM: // assert.instanceOf(divs, NodeList); assert.lengthOf(divs, 2); diff --git a/src/test/lit-element_styling_test.ts b/src/test/lit-element_styling_test.ts index 819db6e4..b32e1177 100644 --- a/src/test/lit-element_styling_test.ts +++ b/src/test/lit-element_styling_test.ts @@ -201,7 +201,7 @@ suite('Styling', () => { div = inner!.shadowRoot!.querySelector('div'); assert.equal( getComputedStyleValue(div!, 'border-top-width').trim(), '2px'); - el2!.shadowRoot!.appendChild(inner!); + el2.shadowRoot!.appendChild(inner!); // Workaround for Safari 9 Promise timing bugs. await el.updateComplete; @@ -307,14 +307,14 @@ suite('Styling', () => { // Workaround for Safari 9 Promise timing bugs. await firstApplied.updateComplete && el.updateComplete && - await (el.applied as I)!.updateComplete; + await (el.applied as I).updateComplete; await nextFrame(); assert.equal( - getComputedStyleValue(firstApplied!, 'border-top-width').trim(), + getComputedStyleValue(firstApplied, 'border-top-width').trim(), '2px'); assert.equal( - getComputedStyleValue(firstApplied!, 'margin-top').trim(), '10px'); + getComputedStyleValue(firstApplied, 'margin-top').trim(), '10px'); assert.equal( getComputedStyleValue(el.applied!, 'border-top-width').trim(), '10px'); diff --git a/tslint.json b/tslint.json index 7b4fc2a9..898af7e0 100644 --- a/tslint.json +++ b/tslint.json @@ -13,6 +13,7 @@ "no-internal-module": true, "no-trailing-whitespace": true, "no-var-keyword": true, + "no-unnecessary-type-assertion": true, "one-line": [ true, "check-open-brace", From 81aa9a0ddee8f78e6850428b73d1a814686118ab Mon Sep 17 00:00:00 2001 From: Peter Burns Date: Sat, 2 Mar 2019 12:15:37 -0800 Subject: [PATCH 2/2] Format. --- src/lib/decorators.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/lib/decorators.ts b/src/lib/decorators.ts index 49203561..43e8db9b 100644 --- a/src/lib/decorators.ts +++ b/src/lib/decorators.ts @@ -71,8 +71,7 @@ const standardCustomElement = export const customElement = (tagName: string) => (classOrDescriptor: Constructor|ClassDescriptor) => (typeof classOrDescriptor === 'function') ? - legacyCustomElement( - tagName, classOrDescriptor) : + legacyCustomElement(tagName, classOrDescriptor) : standardCustomElement(tagName, classOrDescriptor); const standardProperty =