refactor: varargs

This commit is contained in:
cha0s 2021-01-31 02:18:07 -06:00
parent b8fbd61765
commit f7dc24f9d0
2 changed files with 19 additions and 10 deletions

View File

@ -15,9 +15,10 @@ import Variant from './variant';
const Expression = ({ const Expression = ({
context, context,
value,
path, path,
type, type,
value,
vararg,
}) => { }) => {
const latus = useLatus(); const latus = useLatus();
const patch = useJsonPatcher(); const patch = useJsonPatcher();
@ -60,7 +61,6 @@ const Expression = ({
if (isKey(op)) { if (isKey(op)) {
const isFirst = 0 === i; const isFirst = 0 === i;
const isLast = i === ops.length - 1 ? true : isInvocation(ops[i + 1]); const isLast = i === ops.length - 1 ? true : isInvocation(ops[i + 1]);
const isUndefined = 'undefined' === description.type || 'undefined' === type;
Renderables.push( Renderables.push(
<Key <Key
childrenDescription={{ childrenDescription={{
@ -72,7 +72,7 @@ const Expression = ({
: {} : {}
), ),
...( ...(
isUndefined || (isLast && !isFirst) vararg || (isLast && !isFirst)
? {'.': {label: '', type: description.type}} ? {'.': {label: '', type: description.type}}
: {} : {}
), ),
@ -94,7 +94,7 @@ const Expression = ({
const parts = localPath.split('/'); const parts = localPath.split('/');
parts.pop(); parts.pop();
parts.pop(); parts.pop();
if (isUndefined) { if (vararg) {
parts.pop(); parts.pop();
patch({ patch({
op: 'remove', op: 'remove',
@ -297,8 +297,8 @@ const Expression = ({
<div <div
className={classnames( className={classnames(
'expression', 'expression',
{'wrong-type': realType !== type}, {'wrong-type': 'any' !== type && 'undefined' !== type && realType !== type},
{'undefined-type': 'undefined' === realType}, {'undefined-type': 'any' !== type && 'undefined' === realType},
)} )}
> >
{Renderables} {Renderables}
@ -306,7 +306,9 @@ const Expression = ({
); );
}; };
Expression.defaultProps = {}; Expression.defaultProps = {
vararg: false,
};
Expression.displayName = 'Expression'; Expression.displayName = 'Expression';
@ -327,6 +329,7 @@ Expression.propTypes = {
// eslint-disable-next-line react/forbid-prop-types // eslint-disable-next-line react/forbid-prop-types
value: PropTypes.any, value: PropTypes.any,
}).isRequired, }).isRequired,
vararg: PropTypes.bool,
path: PropTypes.string.isRequired, path: PropTypes.string.isRequired,
type: PropTypes.string.isRequired, type: PropTypes.string.isRequired,
}; };

View File

@ -58,8 +58,9 @@ const Invocation = ({
/> />
); );
} }
const options = description?.args?.[i].options; const j = description.vararg ? Math.min(description?.args?.length - 1, i) : i;
const type = description?.args?.[i].type || 'undefined'; const options = description?.args?.[j].options;
const type = description?.args?.[j].type || 'undefined';
return ( return (
<Variant <Variant
context={context} context={context}
@ -68,10 +69,14 @@ const Invocation = ({
path={argPath} path={argPath}
type={type} type={type}
value={arg} value={arg}
vararg={
'undefined' === type
|| (description.vararg && j === description?.args?.length - 1)
}
/> />
); );
}; };
const isVarArg = 'undefined' === description.type; const isVarArg = 'undefined' === description.type || description.vararg;
const args = op.args.concat(); const args = op.args.concat();
if (isVarArg) { if (isVarArg) {
args.push({type: 'undefined'}); args.push({type: 'undefined'});
@ -108,6 +113,7 @@ Invocation.propTypes = {
}), }),
), ),
type: PropTypes.string, type: PropTypes.string,
vararg: PropTypes.bool,
}).isRequired, }).isRequired,
onChange: PropTypes.func.isRequired, onChange: PropTypes.func.isRequired,
op: PropTypes.shape({ op: PropTypes.shape({