|
| 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 | +} |
0 commit comments