From 256c4a2b3610063d0f16b5b9fc579036b26e3d98 Mon Sep 17 00:00:00 2001 From: Annie Zhang Date: Mon, 29 Aug 2016 20:04:56 -0400 Subject: [PATCH] Fix: Allow separate mode option for multiline and align (fixes #6691) (#6991) --- lib/rules/key-spacing.js | 5 +- tests/lib/rules/key-spacing.js | 106 +++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+), 1 deletion(-) diff --git a/lib/rules/key-spacing.js b/lib/rules/key-spacing.js index 0b39f7429a2..b195d31d32a 100644 --- a/lib/rules/key-spacing.js +++ b/lib/rules/key-spacing.js @@ -136,7 +136,7 @@ function initOptions(toOptions, fromOptions) { if (toOptions.multiLine.align) { toOptions.align = { on: toOptions.multiLine.align.on, - mode: toOptions.multiLine.mode, + mode: toOptions.multiLine.align.mode || toOptions.multiLine.mode, beforeColon: toOptions.multiLine.align.beforeColon, afterColon: toOptions.multiLine.align.afterColon }; @@ -289,6 +289,9 @@ module.exports = { multiLine: { type: "object", properties: { + mode: { + enum: ["strict", "minimum"] + }, beforeColon: { type: "boolean" }, diff --git a/tests/lib/rules/key-spacing.js b/tests/lib/rules/key-spacing.js index 7101b803c91..bf1a12c728c 100644 --- a/tests/lib/rules/key-spacing.js +++ b/tests/lib/rules/key-spacing.js @@ -711,6 +711,56 @@ ruleTester.run("key-spacing", rule, { } }], parserOptions: { ecmaVersion: 6, ecmaFeatures: { experimentalObjectRestSpread: true } } + }, { + code: [ + "var obj = {", + " key1: 1,", + "", + " key2: 2,", + " key3: 3,", + "", + " key4: 4", + "}" + ].join("\n"), + options: [{ + multiLine: { + beforeColon: false, + afterColon: true, + mode: "strict", + align: { + beforeColon: false, + afterColon: true, + on: "colon", + mode: "minimum" + } + } + }], + parserOptions: { ecmaVersion: 6, ecmaFeatures: { experimentalObjectRestSpread: true } } + }, { + code: [ + "var obj = {", + " key1: 1,", + "", + " key2: 2,", + " key3: 3,", + "", + " key4: 4", + "}" + ].join("\n"), + options: [{ + multiLine: { + beforeColon: false, + afterColon: true, + mode: "strict" + }, + align: { + beforeColon: false, + afterColon: true, + on: "colon", + mode: "minimum" + } + }], + parserOptions: { ecmaVersion: 6, ecmaFeatures: { experimentalObjectRestSpread: true } } }], invalid: [{ @@ -1622,5 +1672,61 @@ ruleTester.run("key-spacing", rule, { { message: "Extra space before value for key \'func2\'.", line: 8, column: 16, type: "FunctionExpression" }, { message: "Extra space after key \'singleLine\'.", line: 11, column: 5, type: "Identifier" } ] + }, { + code: [ + "var obj = {", + " key1: 1,", + "", + " key2: 2,", + " key3: 3,", + "", + " key4: 4", + "}" + ].join("\n"), + options: [{ + multiLine: { + beforeColon: false, + afterColon: true, + mode: "strict", + align: { + beforeColon: false, + afterColon: true, + on: "colon", + } + } + }], + parserOptions: { ecmaVersion: 6, ecmaFeatures: { experimentalObjectRestSpread: true } }, + errors: [ + { message: "Extra space before value for key \'key2\'.", line: 4, column: 14, type: "Literal" }, + { message: "Extra space before value for key \'key3\'.", line: 5, column: 14, type: "Literal" } + ] + }, { + code: [ + "var obj = {", + " key1: 1,", + "", + " key2: 2,", + " key3: 3,", + "", + " key4: 4", + "}" + ].join("\n"), + options: [{ + multiLine: { + beforeColon: false, + afterColon: true, + mode: "strict" + }, + align: { + beforeColon: false, + afterColon: true, + on: "colon", + } + }], + parserOptions: { ecmaVersion: 6, ecmaFeatures: { experimentalObjectRestSpread: true } }, + errors: [ + { message: "Extra space before value for key \'key2\'.", line: 4, column: 14, type: "Literal" }, + { message: "Extra space before value for key \'key3\'.", line: 5, column: 14, type: "Literal" } + ] }] });