Skip to content

Commit 83d144e

Browse files
author
dbale-altoros
committed
feat: config in multiple dirs
1 parent 204ed94 commit 83d144e

File tree

6 files changed

+651
-35
lines changed

6 files changed

+651
-35
lines changed

lib/config/config-file.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,28 @@ const applyExtends = (config, getter = configGetter) => {
8787
}, config)
8888
}
8989

90+
// NUEVO: Carga y fusiona todos los .solhint.json desde el archivo hasta la raíz
91+
92+
function loadConfigForFile(filePath, rootDir = process.cwd()) {
93+
let dir = path.dirname(path.resolve(filePath))
94+
let configs = []
95+
const configFileName = '.solhint.json'
96+
97+
while (true) {
98+
const configPath = path.join(dir, configFileName)
99+
if (fs.existsSync(configPath)) {
100+
configs.unshift(JSON.parse(fs.readFileSync(configPath, 'utf8')))
101+
}
102+
if (dir === rootDir || path.dirname(dir) === dir) break
103+
dir = path.dirname(dir)
104+
}
105+
// Combina configs, el más profundo sobrescribe
106+
return _.merge({}, ...configs)
107+
}
108+
90109
module.exports = {
91110
applyExtends,
92111
configGetter,
93112
loadConfig,
113+
loadConfigForFile, // Exporta la nueva función
94114
}

lib/index.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const parser = require('@solidity-parser/parser')
33
const glob = require('glob')
44
const ignore = require('ignore')
55
const astParents = require('ast-parents')
6-
const { applyExtends } = require('./config/config-file')
6+
const { applyExtends, loadConfigForFile } = require('./config/config-file')
77
const Reporter = require('./reporter')
88
const TreeListener = require('./tree-listener')
99
const checkers = require('./rules/index')
@@ -51,20 +51,21 @@ function processStr(inputStr, config = {}, fileName = '') {
5151
return reporter
5252
}
5353

54-
function processFile(file, config) {
55-
const report = processStr(fs.readFileSync(file).toString(), config, file)
54+
// Si se pasa config explícita, úsala. Si no, usa la jerárquica (.solhint.json)
55+
function processFile(file, config, rootDir = process.cwd()) {
56+
const finalConfig = config !== undefined ? config : loadConfigForFile(file, rootDir)
57+
const report = processStr(fs.readFileSync(file).toString(), finalConfig, file)
5658
report.file = file
57-
5859
return report
5960
}
6061

61-
function processPath(path, config) {
62-
const ignoreFilter = ignore({ allowRelativePaths: true }).add(config.excludedFiles)
63-
64-
const allFiles = glob.sync(path, { nodir: true })
62+
function processPath(pattern, config, rootDir = process.cwd()) {
63+
const ignoreFilter = ignore({ allowRelativePaths: true }).add(
64+
(config && config.excludedFiles) || []
65+
)
66+
const allFiles = glob.sync(pattern, { nodir: true })
6567
const files = ignoreFilter.filter(allFiles)
66-
67-
return files.map((curFile) => processFile(curFile, config))
68+
return files.map((curFile) => processFile(curFile, config, rootDir))
6869
}
6970

7071
module.exports = { processPath, processFile, processStr }

0 commit comments

Comments
 (0)