Skip to content

Commit

Permalink
Chore: add utils for rule tests (#11453)
Browse files Browse the repository at this point in the history
  • Loading branch information
aladdin-add committed Mar 1, 2019
1 parent d4824e4 commit ee88475
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
25 changes: 25 additions & 0 deletions tests/lib/rules/_utils.js
@@ -0,0 +1,25 @@
/**
* @fileoverview uitls for rule tests.
* @author 唯然<weiran.zsd@outlook.com>
*/

"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
};
16 changes: 2 additions & 14 deletions tests/lib/rules/indent.js
Expand Up @@ -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
Expand Down Expand Up @@ -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, {
Expand Down

0 comments on commit ee88475

Please sign in to comment.