From ee884754e4111e11994ff0df3f0c29e43e1dc3f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=96=9B=E5=AE=9A=E8=B0=94=E7=9A=84=E7=8C=AB?= Date: Fri, 1 Mar 2019 11:49:25 +0800 Subject: [PATCH] Chore: add utils for rule tests (#11453) --- tests/lib/rules/_utils.js | 25 +++++++++++++++++++++++++ tests/lib/rules/indent.js | 16 ++-------------- 2 files changed, 27 insertions(+), 14 deletions(-) create mode 100644 tests/lib/rules/_utils.js diff --git a/tests/lib/rules/_utils.js b/tests/lib/rules/_utils.js new file mode 100644 index 00000000000..a1b642727cc --- /dev/null +++ b/tests/lib/rules/_utils.js @@ -0,0 +1,25 @@ +/** + * @fileoverview uitls for rule tests. + * @author 唯然 + */ + +"use strict"; + +/** + * Prevents leading spaces in a multiline template literal from appearing in the resulting string + * @param {string[]} strings The strings in the template literal + * @returns {string} The template literal, with spaces removed from all lines + */ +function unIndent(strings) { + const templateValue = strings[0]; + const lines = templateValue.replace(/^\n/u, "").replace(/\n\s*$/u, "").split("\n"); + const lineIndents = lines.filter(line => line.trim()).map(line => line.match(/ */u)[0].length); + const minLineIndent = Math.min(...lineIndents); + + return lines.map(line => line.slice(minLineIndent)).join("\n"); +} + + +module.exports = { + unIndent +}; diff --git a/tests/lib/rules/indent.js b/tests/lib/rules/indent.js index 9ce941fb24e..45abc654d0e 100644 --- a/tests/lib/rules/indent.js +++ b/tests/lib/rules/indent.js @@ -21,6 +21,8 @@ const path = require("path"); const fixture = fs.readFileSync(path.join(__dirname, "../../fixtures/rules/indent/indent-invalid-fixture-1.js"), "utf8"); const fixedFixture = fs.readFileSync(path.join(__dirname, "../../fixtures/rules/indent/indent-valid-fixture-1.js"), "utf8"); const parser = require("../../fixtures/fixture-parser"); +const { unIndent } = require("./_utils"); + /** * Create error message object for failure cases with a single 'found' indentation type @@ -54,20 +56,6 @@ function expectedErrors(providedIndentType, providedErrors) { })); } -/** - * Prevents leading spaces in a multiline template literal from appearing in the resulting string - * @param {string[]} strings The strings in the template literal - * @returns {string} The template literal, with spaces removed from all lines - */ -function unIndent(strings) { - const templateValue = strings[0]; - const lines = templateValue.replace(/^\n/u, "").replace(/\n\s*$/u, "").split("\n"); - const lineIndents = lines.filter(line => line.trim()).map(line => line.match(/ */u)[0].length); - const minLineIndent = Math.min(...lineIndents); - - return lines.map(line => line.slice(minLineIndent)).join("\n"); -} - const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 8, ecmaFeatures: { jsx: true } } }); ruleTester.run("indent", rule, {