Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed May 29, 2022
1 parent 0a5bb38 commit 7e6b888
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 19 deletions.
37 changes: 36 additions & 1 deletion src/config/normalize/lib/call.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,47 @@ import { inspect } from 'util'

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

// Call `keyword.test()`
export const callTest = async function (test, input, info) {
return await callFunc({ func: test, input, info, hasInput: true, test })
}

// Call `keyword.normalize()`
export const callNormalize = async function (normalize, definition, info) {
const func = () => normalize(definition)
return await callFunc({ func, info, hasInput: false })
}

// Call definition function
export const callDefinition = async function ({
definition,
input,
info,
hasInput,
test,
}) {
return await callFunc({ func: definition, input, info, hasInput, test })
}

// Call `keyword.main()`
export const callMain = async function ({
main,
definition,
input,
info,
hasInput,
test,
}) {
const func = main.bind(undefined, definition)
return await callFunc({ func, input, info, hasInput, test })
}

// Call a function from `test()`, `main()` or a definition.
// They are all:
// - Optionally async
// - Called with same arguments
// - Error handled
export const callFunc = async function ({
const callFunc = async function ({
func,
input,
info: { originalName },
Expand Down
12 changes: 4 additions & 8 deletions src/config/normalize/lib/keywords/definition.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { callFunc } from '../call.js'
import { callDefinition, callNormalize } from '../call.js'

// The property name is defined by `name`.
// `aliases` can be defined to either:
Expand All @@ -16,15 +16,15 @@ export const getDefinition = function (rule, name, aliases) {
}

// Call definition when it is a function
export const callDefinition = async function ({
export const callDefinitionFunc = async function ({
definition,
input,
info,
hasInput,
test,
}) {
return typeof definition === 'function'
? await callFunc({ func: definition, input, info, hasInput, test })
? await callDefinition({ definition, input, info, hasInput, test })
: definition
}

Expand All @@ -36,9 +36,5 @@ export const normalizeDefinition = async function (
) {
return normalize === undefined
? definition
: await callFunc({
func: () => normalize(definition),
info,
hasInput: false,
})
: await callNormalize(normalize, definition, info)
}
11 changes: 6 additions & 5 deletions src/config/normalize/lib/keywords/main.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { callFunc } from '../call.js'
import { callMain } from '../call.js'

import {
getDefinition,
callDefinition,
callDefinitionFunc,
normalizeDefinition,
} from './definition.js'
import { applyReturnValue } from './return.js'
Expand Down Expand Up @@ -99,7 +99,7 @@ const applyKeyword = async function ({
return state
}

const definitionA = await callDefinition({
const definitionA = await callDefinitionFunc({
definition,
input,
info,
Expand All @@ -112,8 +112,9 @@ const applyKeyword = async function ({
}

const definitionB = await normalizeDefinition(definitionA, normalize, info)
const returnValue = await callFunc({
func: main.bind(undefined, definitionB),
const returnValue = await callMain({
main,
definition: definitionB,
input,
info,
hasInput,
Expand Down
7 changes: 2 additions & 5 deletions src/config/normalize/lib/keywords/skip.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { callFunc } from '../call.js'
import { callTest } from '../call.js'

// `undefined` definitions are always skipped because:
// - It allows any keyword to be disabled by setting `definition` to
Expand Down Expand Up @@ -35,10 +35,7 @@ export const shouldSkipKeyword = async function ({
// need to check the input during `test()` but should not pass it during
// `main()` nor the definition function
const hasSkippedTest = async function (test, input, info) {
return (
test !== undefined &&
!(await callFunc({ func: test, input, info, hasInput: true, test }))
)
return test !== undefined && !(await callTest(test, input, info))
}

// Function definitions returning `undefined` are skipped, unless
Expand Down

0 comments on commit 7e6b888

Please sign in to comment.