Skip to content

Commit b4fcf50

Browse files
refactor: migrate internal lint rules + docs shim to TypeScript (#3238)
1 parent c4b9b63 commit b4fcf50

9 files changed

Lines changed: 60 additions & 66 deletions

File tree

‎docs/.vitepress/build-system/build.mts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ function bundle(entryPoint: string, externals: string[]) {
6363
bundle: true,
6464
external: externals,
6565
write: false,
66-
inject: [path.join(dirname, './src/process-shim.mjs')]
66+
inject: [path.join(dirname, './src/process-shim.ts')]
6767
})
6868

6969
return result.outputFiles[0].text
File renamed without changes.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
/* globals window */
2-
// @ts-nocheck
31
export const process = {
42
env: {},
53
cwd: () => '/',
64
stdout: {}
75
}
6+
// @ts-expect-error -- Browser
87
if (typeof window !== 'undefined') {
8+
// @ts-expect-error -- Browser
99
window.process = process
1010
}

‎docs/.vitepress/config.mts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,11 @@ export default async () => {
159159
),
160160

161161
tslib: path.join(dirname, '../../node_modules/tslib/tslib.es6.js'),
162-
globby: path.join(dirname, './build-system/shim/empty.mjs'),
163-
'fast-glob': path.join(dirname, './build-system/shim/empty.mjs'),
164-
tinyglobby: path.join(dirname, './build-system/shim/empty.mjs'),
165-
module: path.join(dirname, './build-system/shim/empty.mjs'),
166-
'node:module': path.join(dirname, './build-system/shim/empty.mjs')
162+
globby: path.join(dirname, './build-system/shim/empty.ts'),
163+
'fast-glob': path.join(dirname, './build-system/shim/empty.ts'),
164+
tinyglobby: path.join(dirname, './build-system/shim/empty.ts'),
165+
module: path.join(dirname, './build-system/shim/empty.ts'),
166+
'node:module': path.join(dirname, './build-system/shim/empty.ts')
167167
}
168168
},
169169
define: {

eslint-internal-rules/no-invalid-meta-docs-categories.js renamed to eslint-internal-rules/no-invalid-meta-docs-categories.ts

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,20 @@
33
* @author Vitor Balocco
44
*/
55

6-
'use strict'
7-
86
/**
97
* Gets the property of the Object node passed in that has the name specified.
10-
*
11-
* @param {string} propertyName Name of the property to return.
12-
* @param {ASTNode} node The ObjectExpression node.
13-
* @returns {ASTNode} The Property node or null if not found.
148
*/
15-
function getPropertyFromObject(propertyName, node) {
9+
function getPropertyFromObject(
10+
propertyName: string,
11+
node: ASTNode | undefined
12+
): Property | null {
1613
if (node && node.type === 'ObjectExpression') {
1714
for (const property of node.properties) {
18-
if (property.type === 'Property' && property.key.name === propertyName) {
15+
if (
16+
property.type === 'Property' &&
17+
property.key.type === 'Identifier' &&
18+
property.key.name === propertyName
19+
) {
1920
return property
2021
}
2122
}
@@ -25,23 +26,18 @@ function getPropertyFromObject(propertyName, node) {
2526

2627
/**
2728
* Extracts the `meta` property from the ObjectExpression that all rules export.
28-
*
29-
* @param {ASTNode} exportsNode ObjectExpression node that the rule exports.
30-
* @returns {ASTNode} The `meta` Property node or null if not found.
3129
*/
32-
function getMetaPropertyFromExportsNode(exportsNode) {
30+
function getMetaPropertyFromExportsNode(exportsNode: ASTNode | undefined) {
3331
return getPropertyFromObject('meta', exportsNode)
3432
}
3533

3634
/**
3735
* Checks the validity of the meta definition of this rule and reports any errors found.
38-
*
39-
* @param {RuleContext} context The ESLint rule context.
40-
* @param {ASTNode} exportsNode ObjectExpression node that the rule exports.
41-
* @param {boolean} ruleIsFixable whether the rule is fixable or not.
42-
* @returns {void}
4336
*/
44-
function checkMetaValidity(context, exportsNode) {
37+
function checkMetaValidity(
38+
context: RuleContext,
39+
exportsNode: ASTNode | undefined
40+
) {
4541
const metaProperty = getMetaPropertyFromExportsNode(exportsNode)
4642
if (!metaProperty) {
4743
return
@@ -102,7 +98,7 @@ function checkMetaValidity(context, exportsNode) {
10298
}
10399
}
104100

105-
module.exports = {
101+
export default {
106102
meta: {
107103
type: 'problem',
108104
docs: {
@@ -120,27 +116,29 @@ module.exports = {
120116
languages: ['js/js']
121117
},
122118

123-
create(context) {
124-
let exportsNode
119+
create(context: RuleContext) {
120+
let exportsNode: ASTNode | undefined
125121

126122
return {
127-
AssignmentExpression(node) {
123+
AssignmentExpression(node: AssignmentExpression) {
128124
if (
129125
node.left &&
130126
node.right &&
131127
node.left.type === 'MemberExpression' &&
128+
node.left.object.type === 'Identifier' &&
132129
node.left.object.name === 'module' &&
130+
node.left.property.type === 'Identifier' &&
133131
node.left.property.name === 'exports'
134132
) {
135133
exportsNode = node.right
136134
}
137135
},
138136

139-
ExportDefaultDeclaration(node) {
137+
ExportDefaultDeclaration(node: ExportDefaultDeclaration) {
140138
exportsNode = node.declaration
141139
},
142140

143-
'Program:exit'(programNode) {
141+
'Program:exit'() {
144142
checkMetaValidity(context, exportsNode)
145143
}
146144
}
Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,20 @@
33
* @author Vitor Balocco
44
*/
55

6-
'use strict'
7-
86
/**
97
* Gets the property of the Object node passed in that has the name specified.
10-
*
11-
* @param {string} propertyName Name of the property to return.
12-
* @param {ASTNode} node The ObjectExpression node.
13-
* @returns {ASTNode} The Property node or null if not found.
148
*/
15-
function getPropertyFromObject(propertyName, node) {
9+
function getPropertyFromObject(
10+
propertyName: string,
11+
node: ASTNode | undefined
12+
): Property | null {
1613
if (node && node.type === 'ObjectExpression') {
1714
for (const property of node.properties) {
18-
if (property.type === 'Property' && property.key.name === propertyName) {
15+
if (
16+
property.type === 'Property' &&
17+
property.key.type === 'Identifier' &&
18+
property.key.name === propertyName
19+
) {
1920
return property
2021
}
2122
}
@@ -25,17 +26,15 @@ function getPropertyFromObject(propertyName, node) {
2526

2627
/**
2728
* Checks the validity of the meta definition of this rule and reports any errors found.
28-
*
29-
* @param {RuleContext} context The ESLint rule context.
30-
* @param {ASTNode} exportsNode ObjectExpression node that the rule exports.
31-
* @param {boolean} ruleIsFixable whether the rule is fixable or not.
32-
* @returns {void}
3329
*/
34-
function checkMetaValidity(context, exportsNode) {
30+
function checkMetaValidity(
31+
context: RuleContext,
32+
exportsNode: ASTNode | undefined
33+
) {
3534
const metaProperty = getPropertyFromObject('meta', exportsNode)
3635
if (!metaProperty) {
3736
context.report({
38-
node: exportsNode,
37+
node: exportsNode!,
3938
messageId: 'missingMeta'
4039
})
4140
return
@@ -73,7 +72,7 @@ function checkMetaValidity(context, exportsNode) {
7372
})
7473
}
7574

76-
module.exports = {
75+
export default {
7776
meta: {
7877
type: 'problem',
7978
docs: {
@@ -92,27 +91,29 @@ module.exports = {
9291
languages: ['js/js']
9392
},
9493

95-
create(context) {
96-
let exportsNode
94+
create(context: RuleContext) {
95+
let exportsNode: ASTNode | undefined
9796

9897
return {
99-
AssignmentExpression(node) {
98+
AssignmentExpression(node: AssignmentExpression) {
10099
if (
101100
node.left &&
102101
node.right &&
103102
node.left.type === 'MemberExpression' &&
103+
node.left.object.type === 'Identifier' &&
104104
node.left.object.name === 'module' &&
105+
node.left.property.type === 'Identifier' &&
105106
node.left.property.name === 'exports'
106107
) {
107108
exportsNode = node.right
108109
}
109110
},
110111

111-
ExportDefaultDeclaration(node) {
112+
ExportDefaultDeclaration(node: ExportDefaultDeclaration) {
112113
exportsNode = node.declaration
113114
},
114115

115-
'Program:exit'(programNode) {
116+
'Program:exit'() {
116117
checkMetaValidity(context, exportsNode)
117118
}
118119
}

eslint-internal-rules/require-eslint-community.js renamed to eslint-internal-rules/require-eslint-community.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
'use strict'
2-
3-
module.exports = {
1+
export default {
42
meta: {
53
type: 'problem',
64
docs: {
@@ -16,14 +14,10 @@ module.exports = {
1614
languages: ['js/js']
1715
},
1816

19-
/** @param {import('eslint').Rule.RuleContext} context */
20-
create(context) {
17+
create(context: RuleContext) {
2118
return {
22-
/**
23-
* @param {import("../typings/eslint-plugin-vue/util-types/ast").Literal} node
24-
*/
2519
'CallExpression > Literal.arguments[value=/^(?:eslint-utils|regexpp)$/u]'(
26-
node
20+
node: Literal & { value: string }
2721
) {
2822
context.report({
2923
node,

‎eslint.config.mjs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import eslintPluginMarkdownPreferences from 'eslint-plugin-markdown-preferences'
1111
import eslintPluginTs from '@typescript-eslint/eslint-plugin'
1212
import tsEslintParser from '@typescript-eslint/parser'
1313
import vueEslintParser from 'vue-eslint-parser'
14-
import noInvalidMeta from './eslint-internal-rules/no-invalid-meta.js'
15-
import noInvalidMetaDocsCategories from './eslint-internal-rules/no-invalid-meta-docs-categories.js'
16-
import requireEslintCommunity from './eslint-internal-rules/require-eslint-community.js'
14+
import noInvalidMeta from './eslint-internal-rules/no-invalid-meta.ts'
15+
import noInvalidMetaDocsCategories from './eslint-internal-rules/no-invalid-meta-docs-categories.ts'
16+
import requireEslintCommunity from './eslint-internal-rules/require-eslint-community.ts'
1717
import rules from './tools/lib/rules.js'
1818
import path from 'node:path'
1919
import { fileURLToPath } from 'node:url'
@@ -326,7 +326,7 @@ export default typegen([
326326
}
327327
},
328328
{
329-
files: ['eslint-internal-rules/*.js'],
329+
files: ['eslint-internal-rules/*.ts'],
330330
rules: {
331331
'eslint-plugin/require-meta-docs-url': 'off',
332332
'internal/no-invalid-meta': 'error',

‎tsconfig.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
},
2929
"include": [
3030
"lib/**/*",
31+
"eslint-internal-rules/**/*.ts",
3132
"tests/**/*.ts",
3233
"typings/eslint-plugin-vue/global.d.ts",
3334
"docs/.vitepress/**/*.ts",

0 commit comments

Comments
 (0)