Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: migrate to createRuleTestCaseFunction #184

Merged
merged 49 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
f2a3dba
test(default): migrate to `createRuleTestCaseFunction`
marcalexiei Nov 14, 2024
4614f7a
test(consistent-type-specifier-style): migrate to `createRuleTestCase…
marcalexiei Nov 14, 2024
3a28a50
test(dynamic-import-chunkname): migrate to `createRuleTestCaseFunction`
marcalexiei Nov 14, 2024
bf70690
test(export): migrate to `createRuleTestCaseFunction`
marcalexiei Nov 14, 2024
aca0f8d
test(exports-last): migrate to `createRuleTestCaseFunction`
marcalexiei Nov 14, 2024
f34ec1a
fix(first): add missing value in options type
marcalexiei Nov 15, 2024
ccd3ade
test(group-exports): migrate to `createRuleTestCaseFunction`
marcalexiei Nov 15, 2024
4400f8b
test(max-dependencies): migrate to `createRuleTestCaseFunction`
marcalexiei Nov 15, 2024
4c514f8
test(named): migrate to `createRuleTestCaseFunction`
marcalexiei Nov 15, 2024
1a25c6c
ci: check if the issue is with the last eslint version
marcalexiei Nov 15, 2024
9df6150
docs: update changeset entry
marcalexiei Nov 16, 2024
aff7922
test(namespace): migrate to `createRuleTestCaseFunction`
marcalexiei Nov 16, 2024
4b649ce
test(newline-after-import): add types to helper functions
marcalexiei Nov 16, 2024
0a7a1f0
test(no-absolute-path): add types to helper functions
marcalexiei Nov 16, 2024
c945045
test(no-anonymous-default-export): migrate to `createRuleTestCaseFunc…
marcalexiei Nov 16, 2024
2b0e3fb
fix(no-cycle): improves the type declaration of the rule `no-cycle`’s…
marcalexiei Nov 17, 2024
3fa2820
test(utils): rewrite `createRuleTestCaseFunctions` to be more type ch…
marcalexiei Nov 17, 2024
43eb6ef
test(no-default-export): migrate to `createRuleTestCaseFunctions`
marcalexiei Nov 18, 2024
0478063
fix(no-deprecated): improve error message when no description is avai…
marcalexiei Nov 18, 2024
876a6c7
test(no-duplicates): migrate to `createRuleTestCaseFunctions`
marcalexiei Nov 18, 2024
967c38a
test(no-dynamic-require): migrate to `createRuleTestCaseFunctions`
marcalexiei Nov 18, 2024
7741c15
test(no-empty-named-blocks): migrate to `createRuleTestCaseFunctions`
marcalexiei Nov 19, 2024
cdf91f9
chore(eslint): enable `reportUnusedDisableDirectives`
marcalexiei Nov 19, 2024
4f9e82b
test(no-extraneous-dependencies): migrate to `createRuleTestCaseFunct…
marcalexiei Nov 19, 2024
1c5add5
test(no-import-module-exports): migrate to `createRuleTestCaseFunctions`
marcalexiei Nov 19, 2024
2ca670d
test(no-internal-modules): migrate to `createRuleTestCaseFunctions`
marcalexiei Nov 19, 2024
9f33bd6
test(no-mutable-exports): migrate to `createRuleTestCaseFunctions`
marcalexiei Nov 19, 2024
9d238ce
test(no-named-as-default-member): migrate to `createRuleTestCaseFunct…
marcalexiei Nov 19, 2024
8b15d24
test(no-named-as-default): migrate to `createRuleTestCaseFunctions`
marcalexiei Nov 19, 2024
f4e2db1
test(no-named-default): migrate to `createRuleTestCaseFunctions`
marcalexiei Nov 19, 2024
47f5cff
test(no-named-export): migrate to `createRuleTestCaseFunctions`
marcalexiei Nov 19, 2024
48b4034
test(no-namespace): migrate to `createRuleTestCaseFunctions`
marcalexiei Nov 19, 2024
92a0d48
test(no-nodejs-modules): migrate to `createRuleTestCaseFunctions`
marcalexiei Nov 19, 2024
99900ff
test(no-relative-packages): migrate to `createRuleTestCaseFunctions`
marcalexiei Nov 19, 2024
75a66a3
test(no-relative-parent-imports): migrate to `createRuleTestCaseFunct…
marcalexiei Nov 19, 2024
c24546c
test(utils): add defaultOptions param to `createRuleTestCaseFunctions`
marcalexiei Nov 19, 2024
ae3b808
test(no-rename-default): migrate to `createRuleTestCaseFunctions`
marcalexiei Nov 19, 2024
0926074
test(no-restricted-paths): migrate to `createRuleTestCaseFunctions`
marcalexiei Nov 20, 2024
4fe2006
test(no-self-import): migrate to `createRuleTestCaseFunctions`
marcalexiei Nov 20, 2024
91705cc
test(no-unassigned-import): migrate to `createRuleTestCaseFunctions`
marcalexiei Nov 20, 2024
582d921
test(no-unresolved): migrate to `createRuleTestCaseFunctions`
marcalexiei Nov 20, 2024
0529088
fix(no-unused-modules): improves the type declaration of the rule `no…
marcalexiei Nov 20, 2024
dc6276c
test(order): migrate to `createRuleTestCaseFunctions`
marcalexiei Nov 20, 2024
8dcd3cd
test(prefer-default-export): migrate to `createRuleTestCaseFunctions`
marcalexiei Nov 20, 2024
511bcfe
test: use `AST_NODE_TYPES` directlyinstead of reading from `TSESTree`
marcalexiei Nov 20, 2024
71e410b
test: remove old utils
marcalexiei Nov 20, 2024
9a4ee87
test: add `SYNTAX_VALID_CASES` casting
marcalexiei Nov 20, 2024
8b6c09f
chore: apply fix from last version of `eslint-plugin-unicorn`
marcalexiei Nov 20, 2024
5247ee5
ci: restore version `9` on `eslint` matrix
marcalexiei Nov 21, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading