Skip to content

Commit 182810a

Browse files
committed
chore: code style fixes for jsx-whitespace-literal rule
1 parent 55d8a53 commit 182810a

File tree

1 file changed

+24
-29
lines changed

1 file changed

+24
-29
lines changed

src/rules/jsxWhitespaceLiteralRule.ts

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @license
3-
* Copyright 2017 Palantir Technologies, Inc.
3+
* Copyright 2019 Palantir Technologies, Inc.
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -23,8 +23,9 @@ const RESERVED_ENTITY = " ";
2323

2424
export class Rule extends Lint.Rules.AbstractRule {
2525
public static metadata: Lint.IRuleMetadata = {
26-
description: Lint.Utils.dedent
27-
`Warn if ' ' is used in JXS markup. Prefer {" "} over ' '`,
26+
description: Lint.Utils.dedent`
27+
Warn if ' ' is used in JSX markup. Prefer {" "} over ' '
28+
`,
2829
optionExamples: ["true"],
2930
options: null,
3031
optionsDescription: "",
@@ -40,49 +41,43 @@ export class Rule extends Lint.Rules.AbstractRule {
4041
}
4142
}
4243

43-
function getSpaces(numOfSpaces: number): string {
44-
return " ".repeat(numOfSpaces);
45-
}
46-
4744
function walk(ctx: Lint.WalkContext<void>): void {
4845
return ts.forEachChild(ctx.sourceFile, function cb(node: ts.Node): void {
4946
if (isJsxText(node)) {
50-
if (node.getText().indexOf(RESERVED_ENTITY) > -1) {
51-
const text: string = node.getText();
52-
const regex: RegExp = new RegExp(RESERVED_ENTITY, "g");
47+
const text = node.getText();
48+
if (text.indexOf(RESERVED_ENTITY) > -1) {
49+
const regex = new RegExp(RESERVED_ENTITY, "g");
5350
const startIndices: number[] = [];
5451
const endIndices: number[] = [];
55-
let countEnitiy: number = -1;
56-
let result: RegExpExecArray | null;
52+
let countEnitiy = -1;
53+
let result = regex.exec(text);
5754

58-
do {
59-
result = regex.exec(text);
60-
if (result !== null) {
61-
if (
62-
startIndices[countEnitiy] !== undefined &&
63-
endIndices[countEnitiy] !== undefined &&
64-
startIndices[countEnitiy] + endIndices[countEnitiy] === result.index
65-
) {
66-
endIndices[countEnitiy] = endIndices[countEnitiy] + RESERVED_ENTITY.length;
67-
} else {
68-
startIndices.push(result.index);
69-
endIndices.push(RESERVED_ENTITY.length);
70-
countEnitiy += 1;
71-
}
55+
while (result !== null) {
56+
if (
57+
startIndices[countEnitiy] !== undefined &&
58+
endIndices[countEnitiy] !== undefined &&
59+
startIndices[countEnitiy] + endIndices[countEnitiy] === result.index
60+
) {
61+
endIndices[countEnitiy] = endIndices[countEnitiy] + RESERVED_ENTITY.length;
62+
} else {
63+
startIndices.push(result.index);
64+
endIndices.push(RESERVED_ENTITY.length);
65+
countEnitiy += 1;
7266
}
73-
} while (result !== null);
67+
result = regex.exec(text);
68+
}
7469

7570
startIndices.forEach((startIndex, index) => {
7671
const start = node.getStart() + startIndex;
7772
const end = endIndices[index];
73+
const spaces = " ".repeat(end / RESERVED_ENTITY.length);
7874
const fix = Lint.Replacement.replaceFromTo(
7975
start,
8076
start + end,
81-
`{"${getSpaces(end / RESERVED_ENTITY.length)}"}`,
77+
`{"${spaces}"}`,
8278
);
8379

8480
ctx.addFailureAt(start, end, Rule.FAILURE_STRING, fix);
85-
8681
});
8782
}
8883
}

0 commit comments

Comments
 (0)