Skip to content

Commit

Permalink
refactor: migrate all core test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin committed Mar 13, 2024
1 parent 6cea7e0 commit e78451c
Show file tree
Hide file tree
Showing 32 changed files with 811 additions and 774 deletions.
11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,17 @@
"export"
],
"scripts": {
"build": "rimraf lib && tsc -p src",
"lint": "yarn lint:es && yarn update:eslint-docs --check",
"build": "tsc -p src",
"clean": "rimraf lib",
"lint": "run-p lint:*",
"lint:docs": "yarn update:eslint-docs --check",
"lint:es": "eslint . --cache",
"lint:tsc": "tsc -p tsconfig.base.json --noEmit",
"prepare": "patch-package",
"release": "changeset publish",
"test": "jest",
"test-compiled": "yarn build && cross-env TEST_COMPILED=1 jest",
"update:eslint-docs": "yarn build && eslint-doc-generator --rule-doc-title-format prefix-name --rule-doc-section-options false --rule-list-split meta.docs.category --ignore-config stage-0 --config-emoji recommended,☑️",
"update:eslint-docs": "eslint-doc-generator --rule-doc-title-format prefix-name --rule-doc-section-options false --rule-list-split meta.docs.category --ignore-config stage-0 --config-emoji recommended,☑️",
"watch": "yarn test --watch"
},
"peerDependencies": {
Expand Down Expand Up @@ -75,6 +78,7 @@
"@types/eslint": "^8.56.5",
"@types/jest": "^29.5.12",
"@types/json-schema": "^7.0.15",
"@types/node": "^20.11.26",
"@typescript-eslint/eslint-plugin": "^5.62.0",
"@typescript-eslint/parser": "^5.62.0",
"@typescript-eslint/typescript-estree": "^5.62.0",
Expand All @@ -94,6 +98,7 @@
"eslint-plugin-json": "^3.1.0",
"eslint-plugin-prettier": "^5.1.3",
"jest": "^29.7.0",
"npm-run-all2": "^6.1.2",
"prettier": "^3.2.5",
"redux": "^5.0.1",
"rimraf": "^5.0.5",
Expand Down
2 changes: 1 addition & 1 deletion src/core/importType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function isInternalRegexMatch(name: string, settings: PluginSettings) {
return internalScope && new RegExp(internalScope).test(name)
}

export function isAbsolute(name: string) {
export function isAbsolute(name?: string | boolean | number | null) {
return typeof name === 'string' && nodeIsAbsolute(name)
}

Expand Down
6 changes: 2 additions & 4 deletions src/export-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ export class ExportMap {

private declare mtime: Date

private declare doc: Annotation
declare doc: Annotation

constructor(public path: string) {}

Expand Down Expand Up @@ -812,8 +812,6 @@ export class ExportMap {
}
}
}

return undefined
}

