Skip to content

Commit

Permalink
Fix linting errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mrginglymus committed Dec 19, 2024
1 parent f1c2b4d commit 37fa82d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 27 deletions.
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import stage0 from './config/stage-0'
import typescript from './config/typescript'
import warnings from './config/warnings'
// rules
import { createNodeResolver } from './node-resolver'
import consistentTypeSpecifierStyle from './rules/consistent-type-specifier-style'
import default_ from './rules/default'
import dynamicImportChunkname from './rules/dynamic-import-chunkname'
Expand Down Expand Up @@ -72,7 +73,6 @@ import type {
PluginFlatConfig,
} from './types'
import { importXResolverCompat } from './utils'
import { createNodeResolver } from './node-resolver'

const rules = {
'no-unresolved': noUnresolved,
Expand Down Expand Up @@ -184,5 +184,5 @@ export = {
flatConfigs,
rules,
importXResolverCompat,
createNodeResolver
createNodeResolver,
}
41 changes: 20 additions & 21 deletions src/node-resolver.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
import { ResolverFactory, CachedInputFileSystem, type ResolveOptions } from 'enhanced-resolve';
import fs from 'node:fs';
import type { NewResolver } from './types';
import { isBuiltin } from 'node:module';
import { dirname } from 'node:path';
import fs from 'node:fs'
import { isBuiltin } from 'node:module'
import { dirname } from 'node:path'

Check failure on line 3 in src/node-resolver.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 20 and ESLint 8.56 on ubuntu-latest

Use default import for module `node:path`

Check failure on line 3 in src/node-resolver.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 20 and ESLint 8 on ubuntu-latest

Use default import for module `node:path`

Check failure on line 3 in src/node-resolver.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 20 and ESLint 9 on ubuntu-latest

Use default import for module `node:path`

interface NodeResolverOptions extends Omit<ResolveOptions, 'useSyncFileSystemCalls'> {
import { ResolverFactory, CachedInputFileSystem } from 'enhanced-resolve'
import type { ResolveOptions } from 'enhanced-resolve'

import type { NewResolver } from './types'

type NodeResolverOptions = {
/**
* The allowed extensions the resolver will attempt to find when resolving a module
* @type {string[] | undefined}
* @default ['.mjs', '.cjs', '.js', '.json', '.node']
*/
extensions?: string[];
extensions?: string[]
/**
* The import conditions the resolver will used when reading the exports map from "package.json"
* @type {string[] | undefined}
* @default ['default', 'module', 'import', 'require']
*/
conditionNames?: string[];
}
conditionNames?: string[]
} & Omit<ResolveOptions, 'useSyncFileSystemCalls'>

export function createNodeResolver({
extensions = ['.mjs', '.cjs', '.js', '.json', '.node'],
Expand All @@ -34,7 +37,7 @@ export function createNodeResolver({
conditionNames,
useSyncFileSystemCalls: true,
...restOptions,
});
})

// shared context across all resolve calls

Expand All @@ -43,26 +46,22 @@ export function createNodeResolver({
name: 'eslint-plugin-import-x built-in node resolver',
resolve: (modulePath, sourceFile) => {
if (isBuiltin(modulePath)) {
return { found: true, path: null };
return { found: true, path: null }
}

if (modulePath.startsWith('data:')) {
return { found: true, path: null };
return { found: true, path: null }
}

try {
const path = resolver.resolveSync(
{},
dirname(sourceFile),
modulePath
);
const path = resolver.resolveSync({}, dirname(sourceFile), modulePath)
if (path) {
return { found: true, path };
return { found: true, path }
}
return { found: false };
return { found: false }
} catch {
return { found: false };
return { found: false }
}
}
},
}
}
12 changes: 8 additions & 4 deletions test/node-resolver.spec.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import path from 'node:path'
import { cwd } from 'node:process'
import { createNodeResolver } from '../src/node-resolver';

import { createNodeResolver } from '../src/node-resolver'

const resolver = createNodeResolver()

function expectResolve(source: string, expected: boolean | string) {
it(`${source} => ${expected}`, () => {
try {
console.log({ source, expected, requireResolve: require.resolve(source, { paths: [__dirname] }) })

console.log({
source,
expected,
requireResolve: require.resolve(source, { paths: [__dirname] }),
})
} catch {
console.log({ source, expected, requireResolve: null })
}
const result = resolver.resolve(source, __filename);
const result = resolver.resolve(source, __filename)
console.log({ source, expected, result })

if (typeof expected === 'string') {
Expand Down

0 comments on commit 37fa82d

Please sign in to comment.