Skip to content

Commit

Permalink
chore: enhance options
Browse files Browse the repository at this point in the history
  • Loading branch information
noootwo committed Nov 21, 2024
1 parent 49c2042 commit 6a21457
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 21 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,10 @@ AutoImport({
// Enable auto import by filename for default module exports under directories
defaultExportByFilename: false,

// Enable auto import the types under the directories
types: true,
// Options for scanning directories for auto import
dirsScanOptions: {
types: true // Enable auto import the types under the directories
},

// Auto import for module exports under directories
// by default it only scan one level of modules under the directory
Expand All @@ -286,11 +288,11 @@ AutoImport({

{
glob: './hooks',
types: true, // enable import the types
types: true // enable import the types
},
{
glob: './composables',
types: false, // If top level types importing enabled, just only disable this directory
types: false // If top level dirsScanOptions.types importing enabled, just only disable this directory
}
// ...
],
Expand Down
12 changes: 7 additions & 5 deletions src/core/ctx.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Import, InlinePreset } from 'unimport'
import type { BiomeLintrc, ESLintGlobalsPropValue, ESLintrc, ImportDir, ImportExtended, NormalizedImportDir, Options } from '../types'
import type { BiomeLintrc, ESLintGlobalsPropValue, ESLintrc, ImportExtended, NormalizedScanDir, Options, ScanDir } from '../types'
import { existsSync, promises as fs } from 'node:fs'
import { dirname, isAbsolute, join, relative, resolve } from 'node:path'
import process from 'node:process'
Expand All @@ -20,19 +20,19 @@ function resolveGlobsExclude(root: string, glob: string) {
return `${excludeReg.test(glob) ? '!' : ''}${resolve(root, glob.replace(excludeReg, ''))}`
}

export function normalizeImportDirs(dirs: (string | ImportDir)[], topLevelTypes = false, root = process.cwd()): NormalizedImportDir[] {
export function normalizeImportDirs(dirs: (string | ScanDir)[], topLevelTypes = false, root = process.cwd()): NormalizedScanDir[] {
return dirs.map((dir) => {
const isString = typeof dir === 'string'
const glob = slash(resolveGlobsExclude(root, join(isString ? dir : dir.glob, '*.{tsx,jsx,ts,js,mjs,cjs,mts,cts}')))
const types = isString ? topLevelTypes : (dir.types ?? false)
const types = isString ? topLevelTypes : (dir.types ?? topLevelTypes)
return {
glob,
types,
}
})
}

async function scanDirExports(dirs: NormalizedImportDir[], root: string) {
async function scanDirExports(dirs: NormalizedScanDir[], root: string) {
const dirPatterns = dirs.map(dir => dir.glob)
const result = await fg(dirPatterns, {
absolute: true,
Expand All @@ -53,11 +53,13 @@ export function createContext(options: Options = {}, root = process.cwd()) {

const {
dts: preferDTS = isPackageExists('typescript'),
types: topLevelTypes = false,
dirsScanOptions,
vueDirectives,
vueTemplate,
} = options

const topLevelTypes = dirsScanOptions?.types ?? false

const dirs = normalizeImportDirs(options.dirs || [], topLevelTypes, root)

const eslintrc: ESLintrc = options.eslintrc || {}
Expand Down
22 changes: 15 additions & 7 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,25 @@ export type Resolver = ResolverFunction | ResolverResultObject
*/
export type ImportsMap = Record<string, (string | ImportNameAlias)[]>


Check failure on line 54 in src/types.ts

View workflow job for this annotation

GitHub Actions / lint

More than 1 blank line not allowed
export interface ScanDirExportsOptions {
/**
* Register type exports
*
* @default true
*/
types?: boolean
}

/**
* Directory to search for import
*/
export interface ImportDir {
export interface ScanDir {
glob: string
types?: boolean
}

export type NormalizedImportDir = Required<ImportDir>
export type NormalizedScanDir = Required<ScanDir>

export type ESLintGlobalsPropValue = boolean | 'readonly' | 'readable' | 'writable' | 'writeable'

Expand Down Expand Up @@ -129,16 +139,14 @@ export interface Options {
injectAtEnd?: boolean

/**
* Auto import the types
*
* @default false
* Options for scanning directories for auto import
*/
types?: boolean
dirsScanOptions?: ScanDirExportsOptions

/**
* Path for directories to be auto imported
*/
dirs?: (string | ImportDir)[]
dirs?: (string | ScanDir)[]

/**
* Pass a custom function to resolve the component importing path from the component name.
Expand Down
10 changes: 5 additions & 5 deletions test/search.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('import the types from the dirs', () => {
it('should top level types enable work', async () => {
const ctx = createContext({
dts: false,
types: true,
dirsScanOptions: { types: true },
dirs: ['src/**'],
}, root)

Expand All @@ -53,7 +53,7 @@ describe('import the types from the dirs', () => {
it('should specific dirs types enable work', async () => {
const ctx = createContext({
dts: false,
types: false,
dirsScanOptions: { types: true },
dirs: [
{
glob: 'src/views',
Expand All @@ -72,11 +72,11 @@ describe('import the types from the dirs', () => {
it('should specific dirs types disable work', async () => {
const ctx = createContext({
dts: false,
types: true,
dirsScanOptions: { types: true },
dirs: [
'src/**',
'src/types',
{
glob: '!src/views',
glob: 'src/views',
types: false,
},
],
Expand Down

0 comments on commit 6a21457

Please sign in to comment.