From ae0f95ff246f851dc6bf65774aff2ef0fd0da829 Mon Sep 17 00:00:00 2001 From: fisker Date: Tue, 29 Oct 2019 10:42:47 +0800 Subject: [PATCH 01/14] Improve lint script --- test/lint/lint.js | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/test/lint/lint.js b/test/lint/lint.js index 6ac13aa258..90d6967b15 100755 --- a/test/lint/lint.js +++ b/test/lint/lint.js @@ -4,15 +4,12 @@ const {CLIEngine} = require('eslint'); const unicorn = require('../..'); const {configs: {recommended}} = unicorn; -const {rules} = recommended; const files = [process.argv[2] || '.']; const fix = process.argv.includes('--fix'); const cli = new CLIEngine({ - ...recommended, + baseConfig: recommended, rules: { - ...rules, - // TODO: remove this override, when #391 is fixed 'unicorn/consistent-function-scoping': 'off' }, @@ -24,12 +21,30 @@ cli.addPlugin('eslint-plugin-unicorn', unicorn); const report = cli.executeOnFiles(files); -if (fix) { +const {errorCount, warningCount, fixableErrorCount, fixableWarningCount} = report + +const hasFixable = fixableErrorCount || fixableWarningCount + +if (fix && hasFixable) { CLIEngine.outputFixes(report); } -const formatter = cli.getFormatter(); +if (errorCount || warningCount) { + const formatter = cli.getFormatter(); + console.log(formatter(report.results)); -console.log(formatter(report.results)); + console.log(); + console.log('Some test fails, you need fix them, and run `npm run lint ` again.'); + + if (hasFixable) { + console.log(); + console.log('You may also want run `npm run lint --fix` to fix fixable problems.'); + } + + console.log(); + console.log('* If you\'re making a new rule, you can fix this later. *'); +} else { + console.log('All test passed.') +} -process.exit(report.errorCount); +process.exit(errorCount); From 9650757ceb2f770daa2af41a8d2954b034d6746c Mon Sep 17 00:00:00 2001 From: fisker Date: Tue, 29 Oct 2019 10:43:56 +0800 Subject: [PATCH 02/14] Fix code style --- test/lint/lint.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/lint/lint.js b/test/lint/lint.js index 90d6967b15..0898cb7c8e 100755 --- a/test/lint/lint.js +++ b/test/lint/lint.js @@ -21,9 +21,9 @@ cli.addPlugin('eslint-plugin-unicorn', unicorn); const report = cli.executeOnFiles(files); -const {errorCount, warningCount, fixableErrorCount, fixableWarningCount} = report +const {errorCount, warningCount, fixableErrorCount, fixableWarningCount} = report; -const hasFixable = fixableErrorCount || fixableWarningCount +const hasFixable = fixableErrorCount || fixableWarningCount; if (fix && hasFixable) { CLIEngine.outputFixes(report); @@ -44,7 +44,7 @@ if (errorCount || warningCount) { console.log(); console.log('* If you\'re making a new rule, you can fix this later. *'); } else { - console.log('All test passed.') + console.log('All test passed.'); } process.exit(errorCount); From b9f2d5a11245c247ebefa37409e6ffbc66bbb264 Mon Sep 17 00:00:00 2001 From: fisker Date: Tue, 29 Oct 2019 10:50:05 +0800 Subject: [PATCH 03/14] style --- test/lint/lint.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/lint/lint.js b/test/lint/lint.js index 0898cb7c8e..ea94e690ea 100755 --- a/test/lint/lint.js +++ b/test/lint/lint.js @@ -3,7 +3,7 @@ const {CLIEngine} = require('eslint'); const unicorn = require('../..'); -const {configs: {recommended}} = unicorn; +const {recommended} = unicorn.configs; const files = [process.argv[2] || '.']; const fix = process.argv.includes('--fix'); From a75d068b2d4f0abe88bcaa12b1bbc6d93df3b08e Mon Sep 17 00:00:00 2001 From: fisker Date: Tue, 29 Oct 2019 11:00:13 +0800 Subject: [PATCH 04/14] should fail on `prefer-string-slice` and `prevent-abbreviations` rules --- rules/catch-error-name.js | 4 +++- rules/no-console-spaces.js | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/rules/catch-error-name.js b/rules/catch-error-name.js index 602b615129..9da3a5cde9 100644 --- a/rules/catch-error-name.js +++ b/rules/catch-error-name.js @@ -13,7 +13,9 @@ function isLintablePromiseCatch(node) { const {property} = callee; - if (property.type !== 'Identifier' || property.name !== 'catch') { + const prop = property + + if (prop.type !== 'Identifier' || prop.name !== 'catch') { return false; } diff --git a/rules/no-console-spaces.js b/rules/no-console-spaces.js index 24f71fc908..e3feef1748 100644 --- a/rules/no-console-spaces.js +++ b/rules/no-console-spaces.js @@ -34,7 +34,7 @@ const getArgumentValue = (context, nodeArgument) => { const sourceCode = context.getSourceCode(); value = sourceCode.getText(nodeArgument); // Strip off backticks - value = value.slice(1, -1); + value = value.substring(1, value.length - 1); } return value; From 0082902850a1d2dd9fce6e535f9b297c50989399 Mon Sep 17 00:00:00 2001 From: fisker Date: Tue, 29 Oct 2019 11:05:01 +0800 Subject: [PATCH 05/14] Revert "should fail on `prefer-string-slice` and `prevent-abbreviations` rules" This reverts commit a75d068b2d4f0abe88bcaa12b1bbc6d93df3b08e. --- rules/catch-error-name.js | 4 +--- rules/no-console-spaces.js | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/rules/catch-error-name.js b/rules/catch-error-name.js index 9da3a5cde9..602b615129 100644 --- a/rules/catch-error-name.js +++ b/rules/catch-error-name.js @@ -13,9 +13,7 @@ function isLintablePromiseCatch(node) { const {property} = callee; - const prop = property - - if (prop.type !== 'Identifier' || prop.name !== 'catch') { + if (property.type !== 'Identifier' || property.name !== 'catch') { return false; } diff --git a/rules/no-console-spaces.js b/rules/no-console-spaces.js index e3feef1748..24f71fc908 100644 --- a/rules/no-console-spaces.js +++ b/rules/no-console-spaces.js @@ -34,7 +34,7 @@ const getArgumentValue = (context, nodeArgument) => { const sourceCode = context.getSourceCode(); value = sourceCode.getText(nodeArgument); // Strip off backticks - value = value.substring(1, value.length - 1); + value = value.slice(1, -1); } return value; From b69b2876be2525c2230d6280212a1f3311f7e10f Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Tue, 29 Oct 2019 11:20:46 +0800 Subject: [PATCH 06/14] Update test/lint/lint.js Co-Authored-By: yakov116 <16872793+yakov116@users.noreply.github.com> --- test/lint/lint.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/lint/lint.js b/test/lint/lint.js index ea94e690ea..a38f6f32ef 100755 --- a/test/lint/lint.js +++ b/test/lint/lint.js @@ -44,7 +44,7 @@ if (errorCount || warningCount) { console.log(); console.log('* If you\'re making a new rule, you can fix this later. *'); } else { - console.log('All test passed.'); + console.log('All test have passed.'); } process.exit(errorCount); From 8b5b81acd0a37b0f1b8863ba9963e65bcbf158fb Mon Sep 17 00:00:00 2001 From: fisker Date: Tue, 29 Oct 2019 11:33:18 +0800 Subject: [PATCH 07/14] Update messages --- test/lint/lint.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/lint/lint.js b/test/lint/lint.js index a38f6f32ef..a9ce1aa01e 100755 --- a/test/lint/lint.js +++ b/test/lint/lint.js @@ -19,6 +19,8 @@ const cli = new CLIEngine({ cli.addPlugin('eslint-plugin-unicorn', unicorn); +// Find a way to make sure rules are loaded from codebase + const report = cli.executeOnFiles(files); const {errorCount, warningCount, fixableErrorCount, fixableWarningCount} = report; @@ -34,7 +36,7 @@ if (errorCount || warningCount) { console.log(formatter(report.results)); console.log(); - console.log('Some test fails, you need fix them, and run `npm run lint ` again.'); + console.log('Some tests have failed, you need fix them and run `npm run lint ` to check again.'); if (hasFixable) { console.log(); @@ -44,7 +46,7 @@ if (errorCount || warningCount) { console.log(); console.log('* If you\'re making a new rule, you can fix this later. *'); } else { - console.log('All test have passed.'); + console.log('All tests have passed.'); } process.exit(errorCount); From 2000d57fa9cd30b20d4914374d2d167c2946e871 Mon Sep 17 00:00:00 2001 From: fisker Date: Tue, 29 Oct 2019 12:58:14 +0800 Subject: [PATCH 08/14] Add rules definition test --- test/lint/lint.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/test/lint/lint.js b/test/lint/lint.js index a9ce1aa01e..b6bed29b69 100755 --- a/test/lint/lint.js +++ b/test/lint/lint.js @@ -1,11 +1,14 @@ #!/usr/bin/env node 'use strict'; +const assert = require('assert'); const {CLIEngine} = require('eslint'); const unicorn = require('../..'); const {recommended} = unicorn.configs; const files = [process.argv[2] || '.']; const fix = process.argv.includes('--fix'); +const ruleIds = Object.keys(unicorn.rules); +const unicornRules = new Map(Object.entries(unicorn.rules)); const cli = new CLIEngine({ baseConfig: recommended, @@ -13,13 +16,27 @@ const cli = new CLIEngine({ // TODO: remove this override, when #391 is fixed 'unicorn/consistent-function-scoping': 'off' }, + // Prevent plugin load from other place + resolvePluginsRelativeTo: __dirname, useEslintrc: false, fix }); +// Make sure rules are not loaded +const loadedRulesBefore = cli.getRules(); +assert( + !ruleIds.some(ruleId => loadedRulesBefore.has(`unicorn/${ruleId}`)), + '`eslint-plugin-unicorn` rules should not loaded before `addPlugin` is called.' +) + cli.addPlugin('eslint-plugin-unicorn', unicorn); -// Find a way to make sure rules are loaded from codebase +// Make sure rules are loaded from codebase +const loadedRules = cli.getRules(); +assert( + ruleIds.every(ruleId => unicornRules.get(ruleId) === loadedRules.get(`unicorn/${ruleId}`)), + '`eslint-plugin-unicorn` rules are not loaded from codebase.' +) const report = cli.executeOnFiles(files); From eedffa17de0408a4bb69928a1cb389b000269706 Mon Sep 17 00:00:00 2001 From: fisker Date: Tue, 29 Oct 2019 13:09:00 +0800 Subject: [PATCH 09/14] remove assert --- test/lint/lint.js | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/test/lint/lint.js b/test/lint/lint.js index b6bed29b69..f9831b2d4f 100755 --- a/test/lint/lint.js +++ b/test/lint/lint.js @@ -1,6 +1,5 @@ #!/usr/bin/env node 'use strict'; -const assert = require('assert'); const {CLIEngine} = require('eslint'); const unicorn = require('../..'); @@ -16,27 +15,18 @@ const cli = new CLIEngine({ // TODO: remove this override, when #391 is fixed 'unicorn/consistent-function-scoping': 'off' }, - // Prevent plugin load from other place - resolvePluginsRelativeTo: __dirname, useEslintrc: false, fix }); -// Make sure rules are not loaded -const loadedRulesBefore = cli.getRules(); -assert( - !ruleIds.some(ruleId => loadedRulesBefore.has(`unicorn/${ruleId}`)), - '`eslint-plugin-unicorn` rules should not loaded before `addPlugin` is called.' -) - cli.addPlugin('eslint-plugin-unicorn', unicorn); // Make sure rules are loaded from codebase const loadedRules = cli.getRules(); -assert( - ruleIds.every(ruleId => unicornRules.get(ruleId) === loadedRules.get(`unicorn/${ruleId}`)), - '`eslint-plugin-unicorn` rules are not loaded from codebase.' -) +if (!ruleIds.every(ruleId => unicornRules.get(ruleId) === loadedRules.get(`unicorn/${ruleId}`))) { + console.error('`eslint-plugin-unicorn` rules are not loaded from codebase.'); + process.exit(1) +} const report = cli.executeOnFiles(files); @@ -53,7 +43,7 @@ if (errorCount || warningCount) { console.log(formatter(report.results)); console.log(); - console.log('Some tests have failed, you need fix them and run `npm run lint ` to check again.'); + console.log(`You need fix failed test${errorCount + warningCount > 1 ? 's' : ''} above and run \`npm run lint \` to check again.`); if (hasFixable) { console.log(); From 4d09bd5f7e91d52e71140ce7ce52e20579011e28 Mon Sep 17 00:00:00 2001 From: fisker Date: Tue, 29 Oct 2019 13:11:13 +0800 Subject: [PATCH 10/14] style --- test/lint/lint.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/lint/lint.js b/test/lint/lint.js index f9831b2d4f..b50f7b1af2 100755 --- a/test/lint/lint.js +++ b/test/lint/lint.js @@ -25,7 +25,7 @@ cli.addPlugin('eslint-plugin-unicorn', unicorn); const loadedRules = cli.getRules(); if (!ruleIds.every(ruleId => unicornRules.get(ruleId) === loadedRules.get(`unicorn/${ruleId}`))) { console.error('`eslint-plugin-unicorn` rules are not loaded from codebase.'); - process.exit(1) + process.exit(1); } const report = cli.executeOnFiles(files); From d4ef8f393c76669f38bccc8606d23183cd54982e Mon Sep 17 00:00:00 2001 From: fisker Date: Tue, 29 Oct 2019 13:18:10 +0800 Subject: [PATCH 11/14] should fail on `prefer-string-slice` rule --- rules/no-console-spaces.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rules/no-console-spaces.js b/rules/no-console-spaces.js index 24f71fc908..e3feef1748 100644 --- a/rules/no-console-spaces.js +++ b/rules/no-console-spaces.js @@ -34,7 +34,7 @@ const getArgumentValue = (context, nodeArgument) => { const sourceCode = context.getSourceCode(); value = sourceCode.getText(nodeArgument); // Strip off backticks - value = value.slice(1, -1); + value = value.substring(1, value.length - 1); } return value; From 22c12e7f50b629a8d119a715dcc7299bae9a3ec9 Mon Sep 17 00:00:00 2001 From: fisker Date: Tue, 29 Oct 2019 13:22:45 +0800 Subject: [PATCH 12/14] Revert "should fail on `prefer-string-slice` rule" --- rules/no-console-spaces.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rules/no-console-spaces.js b/rules/no-console-spaces.js index e3feef1748..24f71fc908 100644 --- a/rules/no-console-spaces.js +++ b/rules/no-console-spaces.js @@ -34,7 +34,7 @@ const getArgumentValue = (context, nodeArgument) => { const sourceCode = context.getSourceCode(); value = sourceCode.getText(nodeArgument); // Strip off backticks - value = value.substring(1, value.length - 1); + value = value.slice(1, -1); } return value; From c64e983410438766473fb076c2d914c3094c6b97 Mon Sep 17 00:00:00 2001 From: fisker Date: Thu, 31 Oct 2019 16:16:55 +0800 Subject: [PATCH 13/14] add a note to `new-rule` --- docs/new-rule.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/new-rule.md b/docs/new-rule.md index 191d04d21e..7b688433df 100644 --- a/docs/new-rule.md +++ b/docs/new-rule.md @@ -24,6 +24,6 @@ Use the [`astexplorer` site](https://astexplorer.net) with the `espree` parser a *(The description should be the same as the heading of the documentation file).* - Run `$ npm test` to ensure the tests pass. - Run `$ npm run integration` to run the rules against real projects to ensure your rule does not fail on real-world code. -- Run `$ npm run lint` to run the rules against this repository to ensure code in the repository are following your rule. - Open a pull request with a title in exactly the format `` Add `rule-name` rule ``, for example, `` Add `no-unused-properties` rule ``. - The pull request description should include the issue it fixes, for example, `Fixes #123`. +- Run `$ npm run lint` to run the rules against codebase to ensure code in the repository are following your rule, _you can ignore this step until your PR is reviewed_. From 6c6a63e2c3e4de8365ef84d97327a759f817fb97 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Thu, 31 Oct 2019 16:19:41 +0800 Subject: [PATCH 14/14] Update test/lint/lint.js Co-Authored-By: yakov116 <16872793+yakov116@users.noreply.github.com> --- test/lint/lint.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/lint/lint.js b/test/lint/lint.js index b50f7b1af2..04e1ee34f1 100755 --- a/test/lint/lint.js +++ b/test/lint/lint.js @@ -43,7 +43,7 @@ if (errorCount || warningCount) { console.log(formatter(report.results)); console.log(); - console.log(`You need fix failed test${errorCount + warningCount > 1 ? 's' : ''} above and run \`npm run lint \` to check again.`); + console.log(`You need to fix the failed test${errorCount + warningCount > 1 ? 's' : ''} above and run \`npm run lint \` to check again.`); if (hasFixable) { console.log();