diff --git a/node_package/.babelrc b/node_package/.babelrc index 87aaf4be6..289fc305b 100644 --- a/node_package/.babelrc +++ b/node_package/.babelrc @@ -1,4 +1,7 @@ { "presets": ["es2015", "stage-0", "react"], - "plugins": ["transform-runtime"] + "plugins": [ + "transform-flow-strip-types", + "transform-runtime" + ] } diff --git a/node_package/.flowconfig b/node_package/.flowconfig new file mode 100644 index 000000000..ef6f4c16d --- /dev/null +++ b/node_package/.flowconfig @@ -0,0 +1,3 @@ +[ignore] +.*/lib/.* +.*/node_modules/.* diff --git a/node_package/src/Authenticity.js b/node_package/src/Authenticity.js index bc6e30930..6106c863b 100644 --- a/node_package/src/Authenticity.js +++ b/node_package/src/Authenticity.js @@ -1,11 +1,13 @@ +// @flow + export default { authenticityToken() { - const token = document.querySelector('meta[name="csrf-token"]'); + const token: {content?: string} = document.querySelector('meta[name="csrf-token"]'); return token ? token.content : null; }, - authenticityHeaders(otherHeaders = {}) { + authenticityHeaders(otherHeaders: {[id:string]: string} = {}) { return Object.assign(otherHeaders, { 'X-CSRF-Token': this.authenticityToken(), 'X-Requested-With': 'XMLHttpRequest', diff --git a/node_package/src/buildConsoleReplay.js b/node_package/src/buildConsoleReplay.js index 0aaad331b..056a58a0a 100644 --- a/node_package/src/buildConsoleReplay.js +++ b/node_package/src/buildConsoleReplay.js @@ -1,14 +1,15 @@ +// @flow + import RenderUtils from './RenderUtils'; import scriptSanitizedVal from './scriptSanitizedVal'; export function consoleReplay() { // console.history is a global polyfill used in server rendering. - const history = console.history; - if (!history || history.length === 0) { + if (!(console.history instanceof Array)) { return ''; } - const lines = history.map(msg => { + const lines = console.history.map(msg => { const stringifiedList = msg.arguments.map(arg => { let val; try { diff --git a/node_package/src/context.js b/node_package/src/context.js index 45e9603f1..1bc612e6b 100644 --- a/node_package/src/context.js +++ b/node_package/src/context.js @@ -1,3 +1,5 @@ +// @flow + /** * Get the context, be it window or global * @returns {boolean|Window|*|context} diff --git a/node_package/src/generatorFunction.js b/node_package/src/generatorFunction.js index 364742b56..11c134d23 100644 --- a/node_package/src/generatorFunction.js +++ b/node_package/src/generatorFunction.js @@ -1,3 +1,5 @@ +// @flow + // See discussion: // https://discuss.reactjs.org/t/how-to-determine-if-js-object-is-react-component/2825/2 @@ -7,12 +9,11 @@ * @param component * @returns {boolean} */ -export default function generatorFunction(component) { +export default function generatorFunction(component: any) { if (!component.prototype) { return false; } // es5 or es6 React Component - const es5OrEs6ReactComponent = component.prototype.isReactComponent; - return !es5OrEs6ReactComponent; + return !component.prototype.isReactComponent; } diff --git a/package.json b/package.json index 3f798e6b3..53bf16bf7 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "babel-core": "^6.7.4", "babel-loader": "^6.2.4", "babel-plugin-react-transform": "^2.0.2", + "babel-plugin-transform-flow-strip-types": "^6.8.0", "babel-plugin-transform-runtime": "^6.6.0", "babel-preset-es2015": "^6.6.0", "babel-preset-react": "^6.5.0", @@ -25,6 +26,7 @@ "eslint-plugin-import": "^1.13.0", "eslint-plugin-jsx-a11y": "^2.1.0", "eslint-plugin-react": "^6.1.2", + "flow-bin": "^0.30.0", "jscs": "^2.11.0", "jsdom": "^8.2.0", "react": "^15.0.0", @@ -52,7 +54,8 @@ "build-watch": "babel --watch --out-dir node_package/lib node_package/src", "eslint": "eslint .", "jscs": "jscs -e -v .", - "lint": "npm run eslint && npm run jscs", + "flow": "flow check node_package", + "lint": "npm run eslint && npm run jscs && npm run flow", "lint:fix": "node_package/scripts/lint-fix", "check": "npm run lint && npm run test", "prerelease": "npm run check && npm run clean && npm run build", diff --git a/rakelib/lint.rake b/rakelib/lint.rake index 48d7059be..e7180c16b 100644 --- a/rakelib/lint.rake +++ b/rakelib/lint.rake @@ -28,8 +28,13 @@ namespace :lint do sh_in_dir(gem_root, "npm run jscs") end - desc "Run all eslint, jscs, rubocop linters. Skip ruby-lint and scss" - task lint: [:eslint, :jscs, :rubocop] do + desc "Run flow from shell" + task :flow do + sh_in_dir(gem_root, "npm run flow") + end + + desc "Run all eslint, jscs, flow, rubocop linters. Skip ruby-lint and scss" + task lint: [:eslint, :jscs, :flow, :rubocop] do puts "Completed all linting" end end