Skip to content

Commit e554e55

Browse files
feat: scope names
1 parent 917864a commit e554e55

File tree

3 files changed

+84
-5
lines changed

3 files changed

+84
-5
lines changed

lib/rules/no-sync.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const typeMatchesSpecifier =
1010
)
1111
const getTypeOfNode = require("../util/get-type-of-node")
1212
const getParserServices = require("../util/get-parser-services")
13+
const getNodeScopedName = require("../util/get-node-scoped-name")
1314

1415
const selectors = [
1516
// fs.readFileSync()
@@ -115,6 +116,8 @@ module.exports = {
115116
[selector.join(",")](node) {
116117
const parserServices = getParserServices(context)
117118
let type = undefined
119+
const nodeName =
120+
getNodeScopedName(node, parserServices) ?? node.name
118121

119122
for (const ignore of ignores) {
120123
if (typeof ignore === "string") {
@@ -142,7 +145,7 @@ module.exports = {
142145
type
143146
) &&
144147
(ignore.name === undefined ||
145-
ignore.name.includes(node.name))
148+
ignore.name.includes(nodeName))
146149
) {
147150
return
148151
}
@@ -152,7 +155,7 @@ module.exports = {
152155
node: node.parent,
153156
messageId: "noSync",
154157
data: {
155-
propertyName: node.name,
158+
propertyName: nodeName,
156159
},
157160
})
158161
},

lib/util/get-node-scoped-name.js

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
"use strict"
2+
3+
const ts = (() => {
4+
try {
5+
// eslint-disable-next-line n/no-unpublished-require
6+
return require("typescript")
7+
} catch {
8+
return null
9+
}
10+
})()
11+
12+
const getTypeOfNode = require("./get-type-of-node")
13+
14+
/**
15+
* @param {import('estree').Node & {parent?: import('estree').Node}} node
16+
* @param {import('@typescript-eslint/parser').ParserServices | null} parserServices
17+
* @returns {string | null}
18+
*/
19+
module.exports = function getNodeScopedName(node, parserServices) {
20+
if (ts === null || parserServices === null) {
21+
return null
22+
}
23+
24+
if (parserServices !== null) {
25+
if (node.parent?.type === "MemberExpression") {
26+
// Find the root node of the expression
27+
return getNodeScopedName(node.parent, parserServices)
28+
}
29+
}
30+
31+
return getNodeScopedNameHelper(node, parserServices)
32+
}
33+
34+
/**
35+
* @param {import('estree').Node & {parent?: import('estree').Node}} node
36+
* @param {import('@typescript-eslint/parser').ParserServices} parserServices
37+
* @returns {string | null}
38+
*/
39+
function getNodeScopedNameHelper(node, parserServices) {
40+
if (node.type === "MemberExpression") {
41+
const objectType = getTypeOfNode(node.object, parserServices)
42+
const tsNode = objectType?.symbol.valueDeclaration
43+
44+
if (tsNode === undefined) {
45+
return null
46+
}
47+
48+
// TODO: handle more cases
49+
const scopeName =
50+
tsNode.kind ===
51+
/** @type {import('typescript')} */ (ts).SyntaxKind
52+
.VariableDeclaration
53+
? /** @type {import('typescript').VariableDeclaration} */ (
54+
tsNode
55+
).name.getText()
56+
: null
57+
58+
if (scopeName === null) {
59+
return null
60+
}
61+
62+
const localName = getNodeScopedNameHelper(node.property, parserServices)
63+
64+
if (localName === null) {
65+
return null
66+
}
67+
68+
return `${scopeName}.${localName}`
69+
}
70+
71+
if (node.type === "Identifier") {
72+
return node.name
73+
}
74+
75+
return null
76+
}

tests/lib/rules/no-sync.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ stylesheet.replaceSync("body { font-size: 1.4em; } p { color: red; }");
193193
ignores: [
194194
{
195195
from: "lib",
196-
name: ["replaceSync"],
196+
name: ["CSSStyleSheet.replaceSync"],
197197
},
198198
],
199199
},
@@ -257,15 +257,15 @@ stylesheet.replaceSync("body { font-size: 1.4em; } p { color: red; }");
257257
ignores: [
258258
{
259259
from: "file",
260-
name: ["replaceSync"],
260+
name: ["CSSStyleSheet.replaceSync"],
261261
},
262262
],
263263
},
264264
],
265265
errors: [
266266
{
267267
messageId: "noSync",
268-
data: { propertyName: "replaceSync" },
268+
data: { propertyName: "CSSStyleSheet.replaceSync" },
269269
type: "MemberExpression",
270270
},
271271
],

0 commit comments

Comments
 (0)