Skip to content

Commit

Permalink
馃憤馃徏 improve: stylelint --editor behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
kellymears committed Jul 21, 2023
1 parent f0da14d commit 684a7b3
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 46 deletions.
99 changes: 54 additions & 45 deletions sources/@roots/bud-compiler/src/service.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import {bind} from '@roots/bud-support/decorators/bind'
import {BudError, type BudErrorClass} from '@roots/bud-support/errors'
import {duration} from '@roots/bud-support/human-readable'
import {render} from '@roots/bud-support/ink'
import isNull from '@roots/bud-support/lodash/isNull'
import isString from '@roots/bud-support/lodash/isString'
import stripAnsi from '@roots/bud-support/strip-ansi'
import webpack from '@roots/bud-support/webpack'

Expand Down Expand Up @@ -210,59 +212,66 @@ export class Compiler extends Service implements BudCompiler {
if (!errors || !errors.length) return []

try {
const parseError = (
error: StatsError,
): ErrorWithSourceFile | StatsError => {
let file: SourceFile[`file`] | undefined
let module: undefined | Webpack.StatsModule

const ident = error.moduleId ?? error.moduleName

/**
* In a perfect world webpack plugins would use the
* `nameForCondition` property to identify the module.
*/
if (ident) {
module = this.compilationStats.children
.flatMap(child => child?.modules)
.find(module => [module?.id, module?.name].includes(ident))
return errors
?.map((error: StatsError): ErrorWithSourceFile | StatsError => {
let file: SourceFile[`file`] | undefined
let module: undefined | Webpack.StatsModule

const ident = error.moduleId ?? error.moduleName

/**
* In a perfect world webpack plugins would use the
* `nameForCondition` property to identify the module.
*/
if (ident) {
module = this.compilationStats.children
.flatMap(child => child?.modules)
.find(module => [module?.id, module?.name].includes(ident))
}

/**
* If the module is not found, we try to parse the error message
*/
} else {
const styleError = error.message
.split(`\n`)?.[1]
?.match(/file:\/\/(.*)\x07(.*)\x1B]8;;/)

if (styleError?.[1]) {
module = {
name: styleError[2] ?? styleError[1],
nameForCondition: styleError[1],
}
if (!ident && error.message?.includes(`[stylelint]`)) {
// try to get the origin of the stylelint error,
// which is contained in the second line of the error message
const unparsedOrigin = error.message?.split(`\n`)?.[1]

// if the origin is not a string or too long, we return the error as-is
if (!isString(unparsedOrigin) || unparsedOrigin.length > 100)
return error

// extract absolute path and context relative name of module
const styleError = unparsedOrigin.match(
/file:\/\/(.*)\x07(.*)\x1B]8;;/,
)
if (isNull(styleError)) return error

// get parts of matched error
const [, file, name] = styleError
// return enriched error
return {...error, file, name, nameForCondition: file}
}
}

/**
* If the module is still not found, we return the error as-is
*/
if (!module) return error

/**
* We'll prefer the `nameForCondition` property if it exists,
* otherwise we'll use the `name` property.
*/
if (module.nameForCondition) {
file = module.nameForCondition
} else if (module.name) {
file = this.app.path(`@src`, module.name)
}
/**
* If the module is still not found, we return the error as-is
*/
if (!module) return error

const name = module.name ?? error.name ?? `error`
return {...error, file, name}
}
/**
* We'll prefer the `nameForCondition` property if it exists,
* otherwise we'll use the `name` property.
*/
if (module.nameForCondition) {
file = module.nameForCondition
} else if (module.name) {
file = this.app.path(`@src`, module.name)
}

return errors?.map(parseError).filter(Boolean)
const name = module.name ?? error.name ?? `error`
return {...error, file, name}
})
.filter(Boolean)
} catch (error) {
this.logger.warn(
`Problem parsing errors. This probably won't break anything but please report it: https://github.com/roots/bud/issues/new`,
Expand Down
1 change: 0 additions & 1 deletion sources/@roots/bud-framework/src/notifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ export class Notifier {
*/
public openEditor(input: Array<string> | string) {
if (!this.openEditorEnabled) return

if (!input || isEmpty(input)) return

logger.scope(`notifier`, `openEditor`).log(`input received`, input)
Expand Down

0 comments on commit 684a7b3

Please sign in to comment.