Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Feb 6, 2022
1 parent 2078ce8 commit 87b9c1c
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/config/plugin/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,5 @@ const getPluginsByType = async function ({
context,
configInfos,
})
return [pluginType.selectProp.name, plugins]
return [pluginType.name, plugins]
}
14 changes: 7 additions & 7 deletions src/config/plugin/id.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import { PluginError } from '../../error/main.js'
// (e.g. runners) because those should use variations instead.
// Since the list of plugin module names is unknown, users must indicate using
// this by the usage of a delimiter character.
export const getModuleId = function (id, multiple, selectPropName) {
export const getModuleId = function (id, multiple, name) {
const [moduleId, customId] = splitId(id, multiple)
validateModuleId(moduleId, selectPropName)
validateCustomId(customId, selectPropName)
validateModuleId(moduleId, name)
validateCustomId(customId, name)
return moduleId
}

Expand All @@ -34,9 +34,9 @@ const splitId = function (id, multiple) {

const CUSTOM_ID_DELIMITER = '_'

const validateModuleId = function (moduleId, selectPropName) {
const validateModuleId = function (moduleId, name) {
if (!MODULE_ID_REGEXP.test(moduleId)) {
throw new PluginError(`The identifier of the ${selectPropName} "${moduleId}" is invalid.
throw new PluginError(`The identifier of the ${name} "${moduleId}" is invalid.
It should only contain lowercase letters and digits.`)
}
}
Expand All @@ -52,11 +52,11 @@ It should only contain lowercase letters and digits.`)
// This is purposely not applied to shared configs.
const MODULE_ID_REGEXP = /^[a-z][a-z\d]*$/u

const validateCustomId = function (customId, selectPropName) {
const validateCustomId = function (customId, name) {
if (customId !== undefined) {
validateUserId({
id: customId,
messageName: `${selectPropName} identifier suffix`,
messageName: `${name} identifier suffix`,
})
}
}
16 changes: 6 additions & 10 deletions src/config/plugin/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import { getModuleId } from './id.js'
// a dynamic `Module` instance.
export const importPlugin = async function ({
id,
selectPropName,
name,
modulePrefix,
builtins,
multiple,
}) {
const moduleId = getModuleId(id, multiple, selectPropName)
const moduleId = getModuleId(id, multiple, name)
const builtin = builtins[moduleId]

if (builtin !== undefined) {
Expand All @@ -26,7 +26,7 @@ export const importPlugin = async function ({
const moduleName = `${modulePrefix}${moduleId}`
const pluginPath = safeGetPluginPath({
moduleName,
selectPropName,
name,
base: PLUGINS_IMPORT_BASE,
})

Expand All @@ -35,7 +35,7 @@ export const importPlugin = async function ({
} catch (error) {
throw wrapError(
error,
`Could not load "${selectPropName}" module "${moduleId}"\n\n`,
`Could not load "${name}" module "${moduleId}"\n\n`,
PluginError,
)
}
Expand All @@ -49,15 +49,11 @@ export const importPlugin = async function ({
// - This prevent the confusion (which could be malicious) created by the
// ambiguity
// TODO: use import.meta.resolve() when available
const safeGetPluginPath = function ({ moduleName, selectPropName, base }) {
const safeGetPluginPath = function ({ moduleName, name, base }) {
try {
return getPluginPath(moduleName, base)
} catch (error) {
throw wrapError(
error,
`Configuration property "${selectPropName}"`,
UserError,
)
throw wrapError(error, `The ${name}`, UserError)
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/config/plugin/load.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const loadPlugins = async function ({
const loadPlugin = async function (
{ id, config, topConfig, context, configInfos },
{
selectProp: { name: selectPropName },
name,
configProp: { name: configPropName },
modulePrefix,
builtins,
Expand All @@ -38,7 +38,7 @@ const loadPlugin = async function (
) {
const plugin = await importPlugin({
id,
selectPropName,
name,
modulePrefix,
builtins,
multiple,
Expand Down
4 changes: 2 additions & 2 deletions src/config/plugin/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { RUNNER_PLUGIN_TYPE } from '../../runners/plugin.js'

// All plugin types
export const PLUGIN_TYPES = {
[RUNNER_PLUGIN_TYPE.selectProp.name]: RUNNER_PLUGIN_TYPE,
[REPORTER_PLUGIN_TYPE.selectProp.name]: REPORTER_PLUGIN_TYPE,
[RUNNER_PLUGIN_TYPE.name]: RUNNER_PLUGIN_TYPE,
[REPORTER_PLUGIN_TYPE.name]: REPORTER_PLUGIN_TYPE,
}

export const PLUGIN_TYPES_ARRAY = Object.values(PLUGIN_TYPES)
1 change: 1 addition & 0 deletions src/runners/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const RUNNER_PLUGIN_TYPE = {
// Name used:
// - As a default value for the selection property
// - As a default value for the configuration property appended with `Config`
// - In the return value
// - In error messages
name: 'runner',
// Prefix of the npm package
Expand Down

0 comments on commit 87b9c1c

Please sign in to comment.