From 5ef665f2616a093915356f310ceb5abfb30f4ff7 Mon Sep 17 00:00:00 2001 From: Simon Mollweide Date: Tue, 4 Apr 2017 17:34:22 +0200 Subject: [PATCH] [add] flow config and rules --- .flowconfig | 9 +++ README.md | 6 +- configurations/es5.js | 12 ++-- configurations/es6-react.js | 4 +- configurations/es6.js | 14 ++--- configurations/flow.js | 16 ++++++ package.json | 7 ++- processing/write-rule-test-files.js | 18 +++++- rules/flow.js | 88 +++++++++++++++++++++++++++++ 9 files changed, 153 insertions(+), 21 deletions(-) create mode 100644 .flowconfig create mode 100644 configurations/flow.js create mode 100644 rules/flow.js diff --git a/.flowconfig b/.flowconfig new file mode 100644 index 0000000..ffaf12d --- /dev/null +++ b/.flowconfig @@ -0,0 +1,9 @@ +[ignore] + +[include] + +[libs] + +[options] +unsafe.enable_getters_and_setters=true +esproposal.decorators=ignore diff --git a/README.md b/README.md index aa79e12..d80aec3 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,6 @@ $ npm install --save-dev eslint eslint-plugin-import @namics/eslint-config ``` - ## Usage - `@namics/eslint-config/configurations/es6-browser` - ES6 + browser - `@namics/eslint-config/configurations/es6-react` - ES6 + react @@ -28,12 +27,13 @@ module.exports = { ``` ### with flow +Flowtype rules will be accepted if the flow annotation was defined in first line of each file. ``` module.exports = { "extends": [ "@namics/eslint-config/configurations/flow.js" "@namics/eslint-config/configurations/es6-react.js" - ] + ], } ``` @@ -91,4 +91,4 @@ then run `npm run lint` ## Changelog -Please see the [Releases](https://github.com/namics/eslint-config-namics/releases) \ No newline at end of file +Please see the [Releases](https://github.com/namics/eslint-config-namics/releases) diff --git a/configurations/es5.js b/configurations/es5.js index 10d5686..97cd31c 100644 --- a/configurations/es5.js +++ b/configurations/es5.js @@ -1,12 +1,12 @@ module.exports = { extends: [ - '../rules/es6-disable', - '../rules/node-disable', - '../rules/best-practices', - '../rules/errors', - '../rules/style', - '../rules/variables', + '../rules/es6-disable.js', + '../rules/node-disable.js', + '../rules/best-practices.js', + '../rules/errors.js', + '../rules/style.js', + '../rules/variables.js', ].map(require.resolve), env: {}, parserOptions: { diff --git a/configurations/es6-react.js b/configurations/es6-react.js index a1426ab..d495d88 100644 --- a/configurations/es6-react.js +++ b/configurations/es6-react.js @@ -5,8 +5,8 @@ module.exports = { ], 'extends': [ './es6.js', - '../rules/react', - '../rules/react-a11y', + '../rules/react.js', + '../rules/react-a11y.js', ].map(require.resolve), 'env': { 'browser': true, diff --git a/configurations/es6.js b/configurations/es6.js index 5f95f32..fa46138 100644 --- a/configurations/es6.js +++ b/configurations/es6.js @@ -15,13 +15,13 @@ module.exports = { }, }, extends: [ - '../rules/node-disable', - '../rules/best-practices', - '../rules/errors', - '../rules/style', - '../rules/variables', - '../rules/imports', - '../rules/es6', + '../rules/node-disable.js', + '../rules/best-practices.js', + '../rules/errors.js', + '../rules/style.js', + '../rules/variables.js', + '../rules/imports.js', + '../rules/es6.js', ].map(require.resolve), parserOptions: { diff --git a/configurations/flow.js b/configurations/flow.js new file mode 100644 index 0000000..62c7080 --- /dev/null +++ b/configurations/flow.js @@ -0,0 +1,16 @@ + +module.exports = { + + 'parser': 'babel-eslint', + 'plugins': [ + 'flowtype', + ], + 'extends': [ + '../rules/flow.js', + ], + 'settings': { + 'flowtype': { + 'onlyFilesWithFlowAnnotation': true, + }, + }, +}; diff --git a/package.json b/package.json index 45f6fa1..b56e36c 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "main": "configurations/es6-browser.js", "scripts": { "lint": "npm run lint:js", - "lint:js": "node_modules/.bin/eslint test/**/*.jsx test/**/*.js", + "lint:js": "node_modules/.bin/eslint .", "test": "npm run lint" }, "keywords": [ @@ -39,8 +39,11 @@ "eslint-plugin-import": "^2.0.0" }, "dependencies": { + "babel-eslint": "^7.2.1", "eslint-find-rules": "^1.14.3", + "eslint-plugin-flowtype": "^2.30.4", "eslint-plugin-jsx-a11y": "^4.0.0", - "eslint-plugin-react": "^6.10.0" + "eslint-plugin-react": "^6.10.0", + "flow-bin": "^0.40.0" } } diff --git a/processing/write-rule-test-files.js b/processing/write-rule-test-files.js index 7de0066..1a5ff0e 100644 --- a/processing/write-rule-test-files.js +++ b/processing/write-rule-test-files.js @@ -1,5 +1,12 @@ /* eslint "no-console": 0 */ +/* + + USAGE: + node processing/write-rule-test-files.js rules/flow.js test/flow/rules/ + +*/ + var utils = require('./utils'); var argv = process.argv; var filePath; @@ -20,8 +27,10 @@ function writeTestFiles() { destPath = argv[3]; data = utils.readFile(filePath); + data.split('\n\n').map(function (rule) { + var ruleSpl = rule.split('\n'); var desc; var destFileName; @@ -40,10 +49,17 @@ function writeTestFiles() { .replace(/\[([^, {]*).*/g, '$1') .replace(/'/g, ''); + if (nameStatus.search(/^http/) >= 0) { + nameStatus = ruleSpl[2] + .replace('//', '') + .trim() + .replace(/\[([^, {]*).*/g, '$1') + .replace(/'/g, ''); + } + nameStatusSpl = nameStatus.split(':'); destFileName = destPath + nameStatusSpl[0].replace(/^.*\//, '') + '.js'; - if (utils.existFile(destFileName)) { return true; } diff --git a/rules/flow.js b/rules/flow.js new file mode 100644 index 0000000..9a6d62b --- /dev/null +++ b/rules/flow.js @@ -0,0 +1,88 @@ +/* eslint-disable */ +/* global module */ +/* not yet approved by skill group core team */ +module.exports = { + rules: { + + // Overwrite require-jsdoc rule + 'require-jsdoc': 0, + + // enforces a particular style for boolean type annotations. This rule takes one argument. + // https://github.com/gajus/eslint-plugin-flowtype#eslint-plugin-flowtype-rules-boolean-style + 'flowtype/boolean-style': [2, 'boolean'], + + // marks Flow type identifiers as defined. Used to suppress [`no-undef`](https://github.com/gajus/eslint-plugin-flowtypedocs/rules/no-undef) reporting of type identifiers. + // https://github.com/gajus/eslint-plugin-flowtype#eslint-plugin-flowtype-rules-define-flow-type + 'flowtype/define-flow-type': 2, + + // enforces consistent use of trailing commas in Object and Tuple annotations. + // https://github.com/gajus/eslint-plugin-flowtype#eslint-plugin-flowtype-rules-delimiter-dangle + 'flowtype/delimiter-dangle': [2, 'always-multiline'], + // [20.09.2016] always-multiline -> https://github.com/namics/eslint-config-namics/issues/1 + + // enforces consistent spacing within generic type annotation parameters. + // https://github.com/gajus/eslint-plugin-flowtype#eslint-plugin-flowtype-rules-generic-spacing + 'flowtype/generic-spacing': [2, 'never'], + + // disallows use of primitive constructors as types, such as `Boolean`, `Number` and `String`. [See more](https://flowtype.org/docs/builtins.html). + // https://github.com/gajus/eslint-plugin-flowtype#eslint-plugin-flowtype-rules-no-primitive-constructor-types + 'flowtype/no-primitive-constructor-types': 2, + + // Warns against weak type annotations *any*, *Object* and *Function*. These types can cause flow to silently skip over portions of your code, which would have otherwise caused type errors. + // https://github.com/gajus/eslint-plugin-flowtype#eslint-plugin-flowtype-rules-no-weak-types + 'flowtype/no-weak-types': 0, + // [10.03.2013] disabled because of third party libraries + + // enforces consistent separators between properties in Flow object types. + // https://github.com/gajus/eslint-plugin-flowtype#eslint-plugin-flowtype-rules-object-type-delimiter + 'flowtype/object-type-delimiter': [2, 'comma'], + + // requires that all function parameters have type annotations. + // https://github.com/gajus/eslint-plugin-flowtype#eslint-plugin-flowtype-rules-require-parameter-type + 'flowtype/require-parameter-type': 2, + + // requires that functions have return type annotation. + // https://github.com/gajus/eslint-plugin-flowtype#eslint-plugin-flowtype-rules-require-return-type + 'flowtype/require-return-type': [2, 'always', + { + 'annotateUndefined': 'never', + }, + ], + + // this rule validates Flow file annotations. + // https://github.com/gajus/eslint-plugin-flowtype#eslint-plugin-flowtype-rules-require-valid-file-annotation + 'flowtype/require-valid-file-annotation': 2, + + // enforces consistent use of semicolons after type aliases. + // https://github.com/gajus/eslint-plugin-flowtype#eslint-plugin-flowtype-rules-semi + 'flowtype/semi': [2, 'always'], + + // enforces consistent spacing after the type annotation colon. + // https://github.com/gajus/eslint-plugin-flowtype#eslint-plugin-flowtype-rules-space-after-type-colon + 'flowtype/space-after-type-colon': [2, 'always'], + + // Enforces consistent spacing before the opening < of generic type annotation parameters. + // https://github.com/gajus/eslint-plugin-flowtype#eslint-plugin-flowtype-rules-space-before-generic-bracket + 'flowtype/space-before-generic-bracket': [2, 'never'], + + // Enforces consistent spacing before the type annotation colon. + // https://github.com/gajus/eslint-plugin-flowtype#eslint-plugin-flowtype-rules-space-before-type-colon + 'flowtype/space-before-type-colon': [2, 'never'], + + // Enforces a consistent naming pattern for type aliases. + // https://github.com/gajus/eslint-plugin-flowtype#eslint-plugin-flowtype-rules-type-id-match + 'flowtype/type-id-match': [2, '^([A-Z][a-z0-9]+)+Type$'], + + // Enforces consistent spacing around union and intersection type separators (| and &). + // https://github.com/gajus/eslint-plugin-flowtype#eslint-plugin-flowtype-rules-union-intersection-spacing + 'flowtype/union-intersection-spacing': [2, 'always'], + + // Marks Flow type alias declarations as used. Used to suppress no-unused-vars errors that are triggered by type aliases. + // https://github.com/gajus/eslint-plugin-flowtype#eslint-plugin-flowtype-rules-use-flow-type + 'flowtype/use-flow-type': 2, + + // Checks for simple Flow syntax errors. + // https://github.com/gajus/eslint-plugin-flowtype#eslint-plugin-flowtype-rules-valid-syntax + 'flowtype/valid-syntax': 2, + }, +};