Skip to content

Commit df6e281

Browse files
SulimanAbdulrazzaqbrettz9
authored andcommitted
fix(prefer-import-tag): only use @typedef names as aliases for import() types
1 parent ebe5186 commit df6e281

3 files changed

Lines changed: 111 additions & 1 deletion

File tree

‎docs/rules/prefer-import-tag.md‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,33 @@ let linter;
356356
/** @type {import('eslint').Linter} */\r
357357
let linter;
358358
// Message: Inline `import()` found; prefer `@import`
359+
360+
/**
361+
* @param {import('eslint').Linter} linter
362+
*/
363+
function quux (linter) {}
364+
365+
/** @type {import('eslint').Linter} */
366+
let x;
367+
// Message: Inline `import()` found; prefer `@import`
368+
369+
/**
370+
* @typedef {object} Options
371+
* @property {import('./config.js').Config} config
372+
*/
373+
374+
/**
375+
* @returns {import('./config.js').Config}
376+
*/
377+
function load () {}
378+
// Message: Inline `import()` found; prefer `@import`
379+
380+
/** @const {import('eslint').Linter} LINTER */
381+
const LINTER = getLinter();
382+
383+
/** @type {import('eslint').Linter} */
384+
let x;
385+
// Message: Inline `import()` found; prefer `@import`
359386
````
360387

361388

‎src/rules/preferImportTag.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,14 @@ export default iterateJsdoc(({
4848
);
4949
});
5050

51+
// Only `@typedef` names are type aliases; names of `@param`,
52+
// `@property`, `@const`, etc. are not types
5153
const typedefs = comments
5254
.flatMap((doc) => {
5355
return doc.tags.filter(({
5456
tag,
5557
}) => {
56-
return utils.isNameOrNamepathDefiningTag(tag);
58+
return tag === 'typedef';
5759
});
5860
});
5961

‎test/rules/assertions/preferImportTag.js‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,6 +1058,87 @@ let linter;
10581058
],
10591059
output: '#!/usr/bin/env node\r\n/** @import * as eslint from \'eslint\'; */\r\n/** @type {eslint.Linter} */\r\nlet linter;\r\n',
10601060
},
1061+
{
1062+
code: `
1063+
/**
1064+
* @param {import('eslint').Linter} linter
1065+
*/
1066+
function quux (linter) {}
1067+
1068+
/** @type {import('eslint').Linter} */
1069+
let x;
1070+
`,
1071+
errors: [
1072+
{
1073+
line: 7,
1074+
message: 'Inline `import()` found; prefer `@import`',
1075+
},
1076+
],
1077+
output: `
1078+
/** @import * as eslint from 'eslint'; */
1079+
/**
1080+
* @param {import('eslint').Linter} linter
1081+
*/
1082+
function quux (linter) {}
1083+
1084+
/** @type {eslint.Linter} */
1085+
let x;
1086+
`,
1087+
},
1088+
{
1089+
code: `
1090+
/**
1091+
* @typedef {object} Options
1092+
* @property {import('./config.js').Config} config
1093+
*/
1094+
1095+
/**
1096+
* @returns {import('./config.js').Config}
1097+
*/
1098+
function load () {}
1099+
`,
1100+
errors: [
1101+
{
1102+
line: 8,
1103+
message: 'Inline `import()` found; prefer `@import`',
1104+
},
1105+
],
1106+
output: `
1107+
/** @import * as $k$$l$config$k$js from './config.js'; */
1108+
/**
1109+
* @typedef {object} Options
1110+
* @property {import('./config.js').Config} config
1111+
*/
1112+
1113+
/**
1114+
* @returns {$k$$l$config$k$js.Config}
1115+
*/
1116+
function load () {}
1117+
`,
1118+
},
1119+
{
1120+
code: `
1121+
/** @const {import('eslint').Linter} LINTER */
1122+
const LINTER = getLinter();
1123+
1124+
/** @type {import('eslint').Linter} */
1125+
let x;
1126+
`,
1127+
errors: [
1128+
{
1129+
line: 5,
1130+
message: 'Inline `import()` found; prefer `@import`',
1131+
},
1132+
],
1133+
output: `
1134+
/** @import * as eslint from 'eslint'; */
1135+
/** @const {import('eslint').Linter} LINTER */
1136+
const LINTER = getLinter();
1137+
1138+
/** @type {eslint.Linter} */
1139+
let x;
1140+
`,
1141+
},
10611142
],
10621143
valid: [
10631144
{

0 commit comments

Comments
 (0)