From 232c1d8109e02422de0742ae8ba7d8261a0436d5 Mon Sep 17 00:00:00 2001 From: Igor Gassmann Date: Wed, 25 Apr 2018 17:20:59 -0400 Subject: [PATCH] fix: re-enable flow type checking for the project (#1197) Flow wasn't working anymore. This fixes flow type checking and it to the git precommit hook. --- .gitignore | 1 - .lintstagedrc.json | 1 + CHANGELOG.md | 5 +- CONTRIBUTING.md | 39 +++++++++--- README.md | 6 +- flowtype-plugin.js | 116 ---------------------------------- package.json | 12 ++-- webpack.renderer.additions.js | 10 --- yarn.lock | 23 +------ 9 files changed, 45 insertions(+), 168 deletions(-) delete mode 100644 flowtype-plugin.js diff --git a/.gitignore b/.gitignore index 2bacc7198..f57e421ea 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,3 @@ /static/daemon/lbrynet* /static/locales yarn-error.log -/build/daemon* diff --git a/.lintstagedrc.json b/.lintstagedrc.json index bd9f228ce..24ad6a51f 100644 --- a/.lintstagedrc.json +++ b/.lintstagedrc.json @@ -6,6 +6,7 @@ ], "src/**/*.{js,jsx}": [ "eslint --fix", + "flow focus-check --color always", "git add" ] } diff --git a/CHANGELOG.md b/CHANGELOG.md index 779f3cb5e..9dbd13739 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,13 +15,12 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/). * Add Danger JS to automate code reviews ([#1289](https://github.com/lbryio/lbry-app/pull/1289)) ### Changed -* Add flair to snackbar ([#1313](https://github.com/lbryio/lbry-app/pull/1313)) + * Add flair to snackbar ([#1313](https://github.com/lbryio/lbry-app/pull/1313)) ### Fixed * Black screen on macOS after maximizing LBRY and then closing ([#1235](https://github.com/lbryio/lbry-app/pull/1235)) * Fix content-type not shown correctly in file description ([#863](https://github.com/lbryio/lbry-app/pull/863)) - -### Fixed + * Fix [Flow](https://flow.org/) ([#1197](https://github.com/lbryio/lbry-app/pull/1197)) * Black screen on macOS after maximizing LBRY and then closing ([#1235](https://github.com/lbryio/lbry-app/pull/1235)) * Fix dark theme ([#1034](https://github.com/lbryio/lbry-app/issues/1034)) * Fix download percentage indicator overlay ([#1271](https://github.com/lbryio/lbry-app/issues/1271)) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index c1a3914e7..b69d38e70 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -36,8 +36,8 @@ To make contributing as easy and rewarding of possible, we have instituted the f [Help Wanted](https://github.com/lbryio/lbry-app/issues?q=is%3Aopen+is%3Aissue+label%3A%22help+wanted%22+no%3Aassignee) issue is ranked on a scale from zero to four. -| Level | Description | -| --------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------- | +| Level | Description | +| ------------------------------------------------------------------------------------------------------------------------------------------------ | -------------------------------------------------------------------------------------------------- | | [**level 0**](https://github.com/lbryio/lbry-app/issues?q=is%3Aopen+is%3Aissue+label%3A%22help+wanted%22+label%3A%22level%3A+0%22+no%3Aassignee) | Typos and text edits -- a tech-savvy non-programmer can fix these | | [**level 1**](https://github.com/lbryio/lbry-app/issues?q=is%3Aopen+is%3Aissue+label%3A%22help+wanted%22+label%3A%22level%3A+1%22+no%3Aassignee) | Programming issues that require little knowledge of how the LBRY app works | | [**level 2**](https://github.com/lbryio/lbry-app/issues?q=is%3Aopen+is%3Aissue+label%3A%22help+wanted%22+label%3A%22level%3A+2%22+no%3Aassignee) | Issues of average difficulty that require the developer to dig into how the app works a little bit | @@ -60,24 +60,45 @@ better code quality. It's recommended to make use of them thoroughly during ongo We follow the well-known [Airbnb JavaScript Style Guide](http://airbnb.io/javascript/) for defining our styling rules and code best practices. +### Flow + +[Flow](https://flow.org/) is a static type checker for JavaScript. Flow checks your code for +errors through static type annotations. For using Flow, you need to add the following +annotation to the beginning of the file you're editing: + +`// @flow` + +After adding this, you can start adding [type annotations](https://flow.org/en/docs/types/) to +the code. + +If you add a project dependency and you want to use it with Flow, you need to import its type +definitions in the project by running: + +`$ yarn flow-defs` + ### Run LBRY app can be run for development by using the command: -`yarn dev` +`$ yarn dev` This will launch the app and provide HMR (Hot Module Replacement). Any change made to the sources will automatically reload the app without losing its state. ### Lint -Code linting is ensured by [ESLint](https://eslint.org/). +Code linting is ensured by [ESLint](https://eslint.org/) and [Flow CLI](https://flow.org/en/docs/cli/). You can lint all the project's sources at any time by running: -`yarn lint` +`$ yarn lint` -If you desire to lint a specific file or directory you can use `yarn eslint 'glob/pattern'`. +If you desire to lint a specific file or directory you can use: + +``` +$ yarn eslint 'glob/pattern' +$ yarn flow focus-check 'glob/pattern' +``` In addition to those commands, staged files are automatically linted before commit. Please take the time to fix all staged files' linting problems before committing or suppress them if necessary. @@ -93,10 +114,12 @@ Staged files are automatically formatted before commit. You can also use the following command: -`yarn format` +`$ yarn format` for applying formatting rules to all project's code sources. For formatting a specific file or -directory use `yarn prettier 'glob/pattern'`. +directory use: + +`$ yarn prettier 'glob/pattern'` Editor integrations are available [here](https://prettier.io/docs/en/editors.html). diff --git a/README.md b/README.md index ef1db7035..7d5331c88 100644 --- a/README.md +++ b/README.md @@ -38,12 +38,12 @@ development and testing purposes. * [Yarn](https://yarnpkg.com/en/docs/install) * [C++ Build Tools](https://github.com/felixrieseberg/windows-build-tools) (Only needed on Windows) -### One-time Setup +### Setup 1. Clone this repository 2. Run `$ yarn` -### Running +### Run The app can be run from the sources using the following command: @@ -51,7 +51,7 @@ The app can be run from the sources using the following command: ### Build -Run `$ yarn build`. +Run `$ yarn build` We use [electron-builder](https://github.com/electron-userland/electron-builder) to create distributable packages. diff --git a/flowtype-plugin.js b/flowtype-plugin.js deleted file mode 100644 index f24c7b52c..000000000 --- a/flowtype-plugin.js +++ /dev/null @@ -1,116 +0,0 @@ -var spawnSync = require('child_process').spawnSync; -var flow = require('flow-bin'); -var merge = require('lodash.merge'); - -var store = { - error: null, - flowOptions: [ - 'check', - '--color=always', - ], - options: { - warn: false, - - formatter: function (errorCode, errorDetails) { - return 'Flow: ' + errorCode + '\n\n' + errorDetails; - }, - }, -}; - - -function flowErrorCode(status) { - var error; - switch (status) { - /* - case 0: - error = null; - break; - */ - case 1: - error = 'Server Initializing'; - break; - case 2: - error = 'Type Error'; - break; - case 3: - error = 'Out of Time'; - break; - case 4: - error = 'Kill Error'; - break; - case 6: - error = 'No Server Running'; - break; - case 7: - error = 'Out of Retries'; - break; - case 8: - error = 'Invalid Flowconfig'; - break; - case 9: - error = 'Build Id Mismatch'; - break; - case 10: - error = 'Input Error'; - break; - case 11: - error = 'Lock Stolen'; - break; - case 12: - error = 'Could Not Find Flowconfig'; - break; - case 13: - error = 'Server Out of Date'; - break; - case 14: - error = 'Server Client Directory Mismatch'; - break; - case 15: - error = 'Out of Shared Memory'; - break; - } - - return error; -} - - -function checkFlowStatus(compiler, next) { - var res = spawnSync(flow, store.flowOptions); - var status = res.status; - - if (status !== 0) { - var errorCode = flowErrorCode(status); - var errorDetails = res.stdout.toString() + res.stderr.toString(); - - store.error = new Error(store.options.formatter(errorCode, errorDetails)); - } - - next(); -} - - -function pushError(compilation) { - if (store.error) { - if (store.options.warn) { - compilation.warnings.push(store.error); - } else { - compilation.errors.push(store.error); - } - - store.error = null; - } -} - - -function FlowFlowPlugin(options) { - store.options = merge(store.options, options); -} - -FlowFlowPlugin.prototype.apply = function(compiler) { - compiler.plugin('run', checkFlowStatus); - compiler.plugin('watch-run', checkFlowStatus); - - compiler.plugin('compilation', pushError); -}; - -module.exports = FlowFlowPlugin; diff --git a/package.json b/package.json index 53adee5b9..2fa7b149c 100644 --- a/package.json +++ b/package.json @@ -21,18 +21,19 @@ "main": "src/main/index.js", "scripts": { "extract-langs": "node build/extractLocals.js", - "dev": "electron-webpack dev", - "compile": "electron-webpack && yarn extract-langs", + "compile": "electron-webpack & yarn extract-langs", "build": "yarn compile && electron-builder build", "build:dir": "yarn build -- --dir -c.compression=store -c.mac.identity=null", + "dev": "electron-webpack dev", + "lint": "eslint 'src/**/*.{js,jsx}' --fix && flow", + "format": "prettier 'src/**/*.{js,jsx,scss,json}' --write", + "flow-defs": "flow-typed install", "release": "yarn compile && electron-builder build", "precommit": "lint-staged", "postmerge": "yarnhook", "postcheckout": "yarnhook", "postrewrite": "yarnhook", - "postinstall": "electron-builder install-app-deps && node build/downloadDaemon.js", - "lint": "eslint 'src/**/*.{js,jsx}' --fix", - "format": "prettier 'src/**/*.{js,jsx,scss,json}' --write", + "postinstall": "electron-builder install-app-deps & node build/downloadDaemon.js", "clean": "rm -r node_modules && yarn cache clean lbry-redux && yarn" }, "dependencies": { @@ -105,7 +106,6 @@ "eslint-plugin-jsx-a11y": "^6.0.3", "eslint-plugin-prettier": "^2.6.0", "eslint-plugin-react": "^7.7.0", - "flow-babel-webpack-plugin": "^1.1.1", "flow-bin": "^0.69.0", "flow-typed": "^2.3.0", "fs-path": "^0.0.24", diff --git a/webpack.renderer.additions.js b/webpack.renderer.additions.js index 476a8b242..b0887e7a0 100644 --- a/webpack.renderer.additions.js +++ b/webpack.renderer.additions.js @@ -1,6 +1,4 @@ const path = require('path'); -const FlowFlowPlugin = require('./flowtype-plugin'); -const isDev = require('electron-is-dev'); const ELECTRON_RENDERER_PROCESS_ROOT = path.resolve(__dirname, 'src/renderer/'); @@ -23,11 +21,3 @@ module.exports = { extensions: ['.js', '.jsx', '.scss'], }, }; - -if (isDev) { - module.exports.plugins = [ - new FlowFlowPlugin({ - warn: true, - }), - ]; -} diff --git a/yarn.lock b/yarn.lock index 0ce93c297..b670e737e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -805,7 +805,7 @@ babel-plugin-syntax-exponentiation-operator@^6.8.0: version "6.13.0" resolved "https://registry.yarnpkg.com/babel-plugin-syntax-exponentiation-operator/-/babel-plugin-syntax-exponentiation-operator-6.13.0.tgz#9ee7e8337290da95288201a6a57f4170317830de" -babel-plugin-syntax-flow@^6.18.0, babel-plugin-syntax-flow@^6.8.0: +babel-plugin-syntax-flow@^6.18.0: version "6.18.0" resolved "https://registry.yarnpkg.com/babel-plugin-syntax-flow/-/babel-plugin-syntax-flow-6.18.0.tgz#4c3ab20a2af26aa20cd25995c398c4eb70310c8d" @@ -1032,13 +1032,6 @@ babel-plugin-transform-exponentiation-operator@^6.22.0, babel-plugin-transform-e babel-plugin-syntax-exponentiation-operator "^6.8.0" babel-runtime "^6.22.0" -babel-plugin-transform-flow-comments@^6.17.0: - version "6.22.0" - resolved "https://registry.yarnpkg.com/babel-plugin-transform-flow-comments/-/babel-plugin-transform-flow-comments-6.22.0.tgz#8d9491132f2b48abd0656f96c20f3bbd6fc17529" - dependencies: - babel-plugin-syntax-flow "^6.8.0" - babel-runtime "^6.22.0" - babel-plugin-transform-flow-strip-types@^6.22.0: version "6.22.0" resolved "https://registry.yarnpkg.com/babel-plugin-transform-flow-strip-types/-/babel-plugin-transform-flow-strip-types-6.22.0.tgz#84cb672935d43714fdc32bce84568d87441cf7cf" @@ -4024,18 +4017,6 @@ flatten@^1.0.2: version "1.0.2" resolved "https://registry.yarnpkg.com/flatten/-/flatten-1.0.2.tgz#dae46a9d78fbe25292258cc1e780a41d95c03782" -flow-babel-webpack-plugin@^1.1.1: - version "1.1.1" - resolved "https://registry.yarnpkg.com/flow-babel-webpack-plugin/-/flow-babel-webpack-plugin-1.1.1.tgz#7dd41110b96045eab8d8af6df48883310de54e91" - dependencies: - babel-plugin-transform-flow-comments "^6.17.0" - flow-bin ">=0.44.2 <1" - lodash.merge "^4.6.0" - -"flow-bin@>=0.44.2 <1": - version "0.70.0" - resolved "https://registry.yarnpkg.com/flow-bin/-/flow-bin-0.70.0.tgz#080ae83a997f2b4ddb3dc2649bf13336825292b5" - flow-bin@^0.69.0: version "0.69.0" resolved "https://registry.yarnpkg.com/flow-bin/-/flow-bin-0.69.0.tgz#053159a684a6051fcbf0b71a2eb19a9679082da6" @@ -6000,7 +5981,7 @@ lodash.memoize@^4.1.2: version "4.1.2" resolved "https://registry.yarnpkg.com/lodash.memoize/-/lodash.memoize-4.1.2.tgz#bcc6c49a42a2840ed997f323eada5ecd182e0bfe" -lodash.merge@^4.4.0, lodash.merge@^4.6.0: +lodash.merge@^4.4.0: version "4.6.1" resolved "https://registry.yarnpkg.com/lodash.merge/-/lodash.merge-4.6.1.tgz#adc25d9cb99b9391c59624f379fbba60d7111d54"