forEach(
Expand Down Expand Up @@ -1072,7 +1070,7 @@ function childContext(
: 'getFilename' in context &&
typeof context.getFilename === 'function'
? context.getFilename()
: (('filename' in context && context.filename) as string),
: ('filename' in context && context.filename) || undefined,
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/rules/newline-after-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @author Radek Benkel
*/

import {isStaticRequire} from '../core/staticRequire'
import { isStaticRequire } from '../core/staticRequire'
import { docsUrl } from '../docs-url'

import debug from 'debug'
Expand Down
2 changes: 1 addition & 1 deletion src/rules/no-extraneous-dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { pkgUp } from '../utils/pkgUp'
import { minimatch } from 'minimatch'
import { resolve } from '../utils/resolve'
import { moduleVisitor } from '../utils/moduleVisitor'
import {importType} from '../core/importType'
import { importType } from '../core/importType'
import { getFilePackageName } from '../core/packagePath'
import { docsUrl } from '../docs-url'

Expand Down
2 changes: 1 addition & 1 deletion src/rules/no-internal-modules.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { makeRe } from 'minimatch'

import { resolve } from '../utils/resolve'
import {importType} from '../core/importType'
import { importType } from '../core/importType'
import { moduleVisitor } from '../utils/moduleVisitor'
import { docsUrl } from '../docs-url'

Expand Down
2 changes: 1 addition & 1 deletion src/rules/no-nodejs-modules.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {importType} from '../core/importType'
import { importType } from '../core/importType'
import { moduleVisitor } from '../utils/moduleVisitor'
import { docsUrl } from '../docs-url'

Expand Down
2 changes: 1 addition & 1 deletion src/rules/no-relative-packages.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { readPkgUp } from '../utils/readPkgUp'

import { resolve } from '../utils/resolve'
import { moduleVisitor, makeOptionsSchema } from '../utils/moduleVisitor'
import {importType} from '../core/importType'
import { importType } from '../core/importType'
import { docsUrl } from '../docs-url'

/** @param {string} filePath */
Expand Down
2 changes: 1 addition & 1 deletion src/rules/no-relative-parent-imports.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { docsUrl } from '../docs-url'
import { basename, dirname, relative } from 'path'
import { resolve } from '../utils/resolve'

import {importType} from '../core/importType'
import { importType } from '../core/importType'

module.exports = {
meta: {
Expand Down
2 changes: 1 addition & 1 deletion src/rules/no-restricted-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { moduleVisitor } from '../utils/moduleVisitor'
import isGlob from 'is-glob'
import { Minimatch } from 'minimatch'
import { docsUrl } from '../docs-url'
import {importType} from '../core/importType'
import { importType } from '../core/importType'

const containsPath = (filepath, target) => {
const relative = path.relative(target, filepath)
Expand Down
2 changes: 1 addition & 1 deletion src/rules/no-unassigned-import.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'path'
import { minimatch } from 'minimatch'

import {isStaticRequire} from '../core/staticRequire'
import { isStaticRequire } from '../core/staticRequire'
import { docsUrl } from '../docs-url'

function report(context, node) {
Expand Down
4 changes: 2 additions & 2 deletions src/rules/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import { minimatch } from 'minimatch'

import {importType} from '../core/importType'
import {isStaticRequire} from '../core/staticRequire'
import { importType } from '../core/importType'
import { isStaticRequire } from '../core/staticRequire'
import { docsUrl } from '../docs-url'

// This is a **non-spec compliant** but works in practice replacement of `object.groupby` package.
Expand Down
22 changes: 13 additions & 9 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export interface NodeResolverOptions {
}

export interface WebpackResolverOptions {
config?: string | ResolveOptions
config?: string | { resolve: Omit<ResolveOptions, 'fileSystem'> }
'config-index'?: number
env?: Record<string, unknown>
argv?: Record<string, unknown>
Expand All @@ -22,25 +22,29 @@ export type FileExtension = `.${string}`

export type DocStyle = 'jsdoc' | 'tomdoc'

export type Arrayable<T> = T | readonly T[]

export interface ImportSettings {
cache?: {
lifetime: number | '∞' | 'Infinity'
lifetime?: number | '∞' | 'Infinity'
}
coreModules?: string[]
docstyle?: DocStyle[]
extensions?: readonly FileExtension[]
externalModuleFolders?: string[]
ignore?: string[]
internalRegex?: string
parsers?: Record<string, FileExtension[]>
parsers?: Record<string, readonly FileExtension[]>
resolve?: NodeResolverOptions
resolver?:
resolver?: Arrayable<
| LiteralUnion<'node' | 'typescript' | 'webpack', string>
| {
node?: boolean | NodeResolverOptions
typescript?: boolean | TsResolverOptions
webpack?: WebpackResolverOptions
[resolve: string]: unknown
}
>
}

export type WithPluginName<T extends string | object> = T extends string
Expand All @@ -62,19 +66,19 @@ export interface RuleContext<
TOptions extends readonly unknown[] = readonly unknown[],
> extends Omit<TSESLint.RuleContext<TMessageIds, TOptions>, 'settings'> {
languageOptions?: {
parser: TSESLint.Linter.ParserModule
parserOptions: TSESLint.ParserOptions
parser?: TSESLint.Linter.ParserModule
parserOptions?: TSESLint.ParserOptions
}
settings: PluginSettings
}

export interface ChildContext {
cacheKey: string
settings: PluginSettings
parserPath: string
parserOptions: TSESLint.ParserOptions
parserPath?: string | null
parserOptions?: TSESLint.ParserOptions
path: string
filename: string
filename?: string
}

export interface ParseError extends Error {
Expand Down
13 changes: 9 additions & 4 deletions src/utils/ignore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@ import { extname } from 'path'

import debug from 'debug'

import type { ChildContext, FileExtension, PluginSettings } from '../types'
import type {
ChildContext,
FileExtension,
PluginSettings,
RuleContext,
} from '../types'

const log = debug('eslint-plugin-import-x:utils:ignore')

// one-shot memoized
let cachedSet: Set<FileExtension>
let lastSettings: PluginSettings

function validExtensions(context: ChildContext) {
function validExtensions(context: ChildContext | RuleContext) {
if (cachedSet && context.settings === lastSettings) {
return cachedSet
}
Expand Down Expand Up @@ -40,7 +45,7 @@ export function getFileExtensions(settings: PluginSettings) {
return exts
}

export function ignore(path: string, context: ChildContext) {
export function ignore(path: string, context: ChildContext | RuleContext) {
// check extension whitelist first (cheap)
if (!hasValidExtension(path, context)) {
return true
Expand All @@ -65,7 +70,7 @@ export function ignore(path: string, context: ChildContext) {

export function hasValidExtension(
path: string,
context: ChildContext,
context: ChildContext | RuleContext,
): path is `${string}${FileExtension}` {
return validExtensions(context).has(extname(path) as FileExtension)
}
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './constants'
export * from './create-rule'
export * from './module-require'
15 changes: 10 additions & 5 deletions src/utils/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ import fs from 'fs'
import Module from 'module'
import path from 'path'

import { ImportSettings, PluginSettings, RuleContext } from '../types'
import type {
Arrayable,
ImportSettings,
PluginSettings,
RuleContext,
} from '../types'

import { hashObject } from './hash'
import { ModuleCache } from './ModuleCache'
Expand Down Expand Up @@ -79,7 +84,7 @@ function tryRequire<T>(
// https://stackoverflow.com/a/27382838
export function fileExistsWithCaseSync(
filepath: string | null,
cacheSettings: ImportSettings['cache'],
cacheSettings?: ImportSettings['cache'],
strict?: boolean,
): boolean {
// don't care if the FS is case-sensitive
Expand Down Expand Up @@ -212,7 +217,7 @@ export function relative(
}

function resolverReducer(
resolvers: string[] | string | Record<string, unknown>,
resolvers: Arrayable<string | Record<string, unknown>>,
map: Map<string, unknown>,
) {
if (Array.isArray(resolvers)) {
Expand All @@ -226,8 +231,8 @@ function resolverReducer(
}

if (typeof resolvers === 'object') {
for (const key in resolvers) {
map.set(key, resolvers[key])
for (const [key, value] of Object.entries(resolvers)) {
map.set(key, value)
}
return map
}
Expand Down
Loading

0 comments on commit e78451c

Please sign in to comment.