Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

add jsx-ignore option to no-magic-numbers rule #4460

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
21 changes: 16 additions & 5 deletions src/rules/noMagicNumbersRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import { isCallExpression, isIdentifier } from "tsutils";
import * as Lint from "../index";
import { isNegativeNumberLiteral } from "../language/utils";

const IGNORE_JSX_OPTION = "ignore-jsx";

export class Rule extends Lint.Rules.AbstractRule {
/* tslint:disable:object-literal-sort-keys */
public static metadata: Lint.IRuleMetadata = {
Expand All @@ -36,11 +38,19 @@ export class Rule extends Lint.Rules.AbstractRule {
options: {
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
type: "array",
items: {
type: "number",
anyOf: [
{
type: "number",
minLength: 1,
},
{
type: "string",
options: [IGNORE_JSX_OPTION],
},
],
},
minLength: 1,
},
optionExamples: [true, [true, 1, 2, 3]],
optionExamples: [true, [true, 1, 2, 3], [true, "ignore-jsx"]],
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
type: "typescript",
typescriptOnly: false,
};
Expand Down Expand Up @@ -76,7 +86,7 @@ export class Rule extends Lint.Rules.AbstractRule {
}
}

class NoMagicNumbersWalker extends Lint.AbstractWalker<number[]> {
class NoMagicNumbersWalker extends Lint.AbstractWalker<Array<number | string>> {
public walk(sourceFile: ts.SourceFile) {
const cb = (node: ts.Node): void => {
if (
Expand Down Expand Up @@ -105,7 +115,8 @@ class NoMagicNumbersWalker extends Lint.AbstractWalker<number[]> {
/* Using Object.is() to differentiate between pos/neg zero */
if (
!Rule.ALLOWED_NODES.has(node.parent.kind) &&
!this.options.some(allowedNum => Object.is(allowedNum, parseFloat(num)))
!this.options.some(allowedNum => Object.is(allowedNum, parseFloat(num))) &&
this.options.indexOf(IGNORE_JSX_OPTION) === -1
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
) {
this.addFailureAtNode(node, Rule.FAILURE_STRING(num));
}
Expand Down
8 changes: 8 additions & 0 deletions test/rules/no-magic-numbers/default/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,11 @@ class A {
enum Test {
key = 1337,
}

import * as React from "react";
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
class App extends React.Component {
render() {
return <Component width={200} height={200} />;
~~~ ['magic numbers' are not allowed: 200]
}
}
6 changes: 6 additions & 0 deletions test/rules/no-magic-numbers/ignore-jsx/test.ts.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import * as React from "react";
class App extends React.Component {
render() {
return <Component width={200} height={200} />;
}
}
5 changes: 5 additions & 0 deletions test/rules/no-magic-numbers/ignore-jsx/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"no-magic-numbers": [true, "ignore-jsx"]
}
}