Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

feat(ripple): convert to ts #4300

Merged
merged 12 commits into from
Jan 29, 2019
Merged

feat(ripple): convert to ts #4300

merged 12 commits into from
Jan 29, 2019

Conversation

moog16
Copy link
Contributor

@moog16 moog16 commented Jan 25, 2019

related #4225

@mdc-web-bot
Copy link
Collaborator

All 758 screenshot tests passed for commit 75cb9ba vs. feat/typescript! 💯🎉

@mdc-web-bot
Copy link
Collaborator

All 758 screenshot tests passed for commit 97fed6f vs. feat/typescript! 💯🎉

}

export default MDCRippleAdapter;
export {Point};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

export {MDCRippleAdapter as default, MDCRippleAdapter, Point};


containsEventTarget(target: EventTarget | null): boolean;

registerInteractionHandler<K extends keyof GlobalEventHandlersEventMap>(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ooh fancy! I didn't know this was a thing :)

I'd suggest putting some aliases in a common place somewhere to make the types more readable:

type EventType = keyof GlobalEventHandlersEventMap;
type SpecificEventListener<K extends EventType> = (evt: GlobalEventHandlersEventMap[K]) => void;

Then these would look like:

registerInteractionHandler<E extends EventType>(evtType: E, handler: SpecificEventListener<E>): void;
registerResizeHandler(handler: SpecificEventListener<'resize'>): void;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yaa I like that 👍

@mdc-web-bot
Copy link
Collaborator

All 758 screenshot tests passed for commit 79542ed vs. feat/typescript! 💯🎉

}

