Skip to content

Commit

Permalink
Split files
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed May 29, 2022
1 parent e05f84a commit 8f4189d
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 42 deletions.
2 changes: 1 addition & 1 deletion src/config/normalize/lib/keywords/normalize/name.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { inspect } from 'util'
import isPlainObj from 'is-plain-obj'

import { KeywordError, DefinitionError } from '../../error.js'
import { CORE_PROPS_SET } from '../../rule.js'
import { CORE_PROPS_SET } from '../../rule/props.js'
import { BUILTIN_KEYWORDS } from '../list/main.js'

// Validate `keyword.name` and `keyword.aliases[*]`
Expand Down
2 changes: 1 addition & 1 deletion src/config/normalize/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { cleanObject } from '../../../utils/clean.js'
import { InputError, ErrorTypes } from './error.js'
import { getInfo } from './info.js'
import { applyKeywords } from './keywords/main.js'
import { normalizeRules } from './normalize.js'
import { normalizeOpts } from './options.js'
import { normalizeRules } from './rule/main.js'
import { logWarnings } from './warn.js'

// Validate and normalize a library's inputs.
Expand Down
3 changes: 2 additions & 1 deletion src/config/normalize/lib/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import isPlainObj from 'is-plain-obj'

import { DefinitionError } from './error.js'
import { normalizeKeywords } from './keywords/normalize/main.js'
import { getRuleProps, validateRuleProps } from './rule.js'
import { getRuleProps } from './rule/props.js'
import { validateRuleProps } from './rule/validate.js'

// Normalize `options`
export const normalizeOpts = function (options = {}) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { inspect } from 'util'
import isPlainObj from 'is-plain-obj'
import { normalizeQuery } from 'wild-wild-parser'

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

import { DefinitionError } from './error.js'
import { validateRuleProps } from './rule.js'
import { validateRuleProps } from './validate.js'

// Validate and normalize rules.
// All methods and properties that use queries can use either the string or the
Expand Down
37 changes: 37 additions & 0 deletions src/config/normalize/lib/rule/props.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { inspect } from 'util'

import { DefinitionError } from '../error.js'

// Retrieve the list of possible rule properties
export const getRuleProps = function (keywords) {
return new Set([...CORE_PROPS, ...keywords.map(getKeywordName)])
}

const getKeywordName = function ({ name }) {
return name
}

// Rule properties that are not keywords
const CORE_PROPS = ['name']
export const CORE_PROPS_SET = new Set(CORE_PROPS)

export const validateRuleKey = function ({
ruleProp,
ruleProps,
message,
definitions,
}) {
if (ruleProps.has(ruleProp)) {
return
}

// eslint-disable-next-line fp/no-mutating-methods
const rulePropsA = [...ruleProps].sort().join(', ')
throw new DefinitionError(
`${message}'s "${ruleProp}" property must be valid: ${inspect(definitions)}
It must be one of the following values instead:
${rulePropsA}
Did you misspell "${ruleProp}"?
If "${ruleProp}" is not misspelled, its keyword must be passed to the "keywords" option.`,
)
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
import { inspect } from 'util'
import { DefinitionError } from '../error.js'

import { DefinitionError } from './error.js'

// Retrieve the list of possible rule properties
export const getRuleProps = function (keywords) {
return new Set([...CORE_PROPS, ...keywords.map(getKeywordName)])
}

const getKeywordName = function ({ name }) {
return name
}
import { CORE_PROPS_SET, validateRuleKey } from './props.js'

// Validate that a `definitions` object has only allowed properties
export const validateRuleProps = function ({
Expand All @@ -36,27 +27,6 @@ const validateRuleProp = function ({
validateRuleSync({ ruleProp, message, definitions, sync })
}

const validateRuleKey = function ({
ruleProp,
ruleProps,
message,
definitions,
}) {
if (ruleProps.has(ruleProp)) {
return
}

// eslint-disable-next-line fp/no-mutating-methods
const rulePropsA = [...ruleProps].sort().join(', ')
throw new DefinitionError(
`${message}'s "${ruleProp}" property must be valid: ${inspect(definitions)}
It must be one of the following values instead:
${rulePropsA}
Did you misspell "${ruleProp}"?
If "${ruleProp}" is not misspelled, its keyword must be passed to the "keywords" option.`,
)
}

// If in sync mode, definition functions should not be async.
// However, we cannot know this for sure since:
// - Functions might return `Promise` instead of using `async`/`await`
Expand All @@ -81,7 +51,3 @@ const isAsyncFunction = function (definition) {
definition.constructor.name === 'AsyncFunction'
)
}

// Rule properties that are not keywords
const CORE_PROPS = ['name']
export const CORE_PROPS_SET = new Set(CORE_PROPS)

0 comments on commit 8f4189d

Please sign in to comment.