Skip to content
This repository was archived by the owner on Mar 25, 2021. It is now read-only.

Commit 9924e7a

Browse files
vedadeeptaadidahiya
authored andcommitted
[object-literal-sort-keys] add ignore-blank-lines option (#4808)
1 parent 958ba28 commit 9924e7a

3 files changed

Lines changed: 47 additions & 0 deletions

File tree

src/rules/objectLiteralSortKeysRule.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,15 @@ import * as Lint from "../index";
2929

3030
import { codeExamples } from "./code-examples/objectLiteralSortKeys.examples";
3131

32+
const OPTION_IGNORE_BLANK_LINES = "ignore-blank-lines";
3233
const OPTION_IGNORE_CASE = "ignore-case";
3334
const OPTION_LOCALE_COMPARE = "locale-compare";
3435
const OPTION_MATCH_DECLARATION_ORDER = "match-declaration-order";
3536
const OPTION_MATCH_DECLARATION_ORDER_ONLY = "match-declaration-order-only";
3637
const OPTION_SHORTHAND_FIRST = "shorthand-first";
3738

3839
interface Options {
40+
ignoreBlankLines: boolean;
3941
ignoreCase: boolean;
4042
localeCompare: boolean;
4143
matchDeclarationOrder: boolean;
@@ -52,12 +54,14 @@ export class Rule extends Lint.Rules.OptionallyTypedRule {
5254
5355
When using the default alphabetical ordering, additional blank lines may be used to group
5456
object properties together while keeping the elements within each group in alphabetical order.
57+
To opt out of this use ${OPTION_IGNORE_BLANK_LINES} option.
5558
`,
5659
rationale: "Useful in preventing merge conflicts",
5760
optionsDescription: Lint.Utils.dedent`
5861
By default, this rule checks that keys are in alphabetical order.
5962
The following may optionally be passed:
6063
64+
* \`${OPTION_IGNORE_BLANK_LINES}\` will enforce alphabetical ordering regardless of blank lines between each key-value pair.
6165
* \`${OPTION_IGNORE_CASE}\` will compare keys in a case insensitive way.
6266
* \`${OPTION_LOCALE_COMPARE}\` will compare keys using the expected sort order of special characters, such as accents.
6367
* \`${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 {
8387
options: {
8488
type: "string",
8589
enum: [
90+
OPTION_IGNORE_BLANK_LINES,
8691
OPTION_IGNORE_CASE,
8792
OPTION_LOCALE_COMPARE,
8893
OPTION_MATCH_DECLARATION_ORDER,
@@ -94,6 +99,7 @@ export class Rule extends Lint.Rules.OptionallyTypedRule {
9499
true,
95100
[
96101
true,
102+
OPTION_IGNORE_BLANK_LINES,
97103
OPTION_IGNORE_CASE,
98104
OPTION_LOCALE_COMPARE,
99105
OPTION_MATCH_DECLARATION_ORDER,
@@ -162,6 +168,7 @@ export class Rule extends Lint.Rules.OptionallyTypedRule {
162168

163169
function parseOptions(ruleArguments: any[]): Options {
164170
return {
171+
ignoreBlankLines: has(OPTION_IGNORE_BLANK_LINES),
165172
ignoreCase: has(OPTION_IGNORE_CASE),
166173
localeCompare: has(OPTION_LOCALE_COMPARE),
167174
matchDeclarationOrder: has(OPTION_MATCH_DECLARATION_ORDER),
@@ -178,6 +185,7 @@ function walk(ctx: Lint.WalkContext<Options>, checker?: ts.TypeChecker): void {
178185
const {
179186
sourceFile,
180187
options: {
188+
ignoreBlankLines,
181189
ignoreCase,
182190
localeCompare,
183191
matchDeclarationOrder,
@@ -266,6 +274,14 @@ function walk(ctx: Lint.WalkContext<Options>, checker?: ts.TypeChecker): void {
266274
: localeCompare
267275
? lastKey.localeCompare(key) === 1
268276
: lastKey > key;
277+
278+
if (keyOrderDescending && ignoreBlankLines) {
279+
ctx.addFailureAtNode(
280+
property.name,
281+
Rule.FAILURE_STRING_ALPHABETICAL(property.name.text),
282+
);
283+
return;
284+
}
269285
if (keyOrderDescending && !hasBlankLineBefore(ctx.sourceFile, property)) {
270286
ctx.addFailureAtNode(
271287
property.name,
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
var testPassA = {
2+
a: 'a',
3+
4+
b: 'b',
5+
6+
c: 'c',
7+
};
8+
9+
var testFailA = {
10+
b: 'b',
11+
12+
a: 'a',
13+
~ [err % ('a')]
14+
c: 'c',
15+
};
16+
17+
var testFailB = {
18+
a: 'a',
19+
20+
c: 'c',
21+
22+
b: 'b',
23+
~ [err % ('b')]
24+
};
25+
26+
[err]: The key '%s' is not sorted alphabetically
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"rules": {
3+
"object-literal-sort-keys": [true, "ignore-blank-lines"]
4+
}
5+
}

0 commit comments

Comments
 (0)