Skip to content

Commit

Permalink
feat: migrate rule no-unresolved and related modules
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin committed Mar 13, 2024
1 parent 36a62d6 commit 8dbd9b4
Show file tree
Hide file tree
Showing 89 changed files with 758 additions and 748 deletions.
7 changes: 6 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* @type {import('eslint').Linter.Config}
*/
module.exports = {
root: true,
plugins: ['eslint-plugin', 'import-x'],
Expand All @@ -18,6 +21,7 @@ module.exports = {
ecmaVersion: 2020,
},
rules: {
'@typescript-eslint/no-non-null-assertion': 'off',
'@typescript-eslint/no-var-requires': 'off',

'eslint-plugin/consistent-output': ['error', 'always'],
Expand Down Expand Up @@ -56,7 +60,8 @@ module.exports = {

overrides: [
{
files: ['src/**/*.ts'],
files: ['*.js', '*.ts'],
excludedFiles: ['test/fixtures'],
settings: {
'import-x/resolver': {
typescript: true,
Expand Down
3 changes: 2 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ module.exports = {
modulePathIgnorePatterns: ['<rootDir>/test/fixtures/with-syntax-error'],
moduleNameMapper: {
'^core/(.+)$': `<rootDir>/${srcDir}/core/$1`,
'^eslint-plugin-import-x$': `<rootDir>/${srcDir}/index.js`,
'^eslint-plugin-import-x$': `<rootDir>/${srcDir}`,
'^eslint-plugin-import-x/package.json$': '<rootDir>/package.json',
'^eslint-plugin-import-x/(.+)$': `<rootDir>/${srcDir}/$1`,
'^rules/(.+)$': `<rootDir>/${srcDir}/rules/$1`,
},
testMatch: [
'<rootDir>/test/**/*.spec.js',
'<rootDir>/test/**/*.spec.ts',
'!<rootDir>/test/fixtures/**/*.js',
],
}
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"build": "rimraf lib && tsc -p src",
"lint": "yarn lint:es && yarn update:eslint-docs --check",
"lint:es": "eslint . --cache",
"prepare": "patch-package",
"release": "changeset publish",
"test": "jest",
"test-compiled": "yarn build && cross-env TEST_COMPILED=1 jest",
Expand All @@ -43,6 +44,7 @@
"eslint": "^7.2.0 || ^8"
},
"dependencies": {
"@typescript-eslint/utils": "^5.62.0",
"debug": "^4.3.4",
"doctrine": "^3.0.0",
"eslint-import-resolver-node": "^0.3.9",
Expand Down Expand Up @@ -70,8 +72,11 @@
"@test-scope/some-module": "link:./test/fixtures/symlinked-module",
"@types/eslint": "^8.56.5",
"@types/jest": "^29.5.12",
"@typescript-eslint/eslint-plugin": "^7.2.0",
"@types/json-schema": "^7.0.15",
"@typescript-eslint/eslint-plugin": "^5.62.0",
"@typescript-eslint/parser": "^5.62.0",
"@typescript-eslint/typescript-estree": "^5.62.0",
"@unts/patch-package": "^8.0.0",
"cross-env": "^7.0.3",
"enhanced-resolve": "^5.16.0",
"escope": "^4.0.0",
Expand All @@ -92,6 +97,6 @@
"rimraf": "^5.0.5",
"svelte": "^4.2.12",
"type-fest": "^4.12.0",
"typescript": "^5.4.2"
"typescript": "~5.1.0"
}
}
15 changes: 15 additions & 0 deletions patches/@typescript-eslint+utils+5.62.0.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
diff --git a/node_modules/@typescript-eslint/utils/dist/ts-eslint/Rule.d.ts b/node_modules/@typescript-eslint/utils/dist/ts-eslint/Rule.d.ts
index 9a3a1fd..46b3961 100644
--- a/node_modules/@typescript-eslint/utils/dist/ts-eslint/Rule.d.ts
+++ b/node_modules/@typescript-eslint/utils/dist/ts-eslint/Rule.d.ts
@@ -6,6 +6,10 @@ import type { Scope } from './Scope';
import type { SourceCode } from './SourceCode';
export type RuleRecommendation = 'error' | 'strict' | 'warn' | false;
interface RuleMetaDataDocs {
+ /**
+ * The category the rule falls under
+ */
+ category?: string;
/**
* Concise description of the rule
*/
4 changes: 2 additions & 2 deletions src/ExportMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { SourceCode } from 'eslint'

import parse from './utils/parse'
import visit from './utils/visit'
import resolve from './utils/resolve'
import { relative, resolve } from './utils/resolve'
import isIgnored, { hasValidExtension } from './utils/ignore'

import { hashObject } from './utils/hash'
Expand Down Expand Up @@ -506,7 +506,7 @@ ExportMap.parse = function (path, content, context) {
const namespaces = new Map()

function remotePath(value) {
return resolve.relative(value, path, context.settings)
return relative(value, path, context.settings)
}

function resolveImport(value) {
Expand Down
4 changes: 2 additions & 2 deletions src/config/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import type { PluginConfig } from '../types'
// Omit `.d.ts` because 1) TypeScript compilation already confirms that
// types are resolved, and 2) it would mask an unresolved
// `.ts`/`.tsx`/`.js`/`.jsx` implementation.
const typeScriptExtensions = ['.ts', '.tsx']
const typeScriptExtensions = ['.ts', '.tsx'] as const

const allExtensions = [...typeScriptExtensions, '.js', '.jsx']
const allExtensions = [...typeScriptExtensions, '.js', '.jsx'] as const

export = {
settings: {
Expand Down
2 changes: 1 addition & 1 deletion src/core/importType.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
} from 'path'

import { isCoreModule } from '../utils/is-core-module'
import resolve from '../utils/resolve'
import { resolve } from '../utils/resolve'
import { getContextPackagePath } from './packagePath'

function baseModule(name) {
Expand Down
4 changes: 2 additions & 2 deletions src/core/packagePath.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { dirname } from 'path'
import pkgUp from '../utils/pkgUp'
import readPkgUp from '../utils/readPkgUp'
import { pkgUp } from '../utils/pkgUp'
import { readPkgUp } from '../utils/readPkgUp'

export function getContextPackagePath(context) {
return getFilePackagePath(
Expand Down
8 changes: 8 additions & 0 deletions src/docs-url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore - We're using commonjs
import pkg from '../package.json'

const repoUrl = 'https://github.com/un-es/eslint-plugin-import-x'

export const docsUrl = (ruleName: string, commitish = `v${pkg.version}`) =>
`${repoUrl}/blob/${commitish}/docs/rules/${ruleName}.md`
7 changes: 0 additions & 7 deletions src/docsUrl.js

This file was deleted.

6 changes: 6 additions & 0 deletions src/import-declaration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { Rule } from 'eslint'

export const importDeclaration = (context: Rule.RuleContext) => {
const ancestors = context.getAncestors()
return ancestors[ancestors.length - 1]
}
4 changes: 0 additions & 4 deletions src/importDeclaration.js

This file was deleted.

11 changes: 8 additions & 3 deletions src/index.js → src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import type { PluginConfig } from './types'

import noUnresolved from './rules/no-unresolved'
import { TSESLint } from '@typescript-eslint/utils'

export const rules = {
'no-unresolved': require('./rules/no-unresolved'),
'no-unresolved': noUnresolved,
named: require('./rules/named'),
default: require('./rules/default'),
namespace: require('./rules/namespace'),
Expand Down Expand Up @@ -52,7 +57,7 @@ export const rules = {

// deprecated aliases to rules
'imports-first': require('./rules/imports-first'),
}
} satisfies Record<string, TSESLint.RuleModule<string, readonly unknown[]>>

export const configs = {
recommended: require('./config/recommended'),
Expand All @@ -68,4 +73,4 @@ export const configs = {
'react-native': require('./config/react-native'),
electron: require('./config/electron'),
typescript: require('./config/typescript'),
}
} satisfies Record<string, PluginConfig>
2 changes: 1 addition & 1 deletion src/rules/consistent-type-specifier-style.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import docsUrl from '../docsUrl'
import { docsUrl } from '../docs-url'

function isComma(token) {
return token.type === 'Punctuator' && token.value === ','
Expand Down
2 changes: 1 addition & 1 deletion src/rules/default.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Exports from '../ExportMap'
import docsUrl from '../docsUrl'
import { docsUrl } from '../docs-url'

module.exports = {
meta: {
Expand Down
2 changes: 1 addition & 1 deletion src/rules/dynamic-import-chunkname.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import vm from 'vm'
import docsUrl from '../docsUrl'
import { docsUrl } from '../docs-url'

module.exports = {
meta: {
Expand Down
2 changes: 1 addition & 1 deletion src/rules/export.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ExportMap, { recursivePatternCapture } from '../ExportMap'
import docsUrl from '../docsUrl'
import { docsUrl } from '../docs-url'

/*
Notes on TypeScript namespaces aka TSModuleDeclaration:
Expand Down
2 changes: 1 addition & 1 deletion src/rules/exports-last.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import docsUrl from '../docsUrl'
import { docsUrl } from '../docs-url'

function isNonExportStatement({ type }) {
return (
Expand Down
6 changes: 3 additions & 3 deletions src/rules/extensions.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import path from 'path'

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

const enumValues = { enum: ['always', 'ignorePackages', 'never'] }
const patternProperties = {
Expand Down
2 changes: 1 addition & 1 deletion src/rules/first.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import docsUrl from '../docsUrl'
import { docsUrl } from '../docs-url'

function getImportValue(node) {
return node.type === 'ImportDeclaration'
Expand Down
2 changes: 1 addition & 1 deletion src/rules/group-exports.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import docsUrl from '../docsUrl'
import { docsUrl } from '../docs-url'

const meta = {
type: 'suggestion',
Expand Down
2 changes: 1 addition & 1 deletion src/rules/imports-first.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import docsUrl from '../docsUrl'
import { docsUrl } from '../docs-url'

const first = require('./first')

Expand Down
4 changes: 2 additions & 2 deletions src/rules/max-dependencies.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import moduleVisitor from '../utils/moduleVisitor'
import docsUrl from '../docsUrl'
import { moduleVisitor } from '../utils/moduleVisitor'
import { docsUrl } from '../docs-url'

const DEFAULT_MAX = 10
const DEFAULT_IGNORE_TYPE_IMPORTS = false
Expand Down
2 changes: 1 addition & 1 deletion src/rules/named.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as path from 'path'
import Exports from '../ExportMap'
import docsUrl from '../docsUrl'
import { docsUrl } from '../docs-url'

module.exports = {
meta: {
Expand Down
4 changes: 2 additions & 2 deletions src/rules/namespace.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import declaredScope from '../utils/declaredScope'
import Exports from '../ExportMap'
import importDeclaration from '../importDeclaration'
import docsUrl from '../docsUrl'
import { importDeclaration } from '../import-declaration'
import { docsUrl } from '../docs-url'

function processBodyStatement(context, namespaces, declaration) {
if (declaration.type !== 'ImportDeclaration') {
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 @@ -4,7 +4,7 @@
*/

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

import debug from 'debug'
const log = debug('eslint-plugin-import-x:rules:newline-after-import')
Expand Down
4 changes: 2 additions & 2 deletions src/rules/no-absolute-path.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'path'
import moduleVisitor, { makeOptionsSchema } from '../utils/moduleVisitor'
import { moduleVisitor, makeOptionsSchema } from '../utils/moduleVisitor'
import { isAbsolute } from '../core/importType'
import docsUrl from '../docsUrl'
import { docsUrl } from '../docs-url'

module.exports = {
meta: {
Expand Down
2 changes: 1 addition & 1 deletion src/rules/no-amd.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @author Jamund Ferguson
*/

import docsUrl from '../docsUrl'
import { docsUrl } from '../docs-url'

//------------------------------------------------------------------------------
// Rule Definition
Expand Down
2 changes: 1 addition & 1 deletion src/rules/no-anonymous-default-export.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @author Duncan Beevers
*/

import docsUrl from '../docsUrl'
import { docsUrl } from '../docs-url'

const hasOwn = Function.bind.bind(Function.prototype.call)(
Object.prototype.hasOwnProperty,
Expand Down
2 changes: 1 addition & 1 deletion src/rules/no-commonjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @author Jamund Ferguson
*/

import docsUrl from '../docsUrl'
import { docsUrl } from '../docs-url'

const EXPORT_MESSAGE = 'Expected "export" or "export default"'
const IMPORT_MESSAGE = 'Expected "import" instead of "require()"'
Expand Down
6 changes: 3 additions & 3 deletions src/rules/no-cycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
* @author Ben Mosher
*/

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

const traversed = new Set()

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

module.exports = {
meta: {
Expand Down
2 changes: 1 addition & 1 deletion src/rules/no-deprecated.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import declaredScope from '../utils/declaredScope'
import Exports from '../ExportMap'
import docsUrl from '../docsUrl'
import { docsUrl } from '../docs-url'

function message(deprecation) {
return `Deprecated${deprecation.description ? `: ${deprecation.description}` : '.'}`
Expand Down
4 changes: 2 additions & 2 deletions src/rules/no-duplicates.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import resolve from '../utils/resolve'
import { resolve } from '../utils/resolve'
import semver from 'semver'

import docsUrl from '../docsUrl'
import { docsUrl } from '../docs-url'

let typescriptPkg
try {
Expand Down
2 changes: 1 addition & 1 deletion src/rules/no-dynamic-require.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import docsUrl from '../docsUrl'
import { docsUrl } from '../docs-url'

function isRequire(node) {
return (
Expand Down
2 changes: 1 addition & 1 deletion src/rules/no-empty-named-blocks.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import docsUrl from '../docsUrl'
import { docsUrl } from '../docs-url'

function getEmptyBlockRange(tokens, index) {
const token = tokens[index]
Expand Down
Loading

0 comments on commit 8dbd9b4

Please sign in to comment.