Skip to content

Commit

Permalink
New rule: jsx-ban-elements (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
dryganets authored and adidahiya committed Jan 11, 2018
1 parent bb323b1 commit 9cb1347
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ The built-in configuration preset you get with `"extends": "tslint-react"` is se
size={size}
/>
```
- `jsx-ban-elements` (since v3.3.4)
- Allows blacklisting of JSX elements with an optional explanatory message in the reported failure.
- `jsx-ban-props` (since v2.3.0)
- Allows blacklisting of props in JSX with an optional explanatory message in the reported failure.
- `jsx-boolean-value` (since v2.5.0)
Expand Down
76 changes: 76 additions & 0 deletions src/rules/jsxBanElementsRule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* @license
* Copyright 2018 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 { isJsxOpeningElement, isJsxSelfClosingElement } from "tsutils";
import * as ts from "typescript";

interface IOption {
pattern: RegExp;
message?: string;
}

export class Rule extends Lint.Rules.AbstractRule {
/* tslint:disable:object-literal-sort-keys */
public static metadata: Lint.IRuleMetadata = {
ruleName: "jsx-ban-elements",
description: Lint.Utils.dedent`
Bans specific JSX elements from being used.`,
options: {
type: "list",
listType: {
type: "array",
items: { type: "string" },
minLength: 1,
maxLength: 2,
},
},
optionsDescription: Lint.Utils.dedent`
A list of \`["regex", "optional explanation here"]\`, which bans
types that match \`regex\``,
optionExamples: [[true, ["Object", "Use {} instead."], ["String"]]],
type: "typescript",
typescriptOnly: true,
};
/* tslint:enable:object-literal-sort-keys */

public static FAILURE_STRING_FACTORY(elementName: string, messageAddition?: string) {
return `JSX element '${elementName}' is banned.${messageAddition !== undefined ? ` ${messageAddition}` : ""}`;
}

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithFunction(sourceFile, walk, this.ruleArguments.map(parseOption));
}
}

function parseOption([pattern, message]: [string, string | undefined]): IOption {
return {message, pattern: new RegExp(`${pattern}`)};
}

function walk(ctx: Lint.WalkContext<IOption[]>) {
return ts.forEachChild(ctx.sourceFile, function cb(node: ts.Node): void {
if (isJsxOpeningElement(node) || isJsxSelfClosingElement(node)) {
const typeName = node.tagName.getText();
for (const ban of ctx.options) {
if (ban.pattern.test(typeName)) {
ctx.addFailureAtNode(node.tagName, Rule.FAILURE_STRING_FACTORY(typeName, ban.message));
break;
}
}
}
return ts.forEachChild(node, cb);
});
}
8 changes: 8 additions & 0 deletions test/rules/jsx-ban-elements/test.tsx.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<span className="primary" foo="bar">baz</span>
~~~~ [JSX element 'span' is banned. Use div instead.]

<span bar={5}/>
~~~~ [JSX element 'span' is banned. Use div instead.]

<button type="button">bar</button>

9 changes: 9 additions & 0 deletions test/rules/jsx-ban-elements/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"rules": {
"jsx-ban-elements": {
"options": [
["span", "Use div instead."]
]
}
}
}

0 comments on commit 9cb1347

Please sign in to comment.