@teleporthq/teleport-plugin-common
Advanced tools
Comparing version 0.37.3 to 0.37.6
@@ -147,6 +147,6 @@ import { | ||
it('works on member expressions', () => { | ||
const node = dynamicNode('prop', 'fields.title') | ||
const node = dynamicNode('prop', 'fields', ['title']) | ||
const result = createConditionIdentifier(node, params, options) | ||
expect(result.key).toBe('fields.title') | ||
expect(result.key).toBe("fields?.['title']") | ||
expect(result.prefix).toBe('this.props') | ||
@@ -153,0 +153,0 @@ expect(result.type).toBe('object') |
@@ -39,2 +39,3 @@ "use strict"; | ||
var teleport_shared_1 = require("@teleporthq/teleport-shared"); | ||
var uidl_utils_1 = require("@teleporthq/teleport-shared/dist/cjs/utils/uidl-utils"); | ||
// Adds all the event handlers and all the instructions for each event handler | ||
@@ -123,7 +124,4 @@ // in case there is more than one specified in the UIDL | ||
var refPath = identifier.content.refPath || []; | ||
var referenceType = identifierContent.referenceType; | ||
var id = identifierContent.id; | ||
refPath === null || refPath === void 0 ? void 0 : refPath.forEach(function (pathItem) { | ||
id = id.concat("?.".concat(pathItem)); | ||
}); | ||
var referenceType = identifierContent.referenceType, id = identifierContent.id; | ||
var idWithPath = (0, uidl_utils_1.generateIdWithRefPath)(id, refPath); | ||
if (referenceType === 'attr' || referenceType === 'children' || referenceType === 'token') { | ||
@@ -134,4 +132,4 @@ throw new Error("Dynamic reference type \"".concat(referenceType, "\" is not supported yet")); | ||
return prefix === '' | ||
? t.identifier(id) | ||
: t.memberExpression(t.identifier(prefix), t.identifier(id)); | ||
? t.identifier(idWithPath) | ||
: t.memberExpression(t.identifier(prefix), t.identifier(idWithPath)); | ||
}; | ||
@@ -142,2 +140,3 @@ exports.createDynamicValueExpression = createDynamicValueExpression; | ||
var createConditionIdentifier = function (dynamicReference, params, options) { | ||
var _a, _b; | ||
if (dynamicReference.type === 'expr') { | ||
@@ -149,10 +148,22 @@ return { | ||
} | ||
var _a = dynamicReference.content, id = _a.id, referenceType = _a.referenceType; | ||
var _c = dynamicReference.content, id = _c.id, referenceType = _c.referenceType, refPath = _c.refPath; | ||
// in case the id is a member expression: eg: fields.name | ||
var referenceRoot = id.split('.')[0]; | ||
var currentType = referenceType === 'prop' | ||
? (_a = params.propDefinitions[referenceRoot]) === null || _a === void 0 ? void 0 : _a.type | ||
: (_b = params.stateDefinitions[referenceRoot]) === null || _b === void 0 ? void 0 : _b.type; | ||
var type = currentType; | ||
if (refPath === null || refPath === void 0 ? void 0 : refPath.length) { | ||
var currentValue = params.propDefinitions[referenceRoot].defaultValue; | ||
for (var _i = 0, refPath_1 = refPath; _i < refPath_1.length; _i++) { | ||
var path = refPath_1[_i]; | ||
currentValue = currentValue === null || currentValue === void 0 ? void 0 : currentValue[path]; | ||
type = currentValue ? typeof currentValue : currentType; | ||
} | ||
} | ||
switch (referenceType) { | ||
case 'prop': | ||
return { | ||
key: id, | ||
type: params.propDefinitions[referenceRoot].type, | ||
key: teleport_shared_1.UIDLUtils.generateIdWithRefPath(id, refPath), | ||
type: type, | ||
prefix: options.dynamicReferencePrefixMap.prop, | ||
@@ -162,4 +173,4 @@ }; | ||
return { | ||
key: id, | ||
type: params.stateDefinitions[referenceRoot].type, | ||
key: teleport_shared_1.UIDLUtils.generateIdWithRefPath(id, refPath), | ||
type: type, | ||
prefix: options.dynamicReferencePrefixMap.state, | ||
@@ -166,0 +177,0 @@ }; |
@@ -255,6 +255,11 @@ "use strict"; | ||
var props = Object.keys(objectMap).reduce(function (acc, key) { | ||
var _a; | ||
var keyIdentifier = t.stringLiteral(key); | ||
var value = objectMap[key]; | ||
var computedLiteralValue = null; | ||
if (value instanceof parsed_ast_1.default || value.constructor.name === 'ParsedASTNode') { | ||
// TODO: Is this safe? This is for function props | ||
if (((_a = value === null || value === void 0 ? void 0 : value.constructor) === null || _a === void 0 ? void 0 : _a.name) === 'Node') { | ||
computedLiteralValue = value; | ||
} | ||
else if (value instanceof parsed_ast_1.default || value.constructor.name === 'ParsedASTNode') { | ||
computedLiteralValue = value.ast; | ||
@@ -261,0 +266,0 @@ } |
@@ -12,3 +12,4 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { | ||
import { convertToBinaryOperator, convertToUnaryOperator, convertValueToLiteral, } from '../../utils/ast-utils'; | ||
import { StringUtils } from '@teleporthq/teleport-shared'; | ||
import { StringUtils, UIDLUtils } from '@teleporthq/teleport-shared'; | ||
import { generateIdWithRefPath } from '@teleporthq/teleport-shared/dist/cjs/utils/uidl-utils'; | ||
// Adds all the event handlers and all the instructions for each event handler | ||
@@ -96,7 +97,4 @@ // in case there is more than one specified in the UIDL | ||
var refPath = identifier.content.refPath || []; | ||
var referenceType = identifierContent.referenceType; | ||
var id = identifierContent.id; | ||
refPath === null || refPath === void 0 ? void 0 : refPath.forEach(function (pathItem) { | ||
id = id.concat("?.".concat(pathItem)); | ||
}); | ||
var referenceType = identifierContent.referenceType, id = identifierContent.id; | ||
var idWithPath = generateIdWithRefPath(id, refPath); | ||
if (referenceType === 'attr' || referenceType === 'children' || referenceType === 'token') { | ||
@@ -107,4 +105,4 @@ throw new Error("Dynamic reference type \"".concat(referenceType, "\" is not supported yet")); | ||
return prefix === '' | ||
? t.identifier(id) | ||
: t.memberExpression(t.identifier(prefix), t.identifier(id)); | ||
? t.identifier(idWithPath) | ||
: t.memberExpression(t.identifier(prefix), t.identifier(idWithPath)); | ||
}; | ||
@@ -114,2 +112,3 @@ // Prepares an identifier (from props or state or an expr) to be used as a conditional rendering identifier | ||
export var createConditionIdentifier = function (dynamicReference, params, options) { | ||
var _a, _b; | ||
if (dynamicReference.type === 'expr') { | ||
@@ -121,10 +120,22 @@ return { | ||
} | ||
var _a = dynamicReference.content, id = _a.id, referenceType = _a.referenceType; | ||
var _c = dynamicReference.content, id = _c.id, referenceType = _c.referenceType, refPath = _c.refPath; | ||
// in case the id is a member expression: eg: fields.name | ||
var referenceRoot = id.split('.')[0]; | ||
var currentType = referenceType === 'prop' | ||
? (_a = params.propDefinitions[referenceRoot]) === null || _a === void 0 ? void 0 : _a.type | ||
: (_b = params.stateDefinitions[referenceRoot]) === null || _b === void 0 ? void 0 : _b.type; | ||
var type = currentType; | ||
if (refPath === null || refPath === void 0 ? void 0 : refPath.length) { | ||
var currentValue = params.propDefinitions[referenceRoot].defaultValue; | ||
for (var _i = 0, refPath_1 = refPath; _i < refPath_1.length; _i++) { | ||
var path = refPath_1[_i]; | ||
currentValue = currentValue === null || currentValue === void 0 ? void 0 : currentValue[path]; | ||
type = currentValue ? typeof currentValue : currentType; | ||
} | ||
} | ||
switch (referenceType) { | ||
case 'prop': | ||
return { | ||
key: id, | ||
type: params.propDefinitions[referenceRoot].type, | ||
key: UIDLUtils.generateIdWithRefPath(id, refPath), | ||
type: type, | ||
prefix: options.dynamicReferencePrefixMap.prop, | ||
@@ -134,4 +145,4 @@ }; | ||
return { | ||
key: id, | ||
type: params.stateDefinitions[referenceRoot].type, | ||
key: UIDLUtils.generateIdWithRefPath(id, refPath), | ||
type: type, | ||
prefix: options.dynamicReferencePrefixMap.state, | ||
@@ -138,0 +149,0 @@ }; |
@@ -215,6 +215,11 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { | ||
var props = Object.keys(objectMap).reduce(function (acc, key) { | ||
var _a; | ||
var keyIdentifier = t.stringLiteral(key); | ||
var value = objectMap[key]; | ||
var computedLiteralValue = null; | ||
if (value instanceof ParsedASTNode || value.constructor.name === 'ParsedASTNode') { | ||
// TODO: Is this safe? This is for function props | ||
if (((_a = value === null || value === void 0 ? void 0 : value.constructor) === null || _a === void 0 ? void 0 : _a.name) === 'Node') { | ||
computedLiteralValue = value; | ||
} | ||
else if (value instanceof ParsedASTNode || value.constructor.name === 'ParsedASTNode') { | ||
computedLiteralValue = value.ast; | ||
@@ -221,0 +226,0 @@ } |
{ | ||
"name": "@teleporthq/teleport-plugin-common", | ||
"version": "0.37.3", | ||
"version": "0.37.6", | ||
"description": "Common building and modelating functions for ASTs and HASTs", | ||
@@ -30,8 +30,8 @@ "author": "teleportHQ", | ||
"@babel/types": "^7.5.5", | ||
"@teleporthq/teleport-shared": "^0.37.3", | ||
"@teleporthq/teleport-types": "^0.37.3", | ||
"@teleporthq/teleport-shared": "^0.37.6", | ||
"@teleporthq/teleport-types": "^0.37.6", | ||
"jss": "^10.0.0", | ||
"jss-preset-default": "^10.0.0" | ||
}, | ||
"gitHead": "c4a91ebb3cddb547ff0a635ffe2036a609560434" | ||
"gitHead": "463fe45a6f7a5300c06cf8f544c7b522a9ad99fe" | ||
} |
@@ -8,3 +8,3 @@ import * as types from '@babel/types' | ||
} from '../../utils/ast-utils' | ||
import { StringUtils } from '@teleporthq/teleport-shared' | ||
import { StringUtils, UIDLUtils } from '@teleporthq/teleport-shared' | ||
import { | ||
@@ -29,2 +29,3 @@ UIDLPropDefinition, | ||
} from './types' | ||
import { generateIdWithRefPath } from '@teleporthq/teleport-shared/dist/cjs/utils/uidl-utils' | ||
@@ -158,8 +159,5 @@ // Adds all the event handlers and all the instructions for each event handler | ||
const refPath = identifier.content.refPath || [] | ||
const { referenceType } = identifierContent | ||
const { referenceType, id } = identifierContent | ||
let id = identifierContent.id | ||
refPath?.forEach((pathItem) => { | ||
id = id.concat(`?.${pathItem}`) | ||
}) | ||
const idWithPath = generateIdWithRefPath(id, refPath) | ||
@@ -174,4 +172,4 @@ if (referenceType === 'attr' || referenceType === 'children' || referenceType === 'token') { | ||
return prefix === '' | ||
? t.identifier(id) | ||
: t.memberExpression(t.identifier(prefix), t.identifier(id)) | ||
? t.identifier(idWithPath) | ||
: t.memberExpression(t.identifier(prefix), t.identifier(idWithPath)) | ||
} | ||
@@ -193,12 +191,25 @@ | ||
const { id, referenceType } = dynamicReference.content | ||
const { id, referenceType, refPath } = dynamicReference.content | ||
// in case the id is a member expression: eg: fields.name | ||
const referenceRoot = id.split('.')[0] | ||
const currentType = | ||
referenceType === 'prop' | ||
? params.propDefinitions[referenceRoot]?.type | ||
: params.stateDefinitions[referenceRoot]?.type | ||
let type = currentType | ||
if (refPath?.length) { | ||
let currentValue = params.propDefinitions[referenceRoot].defaultValue as Record<string, unknown> | ||
for (const path of refPath) { | ||
currentValue = currentValue?.[path] as Record<string, unknown> | ||
type = currentValue ? typeof currentValue : currentType | ||
} | ||
} | ||
switch (referenceType) { | ||
case 'prop': | ||
return { | ||
key: id, | ||
type: params.propDefinitions[referenceRoot].type, | ||
key: UIDLUtils.generateIdWithRefPath(id, refPath), | ||
type, | ||
prefix: options.dynamicReferencePrefixMap.prop, | ||
@@ -208,4 +219,4 @@ } | ||
return { | ||
key: id, | ||
type: params.stateDefinitions[referenceRoot].type, | ||
key: UIDLUtils.generateIdWithRefPath(id, refPath), | ||
type, | ||
prefix: options.dynamicReferencePrefixMap.state, | ||
@@ -212,0 +223,0 @@ } |
@@ -327,3 +327,6 @@ import * as types from '@babel/types' | ||
if (value instanceof ParsedASTNode || value.constructor.name === 'ParsedASTNode') { | ||
// TODO: Is this safe? This is for function props | ||
if (value?.constructor?.name === 'Node') { | ||
computedLiteralValue = value | ||
} else if (value instanceof ParsedASTNode || value.constructor.name === 'ParsedASTNode') { | ||
computedLiteralValue = (value as ParsedASTNode).ast | ||
@@ -330,0 +333,0 @@ } else if (typeof value === 'boolean') { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
673687
8947