Skip to content

Commit

Permalink
New rule: jsx-curly-brace-presence (#196)
Browse files Browse the repository at this point in the history
  • Loading branch information
OlafMerkert authored and adidahiya committed Mar 28, 2019
1 parent 8bf420c commit d229c84
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 0 deletions.
109 changes: 109 additions & 0 deletions src/rules/jsxCurlyBracePresenceRule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/**
* @license
* 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as Lint from "tslint";
import { isJsxAttribute, isJsxExpression, isStringLiteral, isTextualLiteral } from "tsutils";
import * as ts from "typescript";

const OPTION_ALWAYS = "always";
const OPTION_NEVER = "never";
const CURLY_PRESENCE_VALUES = [OPTION_ALWAYS, OPTION_NEVER];
const CURLY_PRESENCE_OBJECT = {
enum: CURLY_PRESENCE_VALUES,
type: "string",
};

export class Rule extends Lint.Rules.AbstractRule {
/* tslint:disable:object-literal-sort-keys */
public static metadata: Lint.IRuleMetadata = {
ruleName: "jsx-curly-brace-presence",
description: "Enforce curly braces or disallow unnecessary curly braces in JSX props",
hasFix: true,
optionsDescription: Lint.Utils.dedent`
One of the following options may be provided under the "props" key:
* \`"${OPTION_ALWAYS}"\` requires JSX attributes to have curly braces around string literal values
* \`"${OPTION_NEVER}"\` requires JSX attributes to NOT have curly braces around string literal values
If no option is provided, "${OPTION_NEVER}" is chosen as default.`,
options: {
type: "object",
properties: {
props: CURLY_PRESENCE_OBJECT,
},
},
optionExamples: [
`{ props: "${OPTION_ALWAYS}" }`,
`{ props: "${OPTION_NEVER}" }`,
],
type: "style",
typescriptOnly: false,
};
/* tslint:enable:object-literal-sort-keys */

public static FAILURE_CURLY_BRACE_SUPERFLUOUS = "JSX attribute must NOT have curly braces around string literal";
public static FAILURE_CURLY_BRACE_MISSING = "JSX attribute must have curly braces around string literal";

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
const option = Array.isArray(this.ruleArguments) ? this.ruleArguments[0] : undefined;

return this.applyWithFunction(sourceFile, walk, option);
}
}

function walk(ctx: Lint.WalkContext<{ props: string } | undefined>): void {
return ts.forEachChild(ctx.sourceFile, validateCurlyBraces);

function validateCurlyBraces(node: ts.Node): void {
if (isJsxAttribute(node)) {
if (typeof ctx.options === "object" && ctx.options.props === OPTION_ALWAYS) {
validateCurlyBracesArePresent(node);
} else {
validateCurlyBracesAreNotPresent(node);
}
}
return ts.forEachChild(node, validateCurlyBraces);
}

function validateCurlyBracesArePresent(node: ts.JsxAttribute) {
const { initializer } = node;
if (initializer !== undefined) {
const hasStringInitializer = initializer.kind === ts.SyntaxKind.StringLiteral;
if (hasStringInitializer) {
const fix = Lint.Replacement.replaceNode(initializer, `{${initializer.getText()}}`);
ctx.addFailureAtNode(initializer, Rule.FAILURE_CURLY_BRACE_MISSING, fix);
}
}
}

function validateCurlyBracesAreNotPresent(node: ts.JsxAttribute) {
const { initializer } = node;
if (initializer !== undefined
&& isJsxExpression(initializer)
&& initializer.expression !== undefined) {
if (isStringLiteral(initializer.expression)) {
const stringLiteralWithoutCurlies: string = initializer.expression.getText();
const fix = Lint.Replacement.replaceNode(initializer, stringLiteralWithoutCurlies);
ctx.addFailureAtNode(initializer, Rule.FAILURE_CURLY_BRACE_SUPERFLUOUS, fix);
} else if (isTextualLiteral(initializer.expression)) {
const textualLiteralContent = initializer.expression.text;
const fix = Lint.Replacement.replaceNode(initializer, `"${textualLiteralContent}"`);
ctx.addFailureAtNode(initializer, Rule.FAILURE_CURLY_BRACE_SUPERFLUOUS, fix);
}
}
}
}
3 changes: 3 additions & 0 deletions test/rules/jsx-curly-brace-presence/always/test.tsx.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const e1 = (<p className={"good"}>some text</p>);

const e2 = (<p className={"bad"}>some text</p>);
6 changes: 6 additions & 0 deletions test/rules/jsx-curly-brace-presence/always/test.tsx.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const e1 = (<p className={"good"}>some text</p>);

const e2 = (<p className="bad">some text</p>);
~~~~~ [0]

[0]: JSX attribute must have curly braces around string literal
5 changes: 5 additions & 0 deletions test/rules/jsx-curly-brace-presence/always/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"jsx-curly-brace-presence": { "options": { "props": "always" } }
}
}
1 change: 1 addition & 0 deletions test/rules/jsx-curly-brace-presence/default/test.tsx.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const e1 = (<p className="bad">some text</p>);
4 changes: 4 additions & 0 deletions test/rules/jsx-curly-brace-presence/default/test.tsx.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const e1 = (<p className={"bad"}>some text</p>);
~~~~~~~ [0]

[0]: JSX attribute must NOT have curly braces around string literal
5 changes: 5 additions & 0 deletions test/rules/jsx-curly-brace-presence/default/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"jsx-curly-brace-presence": true
}
}
7 changes: 7 additions & 0 deletions test/rules/jsx-curly-brace-presence/never/test.tsx.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const e1 = (<p className="bad">some text</p>);

const e2 = (<p className="good">some text</p>);

const e3 = (<p className={`good ${"a"}`}>some text</p>);

const e4 = (<p className="bad">some text</p>);
11 changes: 11 additions & 0 deletions test/rules/jsx-curly-brace-presence/never/test.tsx.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const e1 = (<p className={"bad"}>some text</p>);
~~~~~~~ [0]

const e2 = (<p className="good">some text</p>);

const e3 = (<p className={`good ${"a"}`}>some text</p>);

const e4 = (<p className={`bad`}>some text</p>);
~~~~~~~ [0]

[0]: JSX attribute must NOT have curly braces around string literal
5 changes: 5 additions & 0 deletions test/rules/jsx-curly-brace-presence/never/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"jsx-curly-brace-presence": { "options": { "props": "never" } }
}
}

0 comments on commit d229c84

Please sign in to comment.