interface CssWindow extends Window {
CSS?: CssObject;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TS has a type CSS which is equivalent to your CssObject, it's just not on the Window for some reason. You might be able to make a file of type fixes somewhere, like this:

declare interface Window { CSS: CSS; }

You could also add a fix for the msMatchesSelector thing, might be nicer than having to invent your own types and cast them everywhere

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ya we need to invent a place for these global types. Maybe the correct place is in mdc-dom package. I'm going to add it to the index.ts for now. We can decide for a better place as this PR evolves.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also I found that this works:

declare global {
  interface Window {
    CSS: CSS;
  }
}

globals seems like a last ditch effort. Is that a valid pattern? Or is there a better way of accomplishing this?

* THE SOFTWARE.
*/

declare type EventType = keyof GlobalEventHandlersEventMap;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for these ones you might want to leave them in a .ts file and export them so users can use them too

@codecov-io
Copy link

codecov-io commented Jan 29, 2019

Codecov Report

Merging #4300 into feat/typescript will decrease coverage by 0.11%.
The diff coverage is 93.46%.

Impacted file tree graph

@@                 Coverage Diff                 @@
##           feat/typescript    #4300      +/-   ##
===================================================
- Coverage            98.57%   98.46%   -0.12%     
===================================================
  Files                   92       92              
  Lines                 5674     5718      +44     
  Branches               760      768       +8     
===================================================
+ Hits                  5593     5630      +37     
- Misses                  81       88       +7
Impacted Files Coverage Δ
packages/mdc-dom/ponyfill.ts 100% <100%> (ø) ⬆️
packages/mdc-ripple/foundation.ts 100% <100%> (ø)
packages/mdc-ripple/util.ts 86.76% <78.04%> (ø)
packages/mdc-ripple/index.ts 98.21% <96.87%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c649de5...b675f5c. Read the comment docs.

@@ -0,0 +1,131 @@
/**
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't know how this ended up here...removing

@mdc-web-bot
Copy link
Collaborator

All 758 screenshot tests passed for commit 7abb9d2 vs. feat/typescript! 💯🎉


deregisterDocumentInteractionHandler<K extends EventType>(evtType: K, handler: SpecificEventListener<K>): void;

registerResizeHandler(handler: (evt: GlobalEventHandlersEventMap['resize']) => void): void;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SpecificEventListener<'resize'>

@googlebot
Copy link

We found a Contributor License Agreement for you (the sender of this pull request), but were unable to find agreements for all the commit author(s) or Co-authors. If you authored these, maybe you used a different email address in the git commits than was used to sign the CLA (login here to double check)? If these were authored by someone else, then they will need to sign a CLA as well, and confirm that they're okay with these being contributed to Google.
In order to pass this check, please resolve this problem and have the pull request author add another comment and the bot will run again. If the bot doesn't comment, it means it doesn't think anything has changed.

@moog16
Copy link
Contributor Author

moog16 commented Jan 29, 2019

I signed it

@mdc-web-bot
Copy link
Collaborator

All 758 screenshot tests passed for commit b675f5c vs. feat/typescript! 💯🎉

@moog16 moog16 merged commit 47a9c38 into feat/typescript Jan 29, 2019
@moog16 moog16 deleted the feat/checkbox-ripple-ts branch January 29, 2019 06:55
@kfranqueiro kfranqueiro mentioned this pull request Jan 29, 2019
45 tasks
Copy link
Contributor

@kfranqueiro kfranqueiro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I missed the boat prior to this being merged but I have a few questions and a couple of cleanup comments.

if (typeof supportsCssVariables_ === 'boolean' && !forceRefresh) {
return supportsCssVariables;
return Boolean(supportsCssVariables_);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this really need this cast? The if is already confirming it's boolean...

Copy link
Contributor Author

@moog16 moog16 Jan 29, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TS Compiler was complaining without the case before. I think what I did was copy from rachel's CL and rewrote the logic.

}

const explicitlySupportsCssVars = windowObj.CSS.supports('--css-vars', 'yes');
const {CSS} = windowObj;
const explicitlySupportsCssVars = CSS
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The CSS && here and below seem redundant here because it's already checked above... is TS complaining without this?

}

const explicitlySupportsCssVars = windowObj.CSS.supports('--css-vars', 'yes');
const {CSS} = windowObj;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do the new typings files automatically get picked up by tsc with no configuration?

Copy link
Contributor Author

@moog16 moog16 Jan 29, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct if we don't define files or includes in the tsconfig.json file, TS defaults to finding all .d.ts files within the tsconfig's root directory.

https://www.typescriptlang.org/docs/handbook/tsconfig-json.

If the "files" and "include" are both left unspecified, the compiler defaults to including all TypeScript (.ts, .d.ts and .tsx) files in the containing directory and subdirectories except those excluded using the "exclude" property. JS files (.js and .jsx) are also included if allowJs is set to true. If the "files" or "include" properties are specified, the compiler will instead include the union of the files included by those two properties. Files in the directory specified using the "outDir" compiler option are excluded as long as "exclude" property is not specified.

*/
function getMatchesProperty(HTMLElementPrototype) {
/** Gets the matches function from an element. */
export function getMatchesFunction(htmlElementPrototype: HTMLElement): (selector: string) => boolean {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this function exist? It did not exist before, appears to be unused, and differs in behavior than getMatchesProperty (it incorrectly prefers vendor-prefixed versions, and it's incorrectly returning the webkit-prefixed version in the ms-prefixed branch).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also something I picked up from Rachel's CL...I didn't check it for usages. Good catch

*/
function getNormalizedEventCoords(ev, pageOffset, clientRect) {
export function getNormalizedEventCoords(
ev: Event | undefined, pageOffset: Point, clientRect: ClientRect): Point {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we allowing undefined here? It was not allowed to be undefined in the previous definition.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in foundation.ripple the activationEvent is defaulted to undefined. It then trickled to this function. Not sure what else to default the defaultActivationState.

ev = /** @type {!TouchEvent} */ (ev);
normalizedX = ev.changedTouches[0].pageX - documentX;
normalizedY = ev.changedTouches[0].pageY - documentY;
const e = ev as TouchEvent;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we give this variable a more descriptive name like touchEvent rather than an even more abbreviated name? Single-letter variables are notoriously harder to search usages of.

normalizedX = e.pageX - documentX;
normalizedY = e.pageY - documentY;
}
if (normalizedX === undefined || normalizedY === undefined) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this actually possible to happen?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like that was added from an original PR to test typescript. So I think it is a relic from an older version of TS complaining. Checked with Rachel and it is not needed. Removing.

get unbounded() {
return this.unbounded_;
disabled = false;
root_: Element = this.root_;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly dumb question: is the assignment here actually doing anything or is it just assigning this.root_ to itself?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is just assigning it to itself. Without that line, this is not of type RippleCapableSurface. This causes line 116 to complain:

  getDefaultFoundation(): MDCRippleFoundation {
    return new MDCRippleFoundation(MDCRipple.createAdapter(this));
  }

Should I add a comment?

moog16 pushed a commit that referenced this pull request Jan 30, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants