Skip to content

Commit

Permalink
chore(FlowType): Fixes all flow type issues through codebase, adds fl…
Browse files Browse the repository at this point in the history
…ow check to test suite, correctly exports .flow definition files for 3rd party comsumption #210 #159 #40
  • Loading branch information
jamesmfriedman committed May 1, 2018
1 parent 88abec3 commit 1158af9
Show file tree
Hide file tree
Showing 61 changed files with 292 additions and 227 deletions.
4 changes: 3 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
{
"ignoreComments": true,
"ignoreTrailingComments": true,
"ignoreUrls": true
"ignoreUrls": true,
"ignoreStrings": true,
"ignoreTemplateLiterals": true
}
],
"react/prop-types": "off",
Expand Down
3 changes: 2 additions & 1 deletion .flowconfig
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
[lints]

[options]
module.name_mapper='^rmwc' ->'<PROJECT_ROOT>'
module.name_mapper='^rmwc\/\(.*\)$' -> '<PROJECT_ROOT>/\1'
module.name_mapper='^rmwc$' -> '<PROJECT_ROOT>'

[strict]
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
/TextField
/Theme
/Toolbar
/TopAppBar
/Typography
react-versions

Expand Down
16 changes: 15 additions & 1 deletion .vscode/cSpell.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,24 @@
// words - list of words to be always considered correct
"words": [
"alertdialog",
"checkboxes",
"deregister",
"eqeqeq",
"fabs",
"lastrow",
"listbox",
"refname",
"reinit",
"rmwc",
"scroll",
"scroller",
"Snackbar",
"snackbars",
"submenu",
"Textfield",
"unelevated"
"truthy",
"unelevated",
"unmounting"
],
// flagWords - list of words to be always considered incorrect
// This is useful for offensive words and common spelling errors.
Expand Down
16 changes: 15 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,21 @@
"editor.tabSize": 2,
"cSpell.words": [
"alertdialog",
"checkboxes",
"deregister",
"eqeqeq",
"fabs",
"lastrow",
"listbox",
"refname",
"reinit",
"rmwc",
"unelevated"
"scroll",
"scroller",
"snackbars",
"submenu",
"truthy",
"unelevated",
"unmounting"
]
}
6 changes: 3 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ RMWC has a process which runs the components through Babel, and moves them into
To "build" the library for distribution

```
npm run babelfy
npm run build
```

To build the umd module
Expand All @@ -57,12 +57,12 @@ npm run build:lib
To build the docs

```
npm run build
npm run build:docs
```

#### Running Tests

Make sure you have the devDepenedencies installed and then just use `npm test`. This should launch the tests in watch mode and continuously look for changed files to run.
Make sure you have the devDependencies installed and then just use `npm test`. This should launch the tests in watch mode and continuously look for changed files to run.

```
npm test
Expand Down
33 changes: 31 additions & 2 deletions index.js.flow
Original file line number Diff line number Diff line change
@@ -1,3 +1,32 @@
// @flow
export { version } from '../package.json';

export * from './src/rmwc';
export * from './Button';
export * from './Card';
export * from './Checkbox';
export * from './Chip';
export * from './Drawer';
export * from './Dialog';
export * from './Elevation';
export * from './Fab';
export * from './FloatingLabel';
export * from './FormField';
export * from './GridList';
export * from './Grid';
export * from './Icon';
export * from './IconToggle';
export * from './LineRipple';
export * from './LinearProgress';
export * from './List';
export * from './Menu';
export * from './Radio';
export * from './Ripple';
export * from './Provider';
export * from './Select';
export * from './Slider';
export * from './Snackbar';
export * from './Switch';
export * from './Tabs';
export * from './TextField';
export * from './Theme';
export * from './Toolbar';
export * from './Typography';
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 9 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,22 @@
},
"scripts": {
"start": "react-app-rewired start",
"build": "npm run docgen && react-app-rewired build && rm -R docs && mv build docs && cp docs/index.html docs/404.html",
"build:docs": "npm run docgen && react-app-rewired build && rm -R docs && mv build docs && cp docs/index.html docs/404.html",
"build:code": "node scripts/build-code.js",
"build:lib": "node scripts/build-lib.js",
"build": "npm run build:code && npm run build:lib && npm run build:docs",
"pretest": "node scripts/pretest.js",
"test": "node scripts/test.js",
"test": "flow && node scripts/test.js",
"test:watch": "react-app-rewired test --env=jsdom",
"babelfy": "node scripts/babelfy.js",
"clean": "node scripts/clean.js",
"prepublishOnly": "npm run babelfy",
"prepublishOnly": "npm run build",
"postpublish": "npm run clean",
"docgen": "export NODE_ENV=development && react-docgen ./src --exclude=/\\.\\(story\\|docs\\|spec\\)\\.js/ --pretty --resolver ./scripts/docgen-resolver.js -o ./src/docs/docgen.json",
"build:lib": "node scripts/build-lib.js",
"deploy:docs": "npm run build && git add docs -f && git commit -m 'Deploying docs' && git push --force && npm run clean",
"deploy:docs": "npm run build:docs && git add docs -f && git commit -m 'Deploying docs' && git push --force && npm run clean",
"changelog": "changelog",
"storybook": "start-storybook -p 6006",
"preversion": "npm test -- --coverage",
"flow": "flow",
"version": "npm run changelog && git add CHANGELOG.md && npm run build:lib && git add -A lib && npm run build && git add docs -f",
"postversion": "git push && git push --tags"
},
Expand All @@ -70,7 +72,7 @@
"eslint-plugin-import": "^2.11.0",
"eslint-plugin-jsx-a11y": "^5.1.1",
"eslint-plugin-react": "^7.7.0",
"flow-bin": "^0.62.0",
"flow-bin": "^0.71.0",
"generate-changelog": "^1.7.0",
"normalize.css": "^7.0.0",
"prettier-eslint": "^8.8.1",
Expand Down
2 changes: 2 additions & 0 deletions scripts/babelfy.js → scripts/build-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ processBuiltFiles(files => {
}

const cmd = `NODE_ENV=production ./node_modules/.bin/babel ${f} -o ${out} --copy-files`;
const copyFlowCmd = `cp ${f} ${out}.flow`;
console.log('Babel:', f, '-> ', out);
exec(cmd);
exec(copyFlowCmd);
});

console.log('Writing version...');
Expand Down
2 changes: 1 addition & 1 deletion scripts/docgen-resolver.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable */
/*
* Copyright (c) 2015, Facebook, Inc.
* All rights reserved.
Expand All @@ -6,7 +7,6 @@
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @flow
*/

const isExportsOrModuleAssignment = require('react-docgen/dist/utils/isExportsOrModuleAssignment')
Expand Down
57 changes: 28 additions & 29 deletions src/Base/MDCFoundation.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,19 @@ const copyProperties = (target, source) => {

allPropertyNames.forEach(propertyName => {
if (
propertyName.match(
String(propertyName).match(
// eslint-disable-next-line max-len
/^(?:constructor|prototype|arguments|caller|name|bind|call|apply|toString|length)$/
)
) {
return;
}
Object.defineProperty(
target,
propertyName,
Object.getOwnPropertyDescriptor(source, propertyName)
);
const value = Object.getOwnPropertyDescriptor(source, propertyName);
value !== undefined && Object.defineProperty(target, propertyName, value);
});
};

/** Simplifies reduant checks for syncWithProps */
/** Simplifies redundant checks for syncWithProps */
export const syncFoundationProp = (
prop: mixed,
foundationValue: mixed,
Expand Down Expand Up @@ -76,24 +74,14 @@ export const removeClass = () =>
// The animation frame corrects an issue where MDC would set a class
// on a form element and cause re-render before the new value could actually be set from the onChange
this._safeSetState(prevState => ({
classes: prevState.classes.delete(className) ?
prevState.classes :
prevState.classes
classes: prevState.classes.delete(className)
? prevState.classes
: prevState.classes
}));
}
});
};

export const registerInteractionHandler = (refName: string = 'root_') =>
function(type: string, handler: Function) {
this[refName] && this[refName].addEventListener(type, handler);
};

export const deregisterInteractionHandler = (refName: string = 'root_') =>
function(type: string, handler: Function) {
this[refName] && this[refName].removeEventListener(type, handler);
};

/************************************************************************
* HOC
***********************************************************************/
Expand All @@ -114,21 +102,25 @@ export const withFoundation = ({
adapter = {},
refs = ['root_']
}: FoundationT): $Shape<constructor> => {
class Foundation<P, S> extends React.Component<P, S & FoundationStateT> {
class Foundation<P: Object, S: any> extends React.Component<
P,
S & FoundationStateT
> {
constructor(props: *) {
super(props);

this.foundationRefs = refs.reduce((acc, r) => {
// Here we gracefully merge two refs together if one was passed down the chain
const propName =
this.props.elementRef && this.props.elementRef.refName_ === r ?
'elementRef' :
r;
this.props.elementRef && this.props.elementRef.refName_ === r
? 'elementRef'
: r;

acc[r] = ref => {
// React will return a null ref when unmounting which will
// in turn make our adapters error out. Make sure we only set a ref if its truthy.
if (ref) {
//$FlowFixMe
this[r] = ref;
this.props[propName] && this.props[propName](ref);
}
Expand All @@ -140,6 +132,7 @@ export const withFoundation = ({
return acc;
}, {});

//$FlowFixMe
this.syncWithProps = this.syncWithProps.bind(this);
}

Expand Down Expand Up @@ -167,7 +160,7 @@ export const withFoundation = ({
classes: new Set()
};

foundation_: Object;
foundation_: ?Object;

foundationRefs: { [string]: (ref: window.DomElement) => mixed };

Expand All @@ -179,10 +172,12 @@ export const withFoundation = ({
this.foundation_ = this.getDefaultFoundation();

Object.entries(adapter).forEach(([handlerName, handler]) => {
//$FlowFixMe
this.foundation_.adapter_[handlerName] = handler.bind(this);
});

this.initialize();
this.foundation_.init();
this.foundation_ && this.foundation_.init();
this.initialSyncWithDOM();
this._safeSyncWithProps(this.props);

Expand All @@ -192,14 +187,15 @@ export const withFoundation = ({

destroyComponent() {
this.destroy();
this.foundation_.destroy();
this.foundation_ && this.foundation_.destroy();
this.foundation_ = undefined;

// We need to hold onto our refs until all child components are unmounted
// Here we just wait a few frames and set them to null so garbage collection will take over.
requestFrames(() => {
refs.forEach(refName => {
this[refName] = undefined;
//$FlowFixMe
this[refName] && (this[refName] = undefined);
});
}, 3);
}
Expand All @@ -208,6 +204,9 @@ export const withFoundation = ({
initialize() {}
initialSyncWithDOM() {}
destroy() {}
getDefaultFoundation() {
return {};
}

/**
* Fires a cross-browser-compatible custom event from the component root of the given type,
Expand All @@ -234,7 +233,7 @@ export const withFoundation = ({

this.props[propName] && this.props[propName](evt);

// MDC can change state internally, if we are triggering a handler, resync with our props
// MDC can change state internally, if we are triggering a handler, re-sync with our props
this._safeSyncWithProps(this.props);

return evt;
Expand Down
Loading

0 comments on commit 1158af9

Please sign in to comment.