Skip to content

Commit

Permalink
test: migrate to type-friendly createRuleTestCaseFunction (#184)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcalexiei authored Nov 25, 2024
1 parent 8c1c14d commit bc4de89
Show file tree
Hide file tree
Showing 69 changed files with 4,414 additions and 4,931 deletions.
5 changes: 5 additions & 0 deletions .changeset/cuddly-ligers-wonder.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-import-x": patch
---

fix(no-cycle): improves the type declaration of the rule `no-cycle`’s `maxDepth` option
5 changes: 5 additions & 0 deletions .changeset/fluffy-dolls-pump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-import-x": patch
---

fix(first): improves the type declaration of the rule `first`'s option
5 changes: 5 additions & 0 deletions .changeset/nice-schools-drive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-import-x": patch
---

fix(no-unused-modules): improves the type declaration of the rule `no-unused-modules`’s `missingExports` option
5 changes: 5 additions & 0 deletions .changeset/nine-wolves-repeat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-import-x": patch
---

fix(no-deprecated): improve error message when no description is available
3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
module.exports = {
root: true,
reportUnusedDisableDirectives: true,
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
Expand Down Expand Up @@ -33,7 +34,7 @@ module.exports = {
'eslint-plugin/report-message-format': 'error',
'eslint-plugin/require-meta-docs-description': [
'error',
{ pattern: '^(Enforce|Ensure|Prefer|Forbid).+\\.$' },
{ pattern: String.raw`^(Enforce|Ensure|Prefer|Forbid).+\.$` },
],
'eslint-plugin/require-meta-schema': 'error',
'eslint-plugin/require-meta-type': 'error',
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"eslint": "^8.57.0 || ^9.0.0"
},
"dependencies": {
"@typescript-eslint/scope-manager": "^8.1.0",
"@typescript-eslint/utils": "^8.1.0",
"debug": "^4.3.4",
"doctrine": "^3.0.0",
Expand Down Expand Up @@ -82,8 +83,8 @@
"@total-typescript/ts-reset": "^0.5.1",
"@types/debug": "^4.1.12",
"@types/doctrine": "^0.0.9",
"@types/eslint8.56": "npm:@types/eslint@^8.56.11",
"@types/eslint": "^9.6.1",
"@types/eslint8.56": "npm:@types/eslint@^8.56.11",
"@types/eslint9": "npm:@types/eslint@^9.6.1",
"@types/is-glob": "^4.0.4",
"@types/jest": "^29.5.12",
Expand All @@ -97,9 +98,7 @@
"cross-env": "^7.0.3",
"enhanced-resolve": "^5.16.0",
"escope": "^4.0.0",
"eslint8.56": "npm:eslint@^8.56.0",
"eslint": "^9.15.0",
"eslint9": "npm:eslint@^9.15.0",
"eslint-config-prettier": "^9.1.0",
"eslint-doc-generator": "^1.7.1",
"eslint-import-resolver-typescript": "^3.6.1",
Expand All @@ -112,6 +111,8 @@
"eslint-plugin-n": "^16.6.2",
"eslint-plugin-prettier": "^5.1.3",
"eslint-plugin-unicorn": "^56.0.1",
"eslint8.56": "npm:eslint@^8.56.0",
"eslint9": "npm:eslint@^9.15.0",
"hermes-eslint": "^0.23.1",
"jest": "^29.7.0",
"klaw-sync": "^6.0.0",
Expand Down
2 changes: 1 addition & 1 deletion src/rules/dynamic-import-chunkname.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export = createRule<[Options?], MessageId>({
const {
importFunctions = [],
allowEmpty = false,
webpackChunknameFormat = '([0-9a-zA-Z-_/.]|\\[(request|index)\\])+',
webpackChunknameFormat = String.raw`([0-9a-zA-Z-_/.]|\[(request|index)\])+`,
} = context.options[0] || {}

const paddedCommentRegex = /^ (\S[\S\s]+\S) $/
Expand Down
4 changes: 3 additions & 1 deletion src/rules/first.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ function isPossibleDirective(node: TSESTree.ProgramStatement) {
)
}

type Options = 'absolute-first' | 'disable-absolute-first'

type MessageId = 'absolute' | 'order'

export = createRule<['absolute-first'?], MessageId>({
export = createRule<[Options?], MessageId>({
name: 'first',
meta: {
type: 'suggestion',
Expand Down
4 changes: 3 additions & 1 deletion src/rules/group-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ function accessorChain(node: TSESTree.MemberExpression) {
return chain
}

export = createRule<[], 'ExportNamedDeclaration' | 'AssignmentExpression'>({
type MessageId = 'ExportNamedDeclaration' | 'AssignmentExpression'

export = createRule<[], MessageId>({
name: 'group-exports',
meta: {
type: 'suggestion',
Expand Down
12 changes: 8 additions & 4 deletions src/rules/namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type MessageId =
| 'namespaceMember'
| 'topLevelNames'
| 'notFoundInNamespace'
| 'notFoundInNamespaceDeep'

type Options = {
allowComputed?: boolean
Expand Down Expand Up @@ -86,15 +87,16 @@ function makeMessage(
namepath: string[],
node: TSESTree.Node = last,
) {
const messageId =
namepath.length > 1 ? 'notFoundInNamespaceDeep' : 'notFoundInNamespace'
return {
node,
messageId: 'notFoundInNamespace' as const,
messageId,
data: {
name: last.name,
depth: namepath.length > 1 ? 'deeply ' : '',
namepath: namepath.join('.'),
},
}
} as const
}

export = createRule<[Options], MessageId>({
Expand Down Expand Up @@ -127,7 +129,9 @@ export = createRule<[Options], MessageId>({
namespaceMember: "Assignment to member of namespace '{{namespace}}'.",
topLevelNames: 'Only destructure top-level names.',
notFoundInNamespace:
"'{{name}}' not found in {{depth}}imported namespace '{{namepath}}'.",
"'{{name}}' not found in imported namespace '{{namepath}}'.",
notFoundInNamespaceDeep:
"'{{name}}' not found in deeply imported namespace '{{namepath}}'.",
},
},
defaultOptions: [
Expand Down
31 changes: 18 additions & 13 deletions src/rules/no-cycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import {
type Options = {
allowUnsafeDynamicCyclicDependency?: boolean
ignoreExternal?: boolean
maxDepth?: number
maxDepth?: number | '∞'
} & ModuleOptions

type MessageId = 'cycle'
type MessageId = 'cycle' | 'cycleSource'

type Traverser = {
mget(): ExportMap | null
Expand Down Expand Up @@ -65,7 +65,8 @@ export = createRule<[Options?], MessageId>({
}),
],
messages: {
cycle: 'Dependency cycle {{source}}',
cycle: 'Dependency cycle detected',
cycleSource: 'Dependency cycle via "{{source}}"',
},
},
defaultOptions: [],
Expand Down Expand Up @@ -187,16 +188,20 @@ export = createRule<[Options?], MessageId>({
while (untraversed.length > 0) {
const next = untraversed.shift()! // bfs!
if (detectCycle(next)) {
context.report({
node: importer,
messageId: 'cycle',
data: {
source:
next.route.length > 0
? `via ${routeString(next.route)}`
: 'detected.',
},
})
if (next.route.length > 0) {
context.report({
node: importer,
messageId: 'cycleSource',
data: {
source: routeString(next.route),
},
})
} else {
context.report({
node: importer,
messageId: 'cycle',
})
}
return
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/rules/no-default-export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ export = createRule({

ExportNamedDeclaration(node) {
for (const specifier of node.specifiers.filter(
specifier => getValue(specifier.exported) === 'default')) {
specifier => getValue(specifier.exported) === 'default',
)) {
const { loc } = sourceCode.getFirstTokens(node)[1] || {}
// @ts-expect-error - experimental parser type
if (specifier.type === 'ExportDefaultSpecifier') {
Expand Down
19 changes: 10 additions & 9 deletions src/rules/no-deprecated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import type { ModuleNamespace } from '../utils'
import { ExportMap, createRule, declaredScope, getValue } from '../utils'

function message(deprecation: Tag) {
return {
messageId: 'deprecated',
data: {
description: deprecation.description
? `: ${deprecation.description}`
: '.',
},
} as const
if (deprecation.description) {
return {
messageId: 'deprecatedDesc',
data: { description: deprecation.description },
} as const
}

return { messageId: 'deprecated' } as const
}

function getDeprecation(metadata?: ModuleNamespace | null) {
Expand All @@ -34,7 +34,8 @@ export = createRule({
},
schema: [],
messages: {
deprecated: 'Deprecated{{description}}',
deprecatedDesc: 'Deprecated: {{description}}',
deprecated: 'Deprecated: consider to find an alternative.',
},
},
defaultOptions: [],
Expand Down
2 changes: 1 addition & 1 deletion src/rules/no-duplicates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ function hasCommentInsideNonSpecifiers(
// `node` (only inside). If there's a `{...}` part, look for comments before
// the `{`, but not before the `}` (hence the `+1`s).
const someTokens =
openBraceIndex >= 0 && closeBraceIndex >= 0
openBraceIndex !== -1 && closeBraceIndex !== -1
? [
...tokens.slice(1, openBraceIndex + 1),
...tokens.slice(closeBraceIndex + 1),
Expand Down
20 changes: 9 additions & 11 deletions src/rules/no-import-module-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,15 @@ function getEntryPoint(context: RuleContext) {

function findScope(context: RuleContext, identifier: string) {
const { scopeManager } = context.sourceCode
return (
scopeManager?.scopes
// eslint-disable-next-line unicorn/prefer-spread
.slice()
.reverse()
.find(scope =>
scope.variables.some(variable =>
variable.identifiers.some(node => node.name === identifier),
),
)
)
return scopeManager?.scopes

.slice()
.reverse()
.find(scope =>
scope.variables.some(variable =>
variable.identifiers.some(node => node.name === identifier),
),
)
}

function findDefinition(objectScope: TSESLint.Scope.Scope, identifier: string) {
Expand Down
3 changes: 1 addition & 2 deletions src/rules/no-named-export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ export = createRule({
}

const someNamed = node.specifiers.some(
specifier => getValue(specifier.exported) !==
'default',
specifier => getValue(specifier.exported) !== 'default',
)
if (someNamed) {
context.report({ node, messageId: 'noAllowed' })
Expand Down
2 changes: 1 addition & 1 deletion src/rules/no-unused-modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ const fileIsInPkg = (file: string) => {
type Options = {
src?: string[]
ignoreExports?: string[]
missingExports?: string[]
missingExports?: true
unusedExports?: boolean
ignoreUnusedTypeExports?: boolean
}
Expand Down
4 changes: 1 addition & 3 deletions src/rules/prefer-default-export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@ export = createRule<[Options?], MessageId>({
},

ExportSpecifier(node) {
if (
getValue(node.exported) === 'default'
) {
if (getValue(node.exported) === 'default') {
hasDefaultExport = true
} else {
specifierExportCount++
Expand Down
2 changes: 1 addition & 1 deletion src/utils/declared-scope.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ScopeType } from '@typescript-eslint/scope-manager'
import type { TSESTree } from '@typescript-eslint/utils'
import type { ScopeType } from '@typescript-eslint/scope-manager';

import type { RuleContext } from '../types'

Expand Down
5 changes: 4 additions & 1 deletion src/utils/export-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,10 @@ export class ExportMap {
}
}

function addNamespace(object: object, identifier: TSESTree.Identifier | TSESTree.StringLiteral) {
function addNamespace(
object: object,
identifier: TSESTree.Identifier | TSESTree.StringLiteral,
) {
const nsfn = getNamespace(getValue(identifier))
if (nsfn) {
Object.defineProperty(object, 'namespace', { get: nsfn })
Expand Down
1 change: 0 additions & 1 deletion src/utils/pkg-up.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ function findUp(filename: string | string[], cwd?: string): string | null {

const filenames = [filename].flat()

// eslint-disable-next-line no-constant-condition
while (true) {
const file = filenames.find(el => fs.existsSync(path.resolve(dir, el)))

Expand Down
1 change: 0 additions & 1 deletion src/utils/read-pkg-up.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ function stripBOM(str: string) {
return str.replace(/^\uFEFF/, '')
}

// eslint-disable-next-line eslint-plugin/require-meta-docs-description, eslint-plugin/require-meta-type, eslint-plugin/prefer-message-ids, eslint-plugin/prefer-object-rule, eslint-plugin/require-meta-schema
export function readPkgUp(opts?: { cwd?: string }) {
const fp = pkgUp(opts)

Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,8 @@ export function fine() {
export function _undocumented() {
return 'sneaky!'
}

/** @deprecated */
export function _deprecatedNoDescription() {
return '_deprecatedNoDescription'
}
Loading

0 comments on commit bc4de89

Please sign in to comment.