monorail
helpers

helpers

Built-in helpers for making custom plugins more robust. The "helpers" are the third parameter passed to a custom plugin's function.

any

Use this helper to test a regex that matches against any single incidence on any line

Usage

const plugin = {
  name: 'get-const',
  fn: (state, file, { any }) => any(/const/)
}
export default plugin

lines

Use this helper to test a regex that matches and filters against every line

Usage

export default {
  name: 'unexported-consts',
  fn: (state, file, { lines }) => lines(/^const/)
}

line

Use this helper to test a regex that matches and finds the first matching line

Usage

export default {
  name: 'exported-default',
  fn: (state, file, { line }) => line(/export default/g)
}

lastLine

Use this helper to test a regex that matches and finds the last matching line

Usage

export default {
  name: 'exported-last',
  fn: (state, file, { lastLine }) => lastLine(/export/g)
}

between

Use this helper to select content between two repeating regular expressions

Usage

export default {
  name: 'expanded-imports',
  fn: (state, file, { between }) => between(/^import/, /from (.*)/)
}

reduce

Use this helper to easily reduce over all lines and aggregate a value

Usage

const plugin = {
  name: 'select-specifics',
  fn: (state, file, { reduce }) => reduce((agg, [line, content]) => content.length > 10 : agg.concat(content) : agg, [])
}
export default plugin

selectAll

Use this helper to select all content between two repeating regular expressions

Usage

export default {
  name: 'expanded-imports',
  fn: (state, file, { selectAll }) => selectAll(/^import/, /from (.*)/)
}

filter

Use this helper to easily filter all lines related to a given regular expression

Usage

const plugin = {
  name: 'keyword-match',
  fn: (state, file, { config, filter }) => filter(
    config?.keyword ?? /monorail/
  )
}
export default plugin

config

The config value is patched in so that it is easier to pass values from the rulefile configuration to the plugins themselves, scoped to the plugin name.

Usage

const plugin = {
  name: 'keyword-match',
  fn: (state, file, { filter, config }) => filter(
    config?.keyword ?? /monorail/
  )
}
export default plugin

Tools for Monorepos