Skip to content

Commit

Permalink
Update: requireStringLiterals option for valid-typeof (fixes #6698)…
Browse files Browse the repository at this point in the history
… (#6923)
  • Loading branch information
not-an-aardvark authored and btmills committed Aug 21, 2016
1 parent 8561389 commit cf2242c
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 4 deletions.
26 changes: 26 additions & 0 deletions docs/rules/valid-typeof.md
Expand Up @@ -6,6 +6,12 @@ For a vast majority of use cases, the result of the `typeof` operator is one of

This rule enforces comparing `typeof` expressions to valid string literals.

## Options

This rule has an object option:

* `"requireStringLiterals": true` requires `typeof` expressions to only be compared to string literals, and disallows comparisons to any other value.

Examples of **incorrect** code for this rule:

```js
Expand All @@ -28,6 +34,26 @@ typeof foo === baz
typeof bar === typeof qux
```

Examples of **incorrect** code with the `{ "requireStringLiterals": true }` option:

```js
typeof foo === undefined
typeof bar == Object
typeof baz === "strnig"
typeof qux === "some invalid type"
typeof baz === anotherVariable
typeof foo == 5
typeof bar === typeof qux
```

Examples of **correct** code with the `{ "requireStringLiterals": true }` option:

```js
typeof foo === "undefined"
typeof bar == "object"
typeof baz === "string"
```

## When Not To Use It

You may want to turn this rule off if you will be using the `typeof` operator on host objects.
20 changes: 17 additions & 3 deletions lib/rules/valid-typeof.js
Expand Up @@ -16,7 +16,17 @@ module.exports = {
recommended: true
},

schema: []
schema: [
{
type: "object",
properties: {
requireStringLiterals: {
type: "boolean"
}
},
additionalProperties: false
}
]
},

create(context) {
Expand All @@ -37,8 +47,12 @@ module.exports = {
if (parent.type === "BinaryExpression" && OPERATORS.indexOf(parent.operator) !== -1) {
const sibling = parent.left === node ? parent.right : parent.left;

if (sibling.type === "Literal" && VALID_TYPES.indexOf(sibling.value) === -1) {
context.report(sibling, "Invalid typeof comparison value.");
if (sibling.type === "Literal") {
if (VALID_TYPES.indexOf(sibling.value) === -1) {
context.report(sibling, "Invalid typeof comparison value.");
}
} else if (context.options[0] && context.options[0].requireStringLiterals) {
context.report(sibling, "Typeof comparisons should be to string literals.");
}
}
}
Expand Down
39 changes: 38 additions & 1 deletion tests/lib/rules/valid-typeof.js
Expand Up @@ -43,7 +43,19 @@ ruleTester.run("valid-typeof", rule, {
"typeof(foo) !== 'string'",
"typeof(foo) == 'string'",
"typeof(foo) != 'string'",
"var oddUse = typeof foo + 'thing'"
"var oddUse = typeof foo + 'thing'",
{
code: "typeof foo === 'number'",
options: [{ requireStringLiterals: true }],
},
{
code: "typeof foo === \"number\"",
options: [{ requireStringLiterals: true }]
},
{
code: "var baz = typeof foo + 'thing'",
options: [{ requireStringLiterals: true }]
}
],

invalid: [
Expand Down Expand Up @@ -94,6 +106,31 @@ ruleTester.run("valid-typeof", rule, {
{
code: "if (typeof bar == 'umdefined') {}",
errors: [{ message: "Invalid typeof comparison value.", type: "Literal" }]
},
{
code: "typeof foo == 'invalid string'",
options: [{ requireStringLiterals: true }],
errors: [{ message: "Invalid typeof comparison value.", type: "Literal" }]
},
{
code: "typeof foo == Object",
options: [{ requireStringLiterals: true }],
errors: [{ message: "Typeof comparisons should be to string literals.", type: "Identifier" }]
},
{
code: "typeof foo === undefined",
options: [{ requireStringLiterals: true }],
errors: [{ message: "Typeof comparisons should be to string literals.", type: "Identifier" }]
},
{
code: "undefined === typeof foo",
options: [{ requireStringLiterals: true }],
errors: [{ message: "Typeof comparisons should be to string literals.", type: "Identifier" }]
},
{
code: "undefined == typeof foo",
options: [{ requireStringLiterals: true }],
errors: [{ message: "Typeof comparisons should be to string literals.", type: "Identifier" }]
}
]
});

0 comments on commit cf2242c

Please sign in to comment.