Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New rule: jsx-curly-brace-presence #196

Merged
merged 14 commits into from
Mar 28, 2019
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 two options may be provided:
OlafMerkert marked this conversation as resolved.
Show resolved Hide resolved

* \`"${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: [
`[true, { props: "${OPTION_ALWAYS}" }]`,
OlafMerkert marked this conversation as resolved.
Show resolved Hide resolved
`[true, { 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": [true, { "props": "always" }]
OlafMerkert marked this conversation as resolved.
Show resolved Hide resolved
}
}
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": [true, { "props": "never" }]
OlafMerkert marked this conversation as resolved.
Show resolved Hide resolved
}
}