Skip to content

Commit 2744339

Browse files
9romiseFloEdelmann
andauthored
refactor: migrate utils/comments and related rules to TypeScript (#3045)
Co-authored-by: Flo Edelmann <git@flo-edelmann.de>
1 parent 4f1c6b5 commit 2744339

File tree

7 files changed

+106
-163
lines changed

7 files changed

+106
-163
lines changed

docs/rules/no-unused-properties.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,5 +289,5 @@ This rule was introduced in eslint-plugin-vue v7.0.0
289289

290290
## :mag: Implementation
291291

292-
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/no-unused-properties.js)
292+
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/no-unused-properties.ts)
293293
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/no-unused-properties.test.ts)

docs/rules/require-prop-comment.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,5 +144,5 @@ This rule was introduced in eslint-plugin-vue v9.8.0
144144

145145
## :mag: Implementation
146146

147-
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/require-prop-comment.js)
147+
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/require-prop-comment.ts)
148148
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/require-prop-comment.test.ts)

lib/plugin.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ import noUndefProperties from './rules/no-undef-properties.js'
154154
import noUnsupportedFeatures from './rules/no-unsupported-features.js'
155155
import noUnusedComponents from './rules/no-unused-components.js'
156156
import noUnusedEmitDeclarations from './rules/no-unused-emit-declarations.js'
157-
import noUnusedProperties from './rules/no-unused-properties.js'
157+
import noUnusedProperties from './rules/no-unused-properties.ts'
158158
import noUnusedRefs from './rules/no-unused-refs.js'
159159
import noUnusedVars from './rules/no-unused-vars.js'
160160
import noUseComputedPropertyLikeMethod from './rules/no-use-computed-property-like-method.js'
@@ -200,7 +200,7 @@ import requireExplicitSlots from './rules/require-explicit-slots.js'
200200
import requireExpose from './rules/require-expose.js'
201201
import requireMacroVariableName from './rules/require-macro-variable-name.js'
202202
import requireNameProperty from './rules/require-name-property.js'
203-
import requirePropComment from './rules/require-prop-comment.js'
203+
import requirePropComment from './rules/require-prop-comment.ts'
204204
import requirePropTypeConstructor from './rules/require-prop-type-constructor.js'
205205
import requirePropTypes from './rules/require-prop-types.js'
206206
import requireRenderReturn from './rules/require-render-return.js'
Lines changed: 76 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,43 @@
22
* @fileoverview Disallow unused properties, data and computed properties.
33
* @author Learning Equality
44
*/
5-
'use strict'
6-
7-
const utils = require('../utils')
8-
const eslintUtils = require('@eslint-community/eslint-utils')
9-
const { isJSDocComment } = require('../utils/comments.js')
10-
const { getStyleVariablesContext } = require('../utils/style-variables')
11-
const {
5+
import type { GroupName, VueObjectData } from '../utils/index.js'
6+
import type { IPropertyReferences } from '../utils/property-references.js'
7+
import utils from '../utils/index.js'
8+
import eslintUtils from '@eslint-community/eslint-utils'
9+
import { isJSDocComment } from '../utils/comments.ts'
10+
import { getStyleVariablesContext } from '../utils/style-variables/index.js'
11+
import {
1212
definePropertyReferenceExtractor,
1313
mergePropertyReferences
14-
} = require('../utils/property-references')
15-
16-
/**
17-
* @typedef {import('../utils').GroupName} GroupName
18-
* @typedef {import('../utils').VueObjectData} VueObjectData
19-
* @typedef {import('../utils/property-references').IPropertyReferences} IPropertyReferences
20-
*/
21-
22-
/**
23-
* @typedef {object} ComponentObjectPropertyData
24-
* @property {string} name
25-
* @property {GroupName} groupName
26-
* @property {'object'} type
27-
* @property {ASTNode} node
28-
* @property {Property} property
29-
*
30-
* @typedef {object} ComponentNonObjectPropertyData
31-
* @property {string} name
32-
* @property {GroupName} groupName
33-
* @property {'array' | 'type' | 'infer-type' | 'model' } type
34-
* @property {ASTNode} node
35-
*
36-
* @typedef { ComponentNonObjectPropertyData | ComponentObjectPropertyData } ComponentPropertyData
37-
*/
14+
} from '../utils/property-references.js'
15+
16+
interface ComponentObjectPropertyData {
17+
name: string
18+
groupName: GroupName
19+
type: 'object'
20+
node: ASTNode
21+
property: Property
22+
}
23+
interface ComponentNonObjectPropertyData {
24+
name: string
25+
groupName: GroupName
26+
type: 'array' | 'type' | 'infer-type' | 'model'
27+
node: ASTNode
28+
}
29+
type ComponentPropertyData =
30+
| ComponentNonObjectPropertyData
31+
| ComponentObjectPropertyData
3832

