Skip to content

Commit

Permalink
Revert "feat: valid-typeof always ban undefined (eslint#15635)"
Browse files Browse the repository at this point in the history
This reverts commit 8d9d0f7.
  • Loading branch information
srijan-deepsource committed May 30, 2022
1 parent 4196380 commit 121dcb8
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 89 deletions.
38 changes: 1 addition & 37 deletions lib/rules/valid-typeof.js
Expand Up @@ -19,8 +19,6 @@ module.exports = {
url: "https://eslint.org/docs/rules/valid-typeof"
},

hasSuggestions: true,

schema: [
{
type: "object",
Expand All @@ -35,8 +33,7 @@ module.exports = {
],
messages: {
invalidValue: "Invalid typeof comparison value.",
notString: "Typeof comparisons should be to string literals.",
suggestString: 'Use `"{{type}}"` instead of `{{type}}`.'
notString: "Typeof comparisons should be to string literals."
}
},

Expand All @@ -47,21 +44,6 @@ module.exports = {

const requireStringLiterals = context.options[0] && context.options[0].requireStringLiterals;

let globalScope;

/**
* Checks whether the given node represents a reference to a global variable that is not declared in the source code.
* These identifiers will be allowed, as it is assumed that user has no control over the names of external global variables.
* @param {ASTNode} node `Identifier` node to check.
* @returns {boolean} `true` if the node is a reference to a global variable.
*/
function isReferenceToGlobalVariable(node) {
const variable = globalScope.set.get(node.name);

return variable && variable.defs.length === 0 &&
variable.references.some(ref => ref.identifier === node);
}

/**
* Determines whether a node is a typeof expression.
* @param {ASTNode} node The node
Expand All @@ -77,10 +59,6 @@ module.exports = {

return {

Program() {
globalScope = context.getScope();
},

UnaryExpression(node) {
if (isTypeofExpression(node)) {
const parent = context.getAncestors().pop();
Expand All @@ -94,20 +72,6 @@ module.exports = {
if (VALID_TYPES.indexOf(value) === -1) {
context.report({ node: sibling, messageId: "invalidValue" });
}
} else if (sibling.type === "Identifier" && sibling.name === "undefined" && isReferenceToGlobalVariable(sibling)) {
context.report({
node: sibling,
messageId: requireStringLiterals ? "notString" : "invalidValue",
suggest: [
{
messageId: "suggestString",
data: { type: "undefined" },
fix(fixer) {
return fixer.replaceText(sibling, '"undefined"');
}
}
]
});
} else if (requireStringLiterals && !isTypeofExpression(sibling)) {
context.report({ node: sibling, messageId: "notString" });
}
Expand Down
55 changes: 3 additions & 52 deletions tests/lib/rules/valid-typeof.js
Expand Up @@ -45,7 +45,6 @@ ruleTester.run("valid-typeof", rule, {
"typeof(foo) == 'string'",
"typeof(foo) != 'string'",
"var oddUse = typeof foo + 'thing'",
"function f(undefined) { typeof x === undefined }",
{
code: "typeof foo === 'number'",
options: [{ requireStringLiterals: true }]
Expand Down Expand Up @@ -137,21 +136,6 @@ ruleTester.run("valid-typeof", rule, {
options: [{ requireStringLiterals: true }],
errors: [{ messageId: "invalidValue", type: "Literal" }]
},
{
code: "if (typeof bar !== undefined) {}",
errors: [
{
messageId: "invalidValue",
type: "Identifier",
suggestions: [
{
messageId: "suggestString",
data: { type: "undefined" },
output: 'if (typeof bar !== "undefined") {}'
}
]
}]
},
{
code: "typeof foo == Object",
options: [{ requireStringLiterals: true }],
Expand All @@ -160,50 +144,17 @@ ruleTester.run("valid-typeof", rule, {
{
code: "typeof foo === undefined",
options: [{ requireStringLiterals: true }],
errors: [
{
messageId: "notString",
type: "Identifier",
suggestions: [
{
messageId: "suggestString",
data: { type: "undefined" },
output: 'typeof foo === "undefined"'
}
]
}]
errors: [{ messageId: "notString", type: "Identifier" }]
},
{
code: "undefined === typeof foo",
options: [{ requireStringLiterals: true }],
errors: [
{
messageId: "notString",
type: "Identifier",
suggestions: [
{
messageId: "suggestString",
data: { type: "undefined" },
output: '"undefined" === typeof foo'
}
]
}]
errors: [{ messageId: "notString", type: "Identifier" }]
},
{
code: "undefined == typeof foo",
options: [{ requireStringLiterals: true }],
errors: [
{
messageId: "notString",
type: "Identifier",
suggestions: [
{
messageId: "suggestString",
data: { type: "undefined" },
output: '"undefined" == typeof foo'
}
]
}]
errors: [{ messageId: "notString", type: "Identifier" }]
},
{
code: "typeof foo === `undefined${foo}`",
Expand Down

0 comments on commit 121dcb8

Please sign in to comment.