Skip to content

Commit

Permalink
Rename value to input
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed May 29, 2022
1 parent af0406f commit d74c1ba
Show file tree
Hide file tree
Showing 19 changed files with 150 additions and 167 deletions.
50 changes: 23 additions & 27 deletions src/config/normalize/lib/apply.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { remove, set, get } from 'wild-wild-path'

import {
callValueFunc,
callNoValueFunc,
callUndefinedValueFunc,
} from './call.js'
import { callInputFunc, callNoInputFunc, callConstraintFunc } from './call.js'
import { validateAndModify } from './modify.js'

// Apply a rule on a specific property
Expand All @@ -24,30 +20,30 @@ export const applyRule = async function ({
transform,
rename,
},
value,
input,
config,
moves,
warnings,
opts,
}) {
if (await againstPick(value, pick, opts)) {
if (await againstPick(input, pick, opts)) {
const configA = remove(config, opts.funcOpts.path)
return { config: configA, warnings, moves }
}

if (await againstCondition(value, condition, opts)) {
if (await againstCondition(input, condition, opts)) {
return { config, warnings, moves }
}

const configB = await computeValue(config, compute, opts)
const configC = await addDefaultValue(configB, defaultValue, opts)
const valueA = get(configC, opts.funcOpts.path)
const configB = await computeInput(config, compute, opts)
const configC = await addDefault(configB, defaultValue, opts)
const inputA = get(configC, opts.funcOpts.path)
const {
config: configD,
warnings: warningsA,
moves: movesA,
} = await validateAndModify({
value: valueA,
input: inputA,
config: configC,
required,
schema,
Expand All @@ -64,41 +60,41 @@ export const applyRule = async function ({
return { config: configD, warnings: warningsA, moves: movesA }
}

// Apply `pick[(value, opts)]` which omits the current value if `false` is
// Apply `pick[(input, opts)]` which omits the current input if `false` is
// returned. It also skips the current rule.
// For example, this is useful when several commands share some properties but
// not all.
const againstPick = async function (value, pick, opts) {
return pick !== undefined && !(await callValueFunc(pick, value, opts))
const againstPick = async function (input, pick, opts) {
return pick !== undefined && !(await callInputFunc(pick, input, opts))
}

// Apply `condition[(value, opts)]` which skips the current rule if `false`
// Apply `condition[(input, opts)]` which skips the current rule if `false`
// is returned.
const againstCondition = async function (value, condition, opts) {
const againstCondition = async function (input, condition, opts) {
return (
condition !== undefined && !(await callValueFunc(condition, value, opts))
condition !== undefined && !(await callInputFunc(condition, input, opts))
)
}

// Apply `compute[(opts)]` which sets a value from the system, instead of the
// Apply `compute[(opts)]` which sets an input from the system, instead of the
// user
const computeValue = async function (config, compute, opts) {
const computeInput = async function (config, compute, opts) {
if (compute === undefined) {
return config
}

const value = await callNoValueFunc(compute, opts)
return set(config, opts.funcOpts.path, value)
const input = await callNoInputFunc(compute, opts)
return set(config, opts.funcOpts.path, input)
}

// Apply `default[(opts)]` which assigns a default value
const addDefaultValue = async function (config, defaultValue, opts) {
const value = get(config, opts.funcOpts.path)
const addDefault = async function (config, defaultValue, opts) {
const input = get(config, opts.funcOpts.path)

if (value !== undefined) {
if (input !== undefined) {
return config
}

const valueA = await callUndefinedValueFunc(defaultValue, opts)
return set(config, opts.funcOpts.path, valueA)
const inputA = await callConstraintFunc(defaultValue, opts)
return set(config, opts.funcOpts.path, inputA)
}
26 changes: 13 additions & 13 deletions src/config/normalize/lib/call.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@ import { addPrefix } from './prefix.js'
import { handleValidateError } from './validate.js'

// Most rule methods follow the same patterns:
// - Called with `value` and `opts`
// - Called with `input` and `opts`
// - Optionally async
export const callValueFunc = async function (userFunc, value, opts) {
export const callInputFunc = async function (userFunc, input, opts) {
try {
return typeof userFunc === 'function'
? await callUserFunc(userFunc.bind(undefined, value), opts)
? await callUserFunc(userFunc.bind(undefined, input), opts)
: userFunc
} catch (error) {
const errorA = handleError(error, opts)
const errorB = addCurrentValue(errorA, value)
const errorB = addCurrentValue(errorA, input)
throw await addExampleValue(errorB, opts)
}
}

// Some methods are not called with any `value` but their logic requires knowing
// Some methods are not called with any `input` but their logic requires knowing
// whether it is undefined
export const callUndefinedValueFunc = async function (userFunc, opts) {
export const callConstraintFunc = async function (userFunc, opts) {
try {
return typeof userFunc === 'function'
? await callUserFunc(userFunc, opts)
Expand All @@ -34,8 +34,8 @@ export const callUndefinedValueFunc = async function (userFunc, opts) {
}
}

// Some methods are not called with any value
export const callNoValueFunc = async function (userFunc, opts) {
// Some methods are not called with any input
export const callNoInputFunc = async function (userFunc, opts) {
try {
return typeof userFunc === 'function'
? await callUserFunc(userFunc, opts)
Expand All @@ -50,21 +50,21 @@ const handleError = function (error, opts) {
return addPrefix(error, opts)
}

// Add the current value as error suffix
const addCurrentValue = function (error, value) {
// Add the current input value as error suffix
const addCurrentValue = function (error, input) {
return error.validation
? wrapErrorValue(error, 'Current value', value)
? wrapErrorValue(error, 'Current value', input)
: error
}

// Add an example value as error suffix, as provided by `example[(opts)]`
// Add an example input value as error suffix, as provided by `example[(opts)]`
const addExampleValue = async function (error, opts) {
if (opts.example === undefined || !error.validation) {
return error
}

try {
const exampleValue = await callNoValueFunc(opts.example, opts)
const exampleValue = await callNoInputFunc(opts.example, opts)
return wrapErrorValue(error, 'Example value', exampleValue)
} catch (error_) {
return wrapError(error_, 'Invalid "example":')
Expand Down
14 changes: 7 additions & 7 deletions src/config/normalize/lib/cwd.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { isDirectory } from 'path-type'

import { wrapError } from '../../../error/wrap.js'

import { callNoValueFunc } from './call.js'
import { callNoInputFunc } from './call.js'
import { validateDefinedString } from './type.js'

// A `cwd[(opts)]` option can be specified to customize the `cwd`.
Expand All @@ -18,15 +18,15 @@ export const computeCwd = async function (cwd, opts) {

const getCwd = async function ({ cwd = DEFAULT_CWD, opts }) {
const cwdA = await callCwdFunc(cwd, opts)
await callNoValueFunc(checkCwd.bind(undefined, cwdA), opts)
await callNoInputFunc(checkCwd.bind(undefined, cwdA), opts)
return resolve(cwdA)
}

const DEFAULT_CWD = '.'

const callCwdFunc = async function (func, opts) {
try {
return await callNoValueFunc(func, opts)
return await callNoInputFunc(func, opts)
} catch (error) {
throw wrapError(error, 'Invalid "cwd" function:')
}
Expand All @@ -43,14 +43,14 @@ const checkCwd = async function (cwd) {
}
}

const validateFileExists = async function (value) {
if (!(await pathExists(value))) {
const validateFileExists = async function (cwd) {
if (!(await pathExists(cwd))) {
throw new Error('must be an existing file.')
}
}

const validateDirectory = async function (value) {
if (!(await isDirectory(value))) {
const validateDirectory = async function (cwd) {
if (!(await isDirectory(cwd))) {
throw new Error('must be a directory.')
}
}
4 changes: 2 additions & 2 deletions src/config/normalize/lib/entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getOpts } from './opts.js'
// Apply rule for a specific entry
export const applyEntryRule = async function (
{ config, moves, warnings },
{ value, namePath, rule, rule: { example }, context, cwd, prefix, parent },
{ input, namePath, rule, rule: { example }, context, cwd, prefix, parent },
) {
const opts = await getOpts({
namePath,
Expand All @@ -20,6 +20,6 @@ export const applyEntryRule = async function (
config: configA,
moves: movesA,
warnings: warningsA,
} = await applyRule({ rule, value, config, moves, warnings, opts })
} = await applyRule({ rule, input, config, moves, warnings, opts })
return { config: configA, moves: movesA, warnings: warningsA }
}
6 changes: 3 additions & 3 deletions src/config/normalize/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ export const normalizeConfigProps = async function (
applyRuleDeep(memo, { rule, context, cwd, prefix, parent }),
{ config, moves: [], warnings: [] },
)
const value = cleanObject(configA)
const configB = cleanObject(configA)
logWarnings(warnings, soft)
return { value, warnings }
return { value: configB, warnings }
} catch (error) {
handleError(error, soft)
return { error, warnings: [] }
Expand All @@ -55,7 +55,7 @@ const applyRuleDeep = async function (
entries,
(memo, { value, path: namePathA }) =>
applyEntryRule(memo, {
value,
input: value,
namePath: namePathA,
rule,
context,
Expand Down
26 changes: 9 additions & 17 deletions src/config/normalize/lib/modify.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import { callUndefinedValueFunc } from './call.js'
import { callConstraintFunc } from './call.js'
import { performPlugins } from './plugin.js'

// Once the initial value has been computed, apply validation and transforms,
// unless the value is `undefined`.

// Once the initial input has been computed, apply validation and transforms,
// unless the input is `undefined`.
export const validateAndModify = async function ({
value,
input,
required,
config,
moves,
warnings,
opts,
...rule
}) {
if (value === undefined) {
if (input === undefined) {
await validateRequired(required, opts)
return { config, warnings, moves }
}
Expand All @@ -22,21 +21,14 @@ export const validateAndModify = async function ({
config: configA,
warnings: warningsA,
moves: movesA,
} = await performPlugins({
rule,
value,
config,
moves,
warnings,
opts,
})
} = await performPlugins({ rule, input, config, moves, warnings, opts })
return { config: configA, warnings: warningsA, moves: movesA }
}

// Apply `required[(opts)]` which throws if `true` and value is `undefined`
// Apply `required[(opts)]` which throws if `true` and input is `undefined`
const validateRequired = async function (required, opts) {
if (await callUndefinedValueFunc(required, opts)) {
await callUndefinedValueFunc(throwRequired, opts)
if (await callConstraintFunc(required, opts)) {
await callConstraintFunc(throwRequired, opts)
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/config/normalize/lib/opts.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { serializePath } from 'wild-wild-parser'

import { wrapError } from '../../../error/wrap.js'

import { callNoValueFunc } from './call.js'
import { callNoInputFunc } from './call.js'
import { computeCwd } from './cwd.js'
import { applyMoves } from './move.js'
import { computeParent } from './parent.js'
Expand Down Expand Up @@ -47,7 +47,7 @@ const computePrefix = async function (prefix, opts) {
}

const addPrefix = async function (prefix, opts) {
const prefixA = await callNoValueFunc(prefix, opts)
const prefixA = await callNoInputFunc(prefix, opts)

if (prefixA === undefined) {
return opts
Expand Down
4 changes: 2 additions & 2 deletions src/config/normalize/lib/parent.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { normalizePath, serializePath } from 'wild-wild-parser'

import { wrapError } from '../../../error/wrap.js'

import { callNoValueFunc } from './call.js'
import { callNoInputFunc } from './call.js'

// `originalName|Path` are like `name|path` except:
// - They are prepended with `opts.parent`
Expand Down Expand Up @@ -34,7 +34,7 @@ const getParent = async function (parent, opts) {
// By default, there are none.
// `normalizePath()` might throw if `parent` contains syntax errors.
const appendParentToName = async function (parent, opts) {
const parentA = await callNoValueFunc(parent, opts)
const parentA = await callNoInputFunc(parent, opts)
const parentPath = normalizePath(parentA)
return [...parentPath, ...opts.funcOpts.originalPath]
}
Loading

0 comments on commit d74c1ba

Please sign in to comment.