diff --git a/src/rules/objectLiteralSortKeysRule.ts b/src/rules/objectLiteralSortKeysRule.ts index 953ecc85259..961d294006b 100644 --- a/src/rules/objectLiteralSortKeysRule.ts +++ b/src/rules/objectLiteralSortKeysRule.ts @@ -29,6 +29,7 @@ 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"; @@ -36,6 +37,7 @@ const OPTION_MATCH_DECLARATION_ORDER_ONLY = "match-declaration-order-only"; const OPTION_SHORTHAND_FIRST = "shorthand-first"; interface Options { + ignoreBlankLines: 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_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_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: @@ -83,6 +87,7 @@ export class Rule extends Lint.Rules.OptionallyTypedRule { options: { type: "string", enum: [ + OPTION_IGNORE_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_IGNORE_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 { + ignoreBlankLines: has(OPTION_IGNORE_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: { + ignoreBlankLines, 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 && ignoreBlankLines) { + 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/ignore-blank-lines/test.ts.lint b/test/rules/object-literal-sort-keys/ignore-blank-lines/test.ts.lint new file mode 100644 index 00000000000..328b2911a5d --- /dev/null +++ b/test/rules/object-literal-sort-keys/ignore-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/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"] + } +}