-
Notifications
You must be signed in to change notification settings - Fork 76
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
Added new jsx-ban-elements rule to ban jsx elements #137
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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.`, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: "JSX", not "jsx" |
||
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}$`)}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we surround the configured pattern with |
||
} | ||
|
||
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, Rule.FAILURE_STRING_FACTORY(typeName, ban.message)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we highlight the failure on just the tag name rather than the whole tag expression? that way, if there are other lint errors in the tag expression, they're less likely to overlap, which makes it easier to read in your editor. |
||
break; | ||
} | ||
} | ||
} | ||
return ts.forEachChild(node, cb); | ||
}); | ||
} |
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> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"rules": { | ||
"jsx-ban-elements": [true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use the newer options syntax please.
|
||
["span", "Use div instead."] | ||
] | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please don't bump the version in this commit. that will be done during the release process.