Skip to content

Commit 943176a

Browse files
committed
refactor from pr comments
1 parent 5ea4aec commit 943176a

File tree

1 file changed

+30
-31
lines changed

1 file changed

+30
-31
lines changed

src/rules/use-client.ts

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ const browserOnlyGlobals = Object.keys(globals.browser).reduce<
2727
return acc;
2828
}, new Set());
2929

30-
const validGlobalsForServerChecks = new Set(["document", "window"]);
31-
3230
type Options = [
3331
{
3432
allowedServerHooks?: string[];
@@ -116,34 +114,6 @@ const create = Components.detect(
116114
});
117115
}
118116

119-
function findFirstParentOfType(
120-
node: Rule.Node,
121-
type: string
122-
): Rule.Node | null {
123-
let currentNode: Rule.Node | null = node;
124-
125-
while (currentNode) {
126-
if (currentNode.type === type) {
127-
return currentNode;
128-
}
129-
currentNode = currentNode?.parent;
130-
}
131-
132-
return null;
133-
}
134-
135-
function isNodeInTree(node: Rule.Node, target: Rule.Node): boolean {
136-
let currentNode: Rule.Node | null = node;
137-
138-
while (currentNode) {
139-
if (currentNode === target) {
140-
return true;
141-
}
142-
currentNode = currentNode.parent;
143-
}
144-
145-
return false;
146-
}
147117

148118
function getBinaryBranchExecutedOnServer(node: BinaryExpression): {
149119
isGlobalClientPropertyCheck: boolean;
@@ -153,7 +123,7 @@ const create = Components.detect(
153123
node.left?.type === "UnaryExpression" &&
154124
node.left.operator === "typeof" &&
155125
node.left.argument?.type === "Identifier" &&
156-
validGlobalsForServerChecks.has(node.left.argument?.name) &&
126+
browserOnlyGlobals.has(node.left.argument?.name as any) &&
157127
node.right?.type === "Literal" &&
158128
node.right.value === "undefined" &&
159129
(node.operator === "===" || node.operator === "!==");
@@ -411,4 +381,33 @@ function isFunction(def: any) {
411381
return false;
412382
}
413383

384+
function findFirstParentOfType(
385+
node: Rule.Node,
386+
type: string
387+
): Rule.Node | null {
388+
let currentNode: Rule.Node | null = node;
389+
390+
while (currentNode) {
391+
if (currentNode.type === type) {
392+
return currentNode;
393+
}
394+
currentNode = currentNode?.parent;
395+
}
396+
397+
return null;
398+
}
399+
400+
function isNodeInTree(node: Rule.Node, target: Rule.Node): boolean {
401+
let currentNode: Rule.Node | null = node;
402+
403+
while (currentNode) {
404+
if (currentNode === target) {
405+
return true;
406+
}
407+
currentNode = currentNode.parent;
408+
}
409+
410+
return false;
411+
}
412+
414413
export const ClientComponents = { meta, create };

0 commit comments

Comments
 (0)