Skip to content

Commit

Permalink
Implement support for relative paths in config globs
Browse files Browse the repository at this point in the history
  • Loading branch information
daniilsapa committed Sep 7, 2024
1 parent 065d037 commit 85e1e07
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
5 changes: 3 additions & 2 deletions packages/steiger/src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node

import { resolve, relative } from 'node:path'
import { resolve, relative, sep } from 'node:path'
import * as process from 'node:process'
import yargs from 'yargs'
import prexit from 'prexit'
Expand Down Expand Up @@ -56,8 +56,9 @@ const { config, filepath } = (await cosmiconfig('steiger').search()) ?? { config
const defaultConfig = fsd.configs.recommended

try {
const configLocationDirectory = filepath ? filepath.split(sep).slice(0, -1).join(sep) : ''
// use FSD recommended config as a default
processConfiguration(config || defaultConfig)
processConfiguration(config || defaultConfig, configLocationDirectory)
} catch (err) {
if (filepath !== undefined) {
console.error(
Expand Down
24 changes: 21 additions & 3 deletions packages/steiger/src/models/config/create-rule-instructions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { reduce, flatten, filter, pipe, map } from 'ramda'

import { RuleInstructions } from './types'
import { getOptions, getSeverity, isConfigObject } from './raw-config'
import { sep } from 'node:path'

function createEmptyInstructions(): RuleInstructions {
return {
Expand All @@ -28,7 +29,24 @@ const preCreateRuleInstructions: (l: Config) => Record<string, RuleInstructions>
),
)

export default function createRuleInstructions(config: Config): Record<string, RuleInstructions> {
function convertRelativeGlobsToAbsolute(rootPath: string, globs: Array<string>) {
function composeAbsolutePath(root: string, glob: string) {
// Remove '/'. The root has platform-specific separators
const segmentsOfRoot = root.slice(1).split(sep)

// Remove './' from the beginning of the glob. Globs always have '/' as separators
const segmentsOfGlob = glob.slice(2).split('/')

return `/${[...segmentsOfRoot, ...segmentsOfGlob].join('/')}`
}

return globs.map((glob) => (glob.startsWith('.') && rootPath ? composeAbsolutePath(rootPath, glob) : glob))
}

export default function createRuleInstructions(
config: Config,
configLocationPath: string,
): Record<string, RuleInstructions> {
const ruleNameToInstructions: Record<string, RuleInstructions> = preCreateRuleInstructions(config)

return config.reduce((acc: Record<string, RuleInstructions>, item) => {
Expand All @@ -43,8 +61,8 @@ export default function createRuleInstructions(config: Config): Record<string, R

acc[ruleName].globGroups.push({
severity: getSeverity(severityOrTuple),
files: item.files ?? [],
ignores: item.ignores ?? [],
files: item.files ? convertRelativeGlobsToAbsolute(configLocationPath, item.files) : [],
ignores: item.ignores ? convertRelativeGlobsToAbsolute(configLocationPath, item.ignores) : [],
})
},
)
Expand Down
4 changes: 2 additions & 2 deletions packages/steiger/src/models/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ export const $enabledRules = combine($ruleInstructions, $plugins, (ruleInstructi
return allRules.filter((rule) => rulesThatHaveInstructions.includes(rule.name))
})

export function processConfiguration(rawConfig: Config) {
export function processConfiguration(rawConfig: Config, configLocationFolder: string) {
const validationScheme = buildValidationScheme(rawConfig)
const validatedConfig = validationScheme.parse(rawConfig)

const plugins = rawConfig.filter(isPlugin)
const ruleInstructions = createRuleInstructions(validatedConfig)
const ruleInstructions = createRuleInstructions(validatedConfig, configLocationFolder)

setPlugins(plugins)
setGlobalIgnores(rawConfig.filter(isGlobalIgnore))
Expand Down

0 comments on commit 85e1e07

Please sign in to comment.