39-
/**
40-
* @typedef {object} TemplatePropertiesContainer
41-
* @property {IPropertyReferences[]} propertyReferences
42-
* @property {Set<string>} refNames
43-
* @typedef {object} VueComponentPropertiesContainer
44-
* @property {ComponentPropertyData[]} properties
45-
* @property {IPropertyReferences[]} propertyReferences
46-
* @property {IPropertyReferences[]} propertyReferencesForProps
47-
*/
33+
interface TemplatePropertiesContainer {
34+
propertyReferences: IPropertyReferences[]
35+
refNames: Set<string>
36+
}
37+
interface VueComponentPropertiesContainer {
38+
properties: ComponentPropertyData[]
39+
propertyReferences: IPropertyReferences[]
40+
propertyReferencesForProps: IPropertyReferences[]
41+
}
4842

4943
const GROUP_PROPERTY = 'props'
5044
const GROUP_DATA = 'data'
@@ -73,12 +67,7 @@ const PROPERTY_LABEL = {
7367
expose: 'expose'
7468
}
7569

76-
/**
77-
* @param {RuleContext} context
78-
* @param {Identifier} id
79-
* @returns {Expression}
80-
*/
81-
function findExpression(context, id) {
70+
function findExpression(context: RuleContext, id: Identifier): Expression {
8271
const variable = utils.findVariableByIdentifier(context, id)
8372
if (!variable) {
8473
return id
@@ -101,10 +90,11 @@ function findExpression(context, id) {
10190

10291
/**
10392
* Check if the given component property is marked as `@public` in JSDoc comments.
104-
* @param {ComponentPropertyData} property
105-
* @param {SourceCode} sourceCode
10693
*/
107-
function isPublicMember(property, sourceCode) {
94+
function isPublicMember(
95+
property: ComponentPropertyData,
96+
sourceCode: SourceCode
97+
) {
10898
if (
10999
property.type === 'object' &&
110100
// Props do not support @public.
@@ -117,10 +107,8 @@ function isPublicMember(property, sourceCode) {
117107

118108
/**
119109
* Check if the given property node is marked as `@public` in JSDoc comments.
120-
* @param {Property} node
121-
* @param {SourceCode} sourceCode
122110
*/
123-
function isPublicProperty(node, sourceCode) {
111+
function isPublicProperty(node: Property, sourceCode: SourceCode) {
124112
const jsdoc = getJSDocFromProperty(node, sourceCode)
125113
if (jsdoc) {
126114
return /(?:^|\s|\*)@public\b/u.test(jsdoc.value)
@@ -130,10 +118,8 @@ function isPublicProperty(node, sourceCode) {
130118

131119
/**
132120
* Get the JSDoc comment for a given property node.
133-
* @param {Property} node
134-
* @param {SourceCode} sourceCode
135121
*/
136-
function getJSDocFromProperty(node, sourceCode) {
122+
function getJSDocFromProperty(node: Property, sourceCode: SourceCode) {
137123
const jsdoc = findJSDocComment(node, sourceCode)
138124
if (jsdoc) {
139125
return jsdoc
@@ -150,13 +136,12 @@ function getJSDocFromProperty(node, sourceCode) {
150136

151137
/**
152138
* Finds a JSDoc comment for the given node.
153-
* @param {ASTNode} node
154-
* @param {SourceCode} sourceCode
155-
* @returns {Comment | null}
156139
*/
157-
function findJSDocComment(node, sourceCode) {
158-
/** @type {ASTNode | Token} */
159-
let currentNode = node
140+
function findJSDocComment(
141+
node: ASTNode,
142+
sourceCode: SourceCode
143+
): Comment | null {
144+
let currentNode: ASTNode | Token = node
160145
let tokenBefore = null
161146

162147
while (currentNode) {
@@ -180,7 +165,7 @@ function findJSDocComment(node, sourceCode) {
180165
return null
181166
}
182167

183-
module.exports = {
168+
export default {
184169
meta: {
185170
type: 'suggestion',
186171
docs: {
@@ -226,15 +211,13 @@ module.exports = {
226211
unused: "'{{name}}' of {{group}} found, but never used."
227212
}
228213
},
229-
/** @param {RuleContext} context */
230-
create(context) {
214+
create(context: RuleContext) {
231215
const options = context.options[0] || {}
232-
const groups = new Set(options.groups || [GROUP_PROPERTY])
216+
const groups = new Set<GroupName>(options.groups || [GROUP_PROPERTY])
233217
const deepData = Boolean(options.deepData)
234218
const ignorePublicMembers = Boolean(options.ignorePublicMembers)
235219
const unreferencedOptions = new Set(options.unreferencedOptions || [])
236-
/** @type {null | Pattern} */
237-
let propsReferencePattern = null
220+
let propsReferencePattern: null | Pattern = null
238221

239222
const propertyReferenceExtractor = definePropertyReferenceExtractor(
240223
context,
@@ -246,19 +229,18 @@ module.exports = {
246229
}
247230
)
248231

249-
/** @type {TemplatePropertiesContainer} */
250-
const templatePropertiesContainer = {
232+
const templatePropertiesContainer: TemplatePropertiesContainer = {
251233
propertyReferences: [],
252234
refNames: new Set()
253235
}
254-
/** @type {Map<ASTNode, VueComponentPropertiesContainer>} */
255-
const vueComponentPropertiesContainerMap = new Map()
256-
257-
/**
258-
* @param {ASTNode} node
259-
* @returns {VueComponentPropertiesContainer}
260-
*/
261-
function getVueComponentPropertiesContainer(node) {
236+
const vueComponentPropertiesContainerMap = new Map<
237+
ASTNode,
238+
VueComponentPropertiesContainer
239+
>()
240+
241+
function getVueComponentPropertiesContainer(
242+
node: ASTNode
243+
): VueComponentPropertiesContainer {
262244
let container = vueComponentPropertiesContainerMap.get(node)
263245
if (!container) {
264246
container = {
@@ -271,15 +253,10 @@ module.exports = {
271253
return container
272254
}
273255

274-
/**
275-
* @param {string[]} segments
276-
* @param {Expression} propertyValue
277-
* @param {IPropertyReferences} propertyReferences
278-
*/
279256
function verifyDataOptionDeepProperties(
280-
segments,
281-
propertyValue,
282-
propertyReferences
257+
segments: string[],
258+
propertyValue: Expression,
259+
propertyReferences: IPropertyReferences
283260
) {
284261
let targetExpr = propertyValue
285262
if (targetExpr.type === 'Identifier') {
@@ -385,11 +362,7 @@ module.exports = {
385362
}
386363
}
387364

388-
/**
389-
* @param {Expression} node
390-
* @returns {Property|null}
391-
*/
392-
function getParentProperty(node) {
365+
function getParentProperty(node: Expression): Property | null {
393366
if (
394367
!node.parent ||
395368
node.parent.type !== 'Property' ||
@@ -490,14 +463,9 @@ module.exports = {
490463
}
491464
}),
492465
utils.defineVueVisitor(context, {
493-
/**
494-
* @param {CallExpression} node
495-
* @param {VueObjectData} vueData
496-
*/
497-
CallExpression(node, vueData) {
466+
CallExpression(node: CallExpression, vueData: VueObjectData) {
498467
if (node.callee.type !== 'Identifier') return
499-
/** @type {'methods'|'computed'|null} */
500-
let groupName = null
468+
let groupName: 'methods' | 'computed' | null = null
501469
if (/^mapMutations|mapActions$/u.test(node.callee.name)) {
502470
groupName = 'methods'
503471
} else if (
@@ -599,10 +567,11 @@ module.exports = {
599567
container.properties.push(...utils.iterateProperties(node, groups))
600568
},
601569

602-
/** @param { (FunctionExpression | ArrowFunctionExpression) & { parent: Property }} node */
603570
'ObjectExpression > Property > :function[params.length>0]'(
604-
node,
605-
vueData
571+
node: (FunctionExpression | ArrowFunctionExpression) & {
572+
parent: Property
573+
},
574+
vueData: VueObjectData
606575
) {
607576
const property = getParentProperty(node)
608577
if (!property) {
@@ -674,11 +643,10 @@ module.exports = {
674643
)
675644
}
676645
},
677-
/**
678-
* @param {ThisExpression | Identifier} node
679-
* @param {VueObjectData} vueData
680-
*/
681-
'ThisExpression, Identifier'(node, vueData) {
646+
'ThisExpression, Identifier'(
647+
node: ThisExpression | Identifier,
648+
vueData: VueObjectData
649+
) {
682650
if (!utils.isThis(node, context)) {
683651
return
684652
}
@@ -699,8 +667,7 @@ module.exports = {
699667
)
700668
}
701669
},
702-
/** @param {Program} node */
703-
'Program:exit'(node) {
670+
'Program:exit'(node: Program) {
704671
if (!node.templateBody) {
705672
reportUnusedProperties()
706673
}
@@ -709,10 +676,7 @@ module.exports = {
709676
)
710677

711678
const templateVisitor = {
712-
/**
713-
* @param {VExpressionContainer} node
714-
*/
715-
VExpressionContainer(node) {
679+
VExpressionContainer(node: VExpressionContainer) {
716680
const property =
717681
propertyReferenceExtractor.extractFromVExpressionContainer(node)
718682

@@ -734,10 +698,7 @@ module.exports = {
734698
}
735699
}
736700
},
737-
/**
738-
* @param {VAttribute} node
739-
*/
740-
'VAttribute[directive=false]'(node) {
701+
'VAttribute[directive=false]'(node: VAttribute) {
741702
if (node.key.name === 'ref' && node.value != null) {
742703
templatePropertiesContainer.refNames.add(node.value.value)
743704
}

0 commit comments

Comments
 (0)