Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7700599

Browse files
committed
Added new jsx-ban-elements rule to ban jsx elements
1 parent bb323b1 commit 7700599

File tree

5 files changed

+94
-1
lines changed

5 files changed

+94
-1
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ The built-in configuration preset you get with `"extends": "tslint-react"` is se
5252
size={size}
5353
/>
5454
```
55+
- `jsx-ban-elements` (since v3.3.4)
56+
- Allows blacklisting of JSX elements with an optional explanatory message in the reported failure.
5557
- `jsx-ban-props` (since v2.3.0)
5658
- Allows blacklisting of props in JSX with an optional explanatory message in the reported failure.
5759
- `jsx-boolean-value` (since v2.5.0)

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tslint-react",
3-
"version": "3.3.3",
3+
"version": "3.3.4",
44
"description": "Lint rules related to React & JSX for TSLint",
55
"main": "tslint-react.json",
66
"scripts": {

src/rules/jsxBanElementsRule.ts

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
* @license
3+
* Copyright 2018 Palantir Technologies, Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
import * as Lint from "tslint";
18+
import { isJsxOpeningElement, isJsxSelfClosingElement } from "tsutils";
19+
import * as ts from "typescript";
20+
21+
interface IOption {
22+
pattern: RegExp;
23+
message?: string;
24+
}
25+
26+
export class Rule extends Lint.Rules.AbstractRule {
27+
/* tslint:disable:object-literal-sort-keys */
28+
public static metadata: Lint.IRuleMetadata = {
29+
ruleName: "jsx-ban-elements",
30+
description: Lint.Utils.dedent`
31+
Bans specific jsx elements from being used.`,
32+
options: {
33+
type: "list",
34+
listType: {
35+
type: "array",
36+
items: { type: "string" },
37+
minLength: 1,
38+
maxLength: 2,
39+
},
40+
},
41+
optionsDescription: Lint.Utils.dedent`
42+
A list of \`["regex", "optional explanation here"]\`, which bans
43+
types that match \`regex\``,
44+
optionExamples: [[true, ["Object", "Use {} instead."], ["String"]]],
45+
type: "typescript",
46+
typescriptOnly: true,
47+
};
48+
/* tslint:enable:object-literal-sort-keys */
49+
50+
public static FAILURE_STRING_FACTORY(elementName: string, messageAddition?: string) {
51+
return `JSX element '${elementName}' is banned.${messageAddition !== undefined ? ` ${messageAddition}` : ""}`;
52+
}
53+
54+
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
55+
return this.applyWithFunction(sourceFile, walk, this.ruleArguments.map(parseOption));
56+
}
57+
}
58+
59+
function parseOption([pattern, message]: [string, string | undefined]): IOption {
60+
return {message, pattern: new RegExp(`^${pattern}$`)};
61+
}
62+
63+
function walk(ctx: Lint.WalkContext<IOption[]>) {
64+
return ts.forEachChild(ctx.sourceFile, function cb(node: ts.Node): void {
65+
if (isJsxOpeningElement(node) || isJsxSelfClosingElement(node)) {
66+
const typeName = node.tagName.getText();
67+
for (const ban of ctx.options) {
68+
if (ban.pattern.test(typeName)) {
69+
ctx.addFailureAtNode(node, Rule.FAILURE_STRING_FACTORY(typeName, ban.message));
70+
break;
71+
}
72+
}
73+
}
74+
return ts.forEachChild(node, cb);
75+
});
76+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<span className="primary" foo="bar">baz</span>
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [JSX element 'span' is banned. Use div instead.]
3+
4+
<span bar={5}/>
5+
~~~~~~~~~~~~~~~ [JSX element 'span' is banned. Use div instead.]
6+
7+
<button type="button">bar</button>
8+
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"rules": {
3+
"jsx-ban-elements": [true,
4+
["span", "Use div instead."]
5+
]
6+
}
7+
}

0 commit comments

Comments
 (0)