From 182810ab4e5f602c1bcebbe3beab24cd92bf5564 Mon Sep 17 00:00:00 2001 From: Adi Dahiya Date: Thu, 1 Aug 2019 17:13:58 -0400 Subject: [PATCH] chore: code style fixes for jsx-whitespace-literal rule --- src/rules/jsxWhitespaceLiteralRule.ts | 53 ++++++++++++--------------- 1 file changed, 24 insertions(+), 29 deletions(-) diff --git a/src/rules/jsxWhitespaceLiteralRule.ts b/src/rules/jsxWhitespaceLiteralRule.ts index eca9b0e..9f74f7b 100644 --- a/src/rules/jsxWhitespaceLiteralRule.ts +++ b/src/rules/jsxWhitespaceLiteralRule.ts @@ -1,6 +1,6 @@ /** * @license - * Copyright 2017 Palantir Technologies, Inc. + * Copyright 2019 Palantir Technologies, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,8 +23,9 @@ const RESERVED_ENTITY = " "; export class Rule extends Lint.Rules.AbstractRule { public static metadata: Lint.IRuleMetadata = { - description: Lint.Utils.dedent - `Warn if ' ' is used in JXS markup. Prefer {" "} over ' '`, + description: Lint.Utils.dedent` + Warn if ' ' is used in JSX markup. Prefer {" "} over ' ' + `, optionExamples: ["true"], options: null, optionsDescription: "", @@ -40,49 +41,43 @@ export class Rule extends Lint.Rules.AbstractRule { } } -function getSpaces(numOfSpaces: number): string { - return " ".repeat(numOfSpaces); -} - function walk(ctx: Lint.WalkContext): void { return ts.forEachChild(ctx.sourceFile, function cb(node: ts.Node): void { if (isJsxText(node)) { - if (node.getText().indexOf(RESERVED_ENTITY) > -1) { - const text: string = node.getText(); - const regex: RegExp = new RegExp(RESERVED_ENTITY, "g"); + const text = node.getText(); + if (text.indexOf(RESERVED_ENTITY) > -1) { + const regex = new RegExp(RESERVED_ENTITY, "g"); const startIndices: number[] = []; const endIndices: number[] = []; - let countEnitiy: number = -1; - let result: RegExpExecArray | null; + let countEnitiy = -1; + let result = regex.exec(text); - do { - result = regex.exec(text); - if (result !== null) { - if ( - startIndices[countEnitiy] !== undefined && - endIndices[countEnitiy] !== undefined && - startIndices[countEnitiy] + endIndices[countEnitiy] === result.index - ) { - endIndices[countEnitiy] = endIndices[countEnitiy] + RESERVED_ENTITY.length; - } else { - startIndices.push(result.index); - endIndices.push(RESERVED_ENTITY.length); - countEnitiy += 1; - } + while (result !== null) { + if ( + startIndices[countEnitiy] !== undefined && + endIndices[countEnitiy] !== undefined && + startIndices[countEnitiy] + endIndices[countEnitiy] === result.index + ) { + endIndices[countEnitiy] = endIndices[countEnitiy] + RESERVED_ENTITY.length; + } else { + startIndices.push(result.index); + endIndices.push(RESERVED_ENTITY.length); + countEnitiy += 1; } - } while (result !== null); + result = regex.exec(text); + } startIndices.forEach((startIndex, index) => { const start = node.getStart() + startIndex; const end = endIndices[index]; + const spaces = " ".repeat(end / RESERVED_ENTITY.length); const fix = Lint.Replacement.replaceFromTo( start, start + end, - `{"${getSpaces(end / RESERVED_ENTITY.length)}"}`, + `{"${spaces}"}`, ); ctx.addFailureAt(start, end, Rule.FAILURE_STRING, fix); - }); } }