Skip to content

Commit

Permalink
refactor: do not use Symbol in configuration mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
iiroj committed May 31, 2022
1 parent 641d1c2 commit 1f06dd0
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 17 deletions.
4 changes: 1 addition & 3 deletions lib/groupFilesByConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ export const groupFilesByConfig = async ({ configs, files, singleConfigMode }) =
const filesByConfig = {}

/** Configs are sorted deepest first by `searchConfigs` */
for (const filepath of Reflect.ownKeys(configs)) {
const config = configs[filepath]

for (const [filepath, config] of Object.entries(configs)) {
/** When passed an explicit config object via the Node.js API‚ or an explicit path, skip logic */
if (singleConfigMode) {
filesByConfig[filepath] = { config, files }
Expand Down
13 changes: 4 additions & 9 deletions lib/runAll.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import {
restoreUnstagedChangesSkipped,
} from './state.js'
import { GitRepoError, GetStagedFilesError, GitError, ConfigNotFoundError } from './symbols.js'
import { ConfigObjectSymbol, searchConfigs } from './searchConfigs.js'
import { searchConfigs } from './searchConfigs.js'

const debugLog = debug('lint-staged:runAll')

Expand Down Expand Up @@ -125,7 +125,7 @@ export const runAll = async (
}

const foundConfigs = await searchConfigs({ configObject, configPath, cwd, gitDir }, logger)
const numberOfConfigs = Reflect.ownKeys(foundConfigs).length
const numberOfConfigs = Object.keys(foundConfigs).length

// Throw if no configurations were found
if (numberOfConfigs === 0) {
Expand Down Expand Up @@ -157,13 +157,8 @@ export const runAll = async (
// Set of all staged files that matched a task glob. Values in a set are unique.
const matchedFiles = new Set()

for (const configPath of Reflect.ownKeys(filesByConfig)) {
const { config, files } = filesByConfig[configPath]

const configName =
configPath === ConfigObjectSymbol
? 'Config object'
: normalize(path.relative(cwd, configPath))
for (const [configPath, { config, files }] of Object.entries(filesByConfig)) {
const configName = configPath ? normalize(path.relative(cwd, configPath)) : 'Config object'

const stagedFileChunks = chunkFiles({ baseDir: gitDir, files, maxArgLength, relative })

Expand Down
4 changes: 1 addition & 3 deletions lib/searchConfigs.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ const sortDeepestParth = (a, b) => (numberOfLevels(a) > numberOfLevels(b) ? -1 :

const isInsideDirectory = (dir) => (file) => file.startsWith(normalize(dir))

export const ConfigObjectSymbol = Symbol()

/**
* Search all config files from the git repository, preferring those inside `cwd`.
*
Expand All @@ -46,7 +44,7 @@ export const searchConfigs = async (
if (configObject) {
debugLog('Using single direct configuration object...')

return { [ConfigObjectSymbol]: validateConfig(configObject, 'config object', logger) }
return { '': validateConfig(configObject, 'config object', logger) }
}

// Use only explicit config path instead of discovering multiple
Expand Down
4 changes: 2 additions & 2 deletions test/searchConfigs.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import normalize from 'normalize-path'

import { execGit } from '../lib/execGit.js'
import { loadConfig } from '../lib/loadConfig.js'
import { ConfigObjectSymbol, searchConfigs } from '../lib/searchConfigs.js'
import { searchConfigs } from '../lib/searchConfigs.js'

jest.mock('../lib/resolveConfig', () => ({
/** Unfortunately necessary due to non-ESM tests. */
Expand Down Expand Up @@ -47,7 +47,7 @@ describe('searchConfigs', () => {

it('should return config for valid config object', async () => {
await expect(searchConfigs({ configObject: { '*.js': 'eslint' } })).resolves.toEqual({
[ConfigObjectSymbol]: { '*.js': 'eslint' },
'': { '*.js': 'eslint' },
})
})

Expand Down

0 comments on commit 1f06dd0

Please sign in to comment.