Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added schema validation for string literal attribute values
- Loading branch information
1 parent
76a418e
commit a61307c
Showing
10 changed files
with
102 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module.exports = function ($$kind) { | ||
const element = {$$kind}; | ||
Object.defineProperty(element, 'attrs', {value: {}, enumerable: false}); | ||
return element; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
module.exports.JSXFragment = require('./jsxFragment'); | ||
module.exports.JSXElement = require('./jsxElement'); | ||
module.exports.JSXSpreadChild = require('./jsxSpreadChild'); | ||
module.exports.JSXAttribute = require('./jsxAttribute'); | ||
module.exports.JSXSpreadAttribute = require('./jsxSpreadAttribute'); | ||
module.exports.JSXExpressionContainer = require('./jsxExpressionContainer'); | ||
module.exports.Program = require('./program'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
const t = require('@babel/types'); | ||
const {getMapKeyForNode, getJSXElement, syntaxErrorThrower, getDefinitionFromSchema, getValueFromSchema} = require('../helpers'); | ||
|
||
module.exports = { | ||
enter(path, scope) { | ||
const jsxElement = getJSXElement(t, path); | ||
const key = getMapKeyForNode(jsxElement); | ||
const element = scope.opts.nodeMap[key]; | ||
const attr = path.node; | ||
const name = attr.name.name; | ||
const isSpreadAttr = t.isJSXSpreadAttribute(attr); | ||
if (element.$$kind === 'component' && !isSpreadAttr) { | ||
element.props[name] = attr.value; | ||
return; | ||
} | ||
|
||
const typeAttr = jsxElement.openingElement.attributes.find(attr => (!t.isJSXSpreadAttribute(attr) && attr.name.name === 'type')); | ||
if (typeAttr && !t.isStringLiteral(typeAttr.value)) { | ||
syntaxErrorThrower(path, 'Type attributes must be string literals'); | ||
} | ||
|
||
const type = typeAttr ? typeAttr.value.value : null; | ||
const definition = getDefinitionFromSchema(path, element.$$kind, type); | ||
if (!definition.properties && path.node.openingElement.attributes.length) { | ||
syntaxErrorThrower(path, `${element.$$kind} does not allow attributes`); | ||
} | ||
|
||
if (!isSpreadAttr && !(name in element.attrs)) { | ||
|
||
if (!definition.properties[name] || (element[name] && element.attrs[name])) { | ||
syntaxErrorThrower(path, `The ${name} attribute is not allowed here`); | ||
} | ||
|
||
// Validation can only occur when we have a string literal | ||
if (t.isStringLiteral(attr.value)) { | ||
const {value} = attr.value; | ||
const attributeDefinition = definition.properties[name]; | ||
let {enum:enums, $ref} = attributeDefinition; | ||
if ($ref) { | ||
enums = getValueFromSchema($ref).enum; | ||
} | ||
if (enums && !enums.includes(value)) { | ||
syntaxErrorThrower(path, `${value} is not a valid value for the ${name} attribute`); | ||
} | ||
} | ||
element.attrs[name] = attr.value; | ||
} | ||
} | ||
}; |