Skip to content

Commit

Permalink
Validate plugin.id matches package name
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Feb 13, 2022
1 parent 0500687 commit 9f22118
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
7 changes: 4 additions & 3 deletions src/config/plugin/lib/each.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { normalizeShape } from './shape.js'
export const addPlugin = async function (
{ name, builtins, pluginProp, shape, item },
{
pluginConfig: { [pluginProp]: id, ...pluginConfig },
pluginConfig: { [pluginProp]: id, moduleId, ...pluginConfig },
index,
pluginsCount,
sharedPropNames,
Expand All @@ -17,11 +17,12 @@ export const addPlugin = async function (
) {
const propName = getPropName(name, index, pluginsCount)
const { plugin, path } = await importPlugin(id, propName, builtins)
const { config: pluginConfigDefinitions, ...pluginA } = await normalizeShape(
const { config: pluginConfigDefinitions, ...pluginA } = await normalizeShape({
plugin,
shape,
sharedPropNames,
)
moduleId,
})
const pluginConfigA = await normalizePluginConfig({
propName,
sharedConfig,
Expand Down
15 changes: 15 additions & 0 deletions src/config/plugin/lib/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const getListDefinitions = function (name, list, pluginProp) {
return [
{ ...list, name, ...normalizeListProp },
{ name: `${name}.*`, ...normalizeItem },
{ name: `${name}.*.moduleId`, ...normalizeItemModuleId },
{ name: `${name}.*.${pluginProp}`, ...normalizeItemId },
]
}
Expand All @@ -53,6 +54,20 @@ const normalizeItem = {
},
}

const normalizeItemModuleId = {
compute({
context: { builtins, modulePrefix },
path: [name, index],
config: {
[name]: {
[index]: { id },
},
},
}) {
return isModuleId(id, modulePrefix, builtins) ? id : undefined
},
}

const normalizeItemId = {
required: true,
path(id, { context: { builtins } }) {
Expand Down
20 changes: 17 additions & 3 deletions src/config/plugin/lib/shape.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,25 @@ import { normalizeConfig } from '../../normalize/main.js'
import { validateDefinedString } from '../../normalize/validate/simple.js'

// Validate a plugin has the correct shape and normalize it
export const normalizeShape = async function (plugin, shape, sharedPropNames) {
export const normalizeShape = async function ({
plugin,
shape,
sharedPropNames,
moduleId,
}) {
return await normalizeConfig(plugin, [...COMMON_SHAPE, ...shape], {
context: { sharedPropNames },
context: { sharedPropNames, moduleId },
UserErrorType: PluginError,
})
}

const idProp = {
name: 'id',
required: true,
validate(id) {
validate(id, { context: { moduleId } }) {
validateDefinedString(id)
validateIdCharacters(id)
validateModuleId(id, moduleId)
},
}

Expand All @@ -37,6 +43,14 @@ const validateIdCharacters = function (id) {

const PLUGIN_ID_REGEXP = /^[a-z][a-z\d]*$/u

// When using a Node module, the exported `id` must match the `id` specified by
// the user
const validateModuleId = function (id, moduleId) {
if (moduleId !== undefined && moduleId !== id) {
throw new Error(`must be "${moduleId}" to match the package name.`)
}
}

const configPropName = {
name: 'config.*.name',
validate(name, { context: { sharedPropNames } }) {
Expand Down

0 comments on commit 9f22118

Please sign in to comment.