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

Commit 6b6f43a

Browse files
authored
Fix lint error in objectLiteralSortKeysRule (#3118)
1 parent 9d712b3 commit 6b6f43a

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

src/rules/objectLiteralSortKeysRule.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export class Rule extends Lint.Rules.OptionallyTypedRule {
6262
public static FAILURE_STRING_ALPHABETICAL(name: string): string {
6363
return `The key '${name}' is not sorted alphabetically`;
6464
}
65+
6566
public static FAILURE_STRING_USE_DECLARATION_ORDER(propName: string, typeName: string | undefined): string {
6667
const type = typeName === undefined ? "its type declaration" : `'${typeName}'`;
6768
return `The key '${propName}' is not in the same order as it is in ${type}.`;
@@ -77,7 +78,10 @@ export class Rule extends Lint.Rules.OptionallyTypedRule {
7778

7879
public applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): Lint.RuleFailure[] {
7980
return this.applyWithFunction<Options, Options>(
80-
sourceFile, (ctx) => walk(ctx, program.getTypeChecker()), parseOptions(this.ruleArguments));
81+
sourceFile,
82+
(ctx) => walk(ctx, program.getTypeChecker()),
83+
parseOptions(this.ruleArguments),
84+
);
8185
}
8286
}
8387

@@ -93,7 +97,11 @@ function parseOptions(ruleArguments: any[]): Options {
9397
}
9498

9599
function walk(ctx: Lint.WalkContext<Options>, checker?: ts.TypeChecker): void {
96-
const { sourceFile, options: { ignoreCase, matchDeclarationOrder } } = ctx;
100+
const {
101+
sourceFile,
102+
options: { ignoreCase, matchDeclarationOrder },
103+
} = ctx;
104+
97105
ts.forEachChild(sourceFile, function cb(node): void {
98106
if (isObjectLiteralExpression(node) && node.properties.length > 1) {
99107
check(node);
@@ -145,7 +153,8 @@ function walk(ctx: Lint.WalkContext<Options>, checker?: ts.TypeChecker): void {
145153
function checkMatchesDeclarationOrder(
146154
{ properties }: ts.ObjectLiteralExpression,
147155
type: TypeLike,
148-
members: ReadonlyArray<{ name: ts.PropertyName }>): void {
156+
members: ReadonlyArray<{ name: ts.PropertyName }>,
157+
): void {
149158

150159
let memberIndex = 0;
151160
outer: for (const prop of properties) {
@@ -167,16 +176,20 @@ function walk(ctx: Lint.WalkContext<Options>, checker?: ts.TypeChecker): void {
167176

168177
// This We didn't find the member we were looking for past the previous member,
169178
// so it must have come before it and is therefore out of order.
170-
ctx.addFailureAtNode(prop.name, Rule.FAILURE_STRING_USE_DECLARATION_ORDER(propName, typeName(type)));
179+
ctx.addFailureAtNode(prop.name, Rule.FAILURE_STRING_USE_DECLARATION_ORDER(propName, getTypeName(type)));
171180
// Don't bother with multiple errors.
172181
break;
173182
}
174183
}
175184
}
176185

177-
function typeName(t: TypeLike): string | undefined {
186+
function getTypeName(t: TypeLike): string | undefined {
178187
const parent = t.parent!;
179-
return t.kind === ts.SyntaxKind.InterfaceDeclaration ? t.name.text : isTypeAliasDeclaration(parent) ? parent.name.text : undefined;
188+
return t.kind === ts.SyntaxKind.InterfaceDeclaration
189+
? t.name.text
190+
: isTypeAliasDeclaration(parent)
191+
? parent.name.text
192+
: undefined;
180193
}
181194

182195
type TypeLike = ts.InterfaceDeclaration | ts.TypeLiteralNode;

0 commit comments

Comments
 (0)