From 52cf774b3823e685f5580c9f8f62877b64105d72 Mon Sep 17 00:00:00 2001 From: Veda Date: Wed, 24 Jul 2019 23:29:02 +0530 Subject: [PATCH 1/2] fix: add option to enfore alphabetical ordering regarding of blank lines --- src/rules/objectLiteralSortKeysRule.ts | 16 ++++++++++++ .../allow-blank-lines/test.ts.lint | 26 +++++++++++++++++++ .../allow-blank-lines/tslint.json | 5 ++++ 3 files changed, 47 insertions(+) create mode 100644 test/rules/object-literal-sort-keys/allow-blank-lines/test.ts.lint create mode 100644 test/rules/object-literal-sort-keys/allow-blank-lines/tslint.json diff --git a/src/rules/objectLiteralSortKeysRule.ts b/src/rules/objectLiteralSortKeysRule.ts index 953ecc85259..6d91b3834b5 100644 --- a/src/rules/objectLiteralSortKeysRule.ts +++ b/src/rules/objectLiteralSortKeysRule.ts @@ -34,8 +34,10 @@ const OPTION_LOCALE_COMPARE = "locale-compare"; const OPTION_MATCH_DECLARATION_ORDER = "match-declaration-order"; const OPTION_MATCH_DECLARATION_ORDER_ONLY = "match-declaration-order-only"; const OPTION_SHORTHAND_FIRST = "shorthand-first"; +const OPTION_ALLOW_BLANK_LINES = "allow-blank-lines"; interface Options { + allowBlankLines: boolean; ignoreCase: boolean; localeCompare: boolean; matchDeclarationOrder: boolean; @@ -52,12 +54,14 @@ export class Rule extends Lint.Rules.OptionallyTypedRule { When using the default alphabetical ordering, additional blank lines may be used to group object properties together while keeping the elements within each group in alphabetical order. + To opt out of this use ${OPTION_ALLOW_BLANK_LINES} option. `, rationale: "Useful in preventing merge conflicts", optionsDescription: Lint.Utils.dedent` By default, this rule checks that keys are in alphabetical order. The following may optionally be passed: + * \`${OPTION_ALLOW_BLANK_LINES}\` will enforce alphabetical ordering regardless of blank lines between each key-value pair. * \`${OPTION_IGNORE_CASE}\` will compare keys in a case insensitive way. * \`${OPTION_LOCALE_COMPARE}\` will compare keys using the expected sort order of special characters, such as accents. * \`${OPTION_MATCH_DECLARATION_ORDER}\` will prefer to use the key ordering of the contextual type of the object literal, as in: @@ -83,6 +87,7 @@ export class Rule extends Lint.Rules.OptionallyTypedRule { options: { type: "string", enum: [ + OPTION_ALLOW_BLANK_LINES, OPTION_IGNORE_CASE, OPTION_LOCALE_COMPARE, OPTION_MATCH_DECLARATION_ORDER, @@ -94,6 +99,7 @@ export class Rule extends Lint.Rules.OptionallyTypedRule { true, [ true, + OPTION_ALLOW_BLANK_LINES, OPTION_IGNORE_CASE, OPTION_LOCALE_COMPARE, OPTION_MATCH_DECLARATION_ORDER, @@ -162,6 +168,7 @@ export class Rule extends Lint.Rules.OptionallyTypedRule { function parseOptions(ruleArguments: any[]): Options { return { + allowBlankLines: has(OPTION_ALLOW_BLANK_LINES), ignoreCase: has(OPTION_IGNORE_CASE), localeCompare: has(OPTION_LOCALE_COMPARE), matchDeclarationOrder: has(OPTION_MATCH_DECLARATION_ORDER), @@ -178,6 +185,7 @@ function walk(ctx: Lint.WalkContext, checker?: ts.TypeChecker): void { const { sourceFile, options: { + allowBlankLines, ignoreCase, localeCompare, matchDeclarationOrder, @@ -266,6 +274,14 @@ function walk(ctx: Lint.WalkContext, checker?: ts.TypeChecker): void { : localeCompare ? lastKey.localeCompare(key) === 1 : lastKey > key; + + if (keyOrderDescending && allowBlankLines) { + ctx.addFailureAtNode( + property.name, + Rule.FAILURE_STRING_ALPHABETICAL(property.name.text), + ); + return; + } if (keyOrderDescending && !hasBlankLineBefore(ctx.sourceFile, property)) { ctx.addFailureAtNode( property.name, diff --git a/test/rules/object-literal-sort-keys/allow-blank-lines/test.ts.lint b/test/rules/object-literal-sort-keys/allow-blank-lines/test.ts.lint new file mode 100644 index 00000000000..328b2911a5d --- /dev/null +++ b/test/rules/object-literal-sort-keys/allow-blank-lines/test.ts.lint @@ -0,0 +1,26 @@ +var testPassA = { + a: 'a', + + b: 'b', + + c: 'c', +}; + +var testFailA = { + b: 'b', + + a: 'a', + ~ [err % ('a')] + c: 'c', +}; + +var testFailB = { + a: 'a', + + c: 'c', + + b: 'b', + ~ [err % ('b')] +}; + +[err]: The key '%s' is not sorted alphabetically \ No newline at end of file diff --git a/test/rules/object-literal-sort-keys/allow-blank-lines/tslint.json b/test/rules/object-literal-sort-keys/allow-blank-lines/tslint.json new file mode 100644 index 00000000000..450bdbaef9d --- /dev/null +++ b/test/rules/object-literal-sort-keys/allow-blank-lines/tslint.json @@ -0,0 +1,5 @@ +{ + "rules": { + "object-literal-sort-keys": [true, "allow-blank-lines"] + } +} From 99f8740569cbf2ba79c846364ce03a041b8ab649 Mon Sep 17 00:00:00 2001 From: Veda Date: Thu, 25 Jul 2019 21:06:09 +0530 Subject: [PATCH 2/2] fix: rename option to --- src/rules/objectLiteralSortKeysRule.ts | 18 +++++++++--------- .../allow-blank-lines/tslint.json | 5 ----- .../test.ts.lint | 0 .../ignore-blank-lines/tslint.json | 5 +++++ 4 files changed, 14 insertions(+), 14 deletions(-) delete mode 100644 test/rules/object-literal-sort-keys/allow-blank-lines/tslint.json rename test/rules/object-literal-sort-keys/{allow-blank-lines => ignore-blank-lines}/test.ts.lint (100%) create mode 100644 test/rules/object-literal-sort-keys/ignore-blank-lines/tslint.json diff --git a/src/rules/objectLiteralSortKeysRule.ts b/src/rules/objectLiteralSortKeysRule.ts index 6d91b3834b5..961d294006b 100644 --- a/src/rules/objectLiteralSortKeysRule.ts +++ b/src/rules/objectLiteralSortKeysRule.ts @@ -29,15 +29,15 @@ import * as Lint from "../index"; import { codeExamples } from "./code-examples/objectLiteralSortKeys.examples"; +const OPTION_IGNORE_BLANK_LINES = "ignore-blank-lines"; const OPTION_IGNORE_CASE = "ignore-case"; const OPTION_LOCALE_COMPARE = "locale-compare"; const OPTION_MATCH_DECLARATION_ORDER = "match-declaration-order"; const OPTION_MATCH_DECLARATION_ORDER_ONLY = "match-declaration-order-only"; const OPTION_SHORTHAND_FIRST = "shorthand-first"; -const OPTION_ALLOW_BLANK_LINES = "allow-blank-lines"; interface Options { - allowBlankLines: boolean; + ignoreBlankLines: boolean; ignoreCase: boolean; localeCompare: boolean; matchDeclarationOrder: boolean; @@ -54,14 +54,14 @@ export class Rule extends Lint.Rules.OptionallyTypedRule { When using the default alphabetical ordering, additional blank lines may be used to group object properties together while keeping the elements within each group in alphabetical order. - To opt out of this use ${OPTION_ALLOW_BLANK_LINES} option. + To opt out of this use ${OPTION_IGNORE_BLANK_LINES} option. `, rationale: "Useful in preventing merge conflicts", optionsDescription: Lint.Utils.dedent` By default, this rule checks that keys are in alphabetical order. The following may optionally be passed: - * \`${OPTION_ALLOW_BLANK_LINES}\` will enforce alphabetical ordering regardless of blank lines between each key-value pair. + * \`${OPTION_IGNORE_BLANK_LINES}\` will enforce alphabetical ordering regardless of blank lines between each key-value pair. * \`${OPTION_IGNORE_CASE}\` will compare keys in a case insensitive way. * \`${OPTION_LOCALE_COMPARE}\` will compare keys using the expected sort order of special characters, such as accents. * \`${OPTION_MATCH_DECLARATION_ORDER}\` will prefer to use the key ordering of the contextual type of the object literal, as in: @@ -87,7 +87,7 @@ export class Rule extends Lint.Rules.OptionallyTypedRule { options: { type: "string", enum: [ - OPTION_ALLOW_BLANK_LINES, + OPTION_IGNORE_BLANK_LINES, OPTION_IGNORE_CASE, OPTION_LOCALE_COMPARE, OPTION_MATCH_DECLARATION_ORDER, @@ -99,7 +99,7 @@ export class Rule extends Lint.Rules.OptionallyTypedRule { true, [ true, - OPTION_ALLOW_BLANK_LINES, + OPTION_IGNORE_BLANK_LINES, OPTION_IGNORE_CASE, OPTION_LOCALE_COMPARE, OPTION_MATCH_DECLARATION_ORDER, @@ -168,7 +168,7 @@ export class Rule extends Lint.Rules.OptionallyTypedRule { function parseOptions(ruleArguments: any[]): Options { return { - allowBlankLines: has(OPTION_ALLOW_BLANK_LINES), + ignoreBlankLines: has(OPTION_IGNORE_BLANK_LINES), ignoreCase: has(OPTION_IGNORE_CASE), localeCompare: has(OPTION_LOCALE_COMPARE), matchDeclarationOrder: has(OPTION_MATCH_DECLARATION_ORDER), @@ -185,7 +185,7 @@ function walk(ctx: Lint.WalkContext, checker?: ts.TypeChecker): void { const { sourceFile, options: { - allowBlankLines, + ignoreBlankLines, ignoreCase, localeCompare, matchDeclarationOrder, @@ -275,7 +275,7 @@ function walk(ctx: Lint.WalkContext, checker?: ts.TypeChecker): void { ? lastKey.localeCompare(key) === 1 : lastKey > key; - if (keyOrderDescending && allowBlankLines) { + if (keyOrderDescending && ignoreBlankLines) { ctx.addFailureAtNode( property.name, Rule.FAILURE_STRING_ALPHABETICAL(property.name.text), diff --git a/test/rules/object-literal-sort-keys/allow-blank-lines/tslint.json b/test/rules/object-literal-sort-keys/allow-blank-lines/tslint.json deleted file mode 100644 index 450bdbaef9d..00000000000 --- a/test/rules/object-literal-sort-keys/allow-blank-lines/tslint.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "rules": { - "object-literal-sort-keys": [true, "allow-blank-lines"] - } -} diff --git a/test/rules/object-literal-sort-keys/allow-blank-lines/test.ts.lint b/test/rules/object-literal-sort-keys/ignore-blank-lines/test.ts.lint similarity index 100% rename from test/rules/object-literal-sort-keys/allow-blank-lines/test.ts.lint rename to test/rules/object-literal-sort-keys/ignore-blank-lines/test.ts.lint diff --git a/test/rules/object-literal-sort-keys/ignore-blank-lines/tslint.json b/test/rules/object-literal-sort-keys/ignore-blank-lines/tslint.json new file mode 100644 index 00000000000..fd1360f919a --- /dev/null +++ b/test/rules/object-literal-sort-keys/ignore-blank-lines/tslint.json @@ -0,0 +1,5 @@ +{ + "rules": { + "object-literal-sort-keys": [true, "ignore-blank-lines"] + } +}