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

Commit

Permalink
[object-literal-sort-keys] add ignore-blank-lines option (#4808)
Browse files Browse the repository at this point in the history
  • Loading branch information
vedadeepta authored and adidahiya committed Jul 25, 2019
1 parent 958ba28 commit 9924e7a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/rules/objectLiteralSortKeysRule.ts
Expand Up @@ -29,13 +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";

interface Options {
ignoreBlankLines: boolean;
ignoreCase: boolean;
localeCompare: boolean;
matchDeclarationOrder: boolean;
Expand All @@ -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:
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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),
Expand All @@ -178,6 +185,7 @@ function walk(ctx: Lint.WalkContext<Options>, checker?: ts.TypeChecker): void {
const {
sourceFile,
options: {
ignoreBlankLines,
ignoreCase,
localeCompare,
matchDeclarationOrder,
Expand Down Expand Up @@ -266,6 +274,14 @@ function walk(ctx: Lint.WalkContext<Options>, 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,
Expand Down
@@ -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
@@ -0,0 +1,5 @@
{
"rules": {
"object-literal-sort-keys": [true, "ignore-blank-lines"]
}
}

0 comments on commit 9924e7a

Please sign in to comment.