Skip to content

Commit 9bb554a

Browse files
authored
fix(no-undefined-types): mark sister method and property names as defined; fixes #1403 ; fixes #1119 (#1404)
1 parent ec387d6 commit 9bb554a

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed

docs/rules/no-undefined-types.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,5 +865,44 @@ quux();
865865
/**
866866
* @type {Linters}
867867
*/
868+
869+
class Filler {
870+
static methodOne () {
871+
return 'Method One';
872+
}
873+
874+
nonStaticMethodTwo (param) {
875+
return `Method Two received: ${param}`;
876+
}
877+
878+
/**
879+
* {@link methodOne} shouldn't report eslint error
880+
* {@link nonStaticMethodTwo} also shouldn't report eslint error
881+
* @returns {number} A number representing the answer to everything.
882+
*/
883+
static methodThree () {
884+
return 42;
885+
}
886+
887+
/**
888+
* {@link Filler.methodOne} doesn't report eslint error
889+
* {@link Filler.nonStaticMethodTwo} also doesn't report eslint error
890+
* @returns {string} A string indicating the method's purpose.
891+
*/
892+
methodFour() {
893+
return 'Method Four';
894+
}
895+
}
896+
897+
class Foo {
898+
foo = "foo";
899+
/**
900+
* Something related to {@link foo}
901+
* @returns {string} Something awesome
902+
*/
903+
bar() {
904+
return "bar";
905+
}
906+
}
868907
````
869908

src/rules/noUndefinedTypes.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,31 @@ export default iterateJsdoc(({
267267
.concat(importTags)
268268
.concat(definedTypes)
269269
.concat(/** @type {string[]} */ (definedPreferredTypes))
270+
.concat((() => {
271+
// Other methods are not in scope, but we need them, and we grab them here
272+
if (node?.type === 'MethodDefinition') {
273+
return /** @type {import('estree').ClassBody} */ (node.parent).body.map((methodOrProp) => {
274+
if (methodOrProp.type === 'MethodDefinition') {
275+
// eslint-disable-next-line unicorn/no-lonely-if -- Pattern
276+
if (methodOrProp.key.type === 'Identifier') {
277+
return methodOrProp.key.name;
278+
}
279+
}
280+
281+
if (methodOrProp.type === 'PropertyDefinition') {
282+
// eslint-disable-next-line unicorn/no-lonely-if -- Pattern
283+
if (methodOrProp.key.type === 'Identifier') {
284+
return methodOrProp.key.name;
285+
}
286+
}
287+
/* c8 ignore next 2 -- Not yet built */
288+
289+
return '';
290+
}).filter(Boolean);
291+
}
292+
293+
return [];
294+
})())
270295
.concat(...getValidRuntimeIdentifiers(node && (
271296
(sourceCode.getScope &&
272297
/* c8 ignore next 2 */

test/rules/assertions/noUndefinedTypes.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1549,5 +1549,53 @@ export default /** @type {import('../index.js').TestCases} */ ({
15491549
*/
15501550
`,
15511551
},
1552+
{
1553+
code: `
1554+
class Filler {
1555+
static methodOne () {
1556+
return 'Method One';
1557+
}
1558+
1559+
nonStaticMethodTwo (param) {
1560+
return \`Method Two received: $\{param}\`;
1561+
}
1562+
1563+
/**
1564+
* {@link methodOne} shouldn't report eslint error
1565+
* {@link nonStaticMethodTwo} also shouldn't report eslint error
1566+
* @returns {number} A number representing the answer to everything.
1567+
*/
1568+
static methodThree () {
1569+
return 42;
1570+
}
1571+
1572+
/**
1573+
* {@link Filler.methodOne} doesn't report eslint error
1574+
* {@link Filler.nonStaticMethodTwo} also doesn't report eslint error
1575+
* @returns {string} A string indicating the method's purpose.
1576+
*/
1577+
methodFour() {
1578+
return 'Method Four';
1579+
}
1580+
}
1581+
`,
1582+
},
1583+
{
1584+
code: `
1585+
class Foo {
1586+
foo = "foo";
1587+
/**
1588+
* Something related to {@link foo}
1589+
* @returns {string} Something awesome
1590+
*/
1591+
bar() {
1592+
return "bar";
1593+
}
1594+
}
1595+
`,
1596+
languageOptions: {
1597+
ecmaVersion: 2_022,
1598+
},
1599+
},
15521600
],
15531601
});

0 commit comments

Comments
 (0)