@teleporthq/teleport-plugin-common
Advanced tools
Comparing version 0.36.6 to 0.37.2
@@ -118,2 +118,4 @@ "use strict"; | ||
return generateSlotNode(node, params, templateSyntax); | ||
case 'expr': | ||
return 'Expression nodes are not supported'; | ||
default: | ||
@@ -124,2 +126,8 @@ throw new Error("generateHTMLSyntax encountered a node of unsupported type: ".concat(JSON.stringify(node, null, 2))); | ||
var generateDynamicNode = function (node, params, templateSyntax) { | ||
if (node.content.referenceType === 'global') { | ||
// TODO: Check this in the future. Not throwing an error for now | ||
console.info("Global dynamic values are not supported in the HTML generator"); | ||
var spanNode = (0, hast_builders_1.createHTMLNode)('span'); | ||
return spanNode; | ||
} | ||
var usedPropType = params.propDefinitions[node.content.id]; | ||
@@ -126,0 +134,0 @@ if (usedPropType && usedPropType.type === 'element') { |
@@ -87,3 +87,4 @@ "use strict"; | ||
case 'expr': | ||
throw new Error("Expressions are not supported in HTML templates"); | ||
// TODO: Check this in the future. Not throwing an error for now | ||
console.info("Expressions are not supported in HTML templates"); | ||
break; | ||
@@ -151,2 +152,5 @@ default: | ||
var operation = _a.operation, operand = _a.operand; | ||
// @todo | ||
// unlike jsx code generation, we are not converting the operand to binary or unary operation. | ||
// Please refer to https://github.com/teleporthq/teleport-code-generators/blob/development/packages/teleport-plugin-common/src/node-handlers/node-to-jsx/utils.ts#L303-L319 | ||
return "(".concat(stringifyConditionalExpression(conditionalKey, operation, operand), ")"); | ||
@@ -153,0 +157,0 @@ }); |
@@ -155,4 +155,3 @@ "use strict"; | ||
if (node === null || node === void 0 ? void 0 : node.dependency) { | ||
/* tslint:disable:no-string-literal */ | ||
params.dependencies['Script'] = node.dependency; | ||
params.dependencies.Script = node.dependency; | ||
} | ||
@@ -163,19 +162,45 @@ return [node.content.toString()]; | ||
case 'dynamic': | ||
// If the dynamic node is a prop and has a default value, | ||
// we should use it with a logical expression. And the most used case is for named-slots. | ||
var prop = params.propDefinitions[node.content.id]; | ||
if ((prop === null || prop === void 0 ? void 0 : prop.type) === 'element' && prop.defaultValue) { | ||
var prefix = options.dynamicReferencePrefixMap[node.content.referenceType] || ''; | ||
var propDefault = prop.defaultValue; | ||
var jsxNode = params.nodesLookup[propDefault.content.key]; | ||
if (jsxNode === undefined) { | ||
throw Error("Prop ".concat(node.content.id, " is of type element \n\n The JSXNode of the prop-").concat(node.content.id, " is missing from the nodesLookup")); | ||
switch (node.content.referenceType) { | ||
case 'prop': { | ||
// If the dynamic node is a prop and has a default value of type UIDLElementNode, | ||
// we should use it with a logical expression. | ||
var prop = params.propDefinitions[node.content.id]; | ||
if ((prop === null || prop === void 0 ? void 0 : prop.type) === 'element' && prop.defaultValue) { | ||
var prefix = options.dynamicReferencePrefixMap[node.content.referenceType] || ''; | ||
var propDefault = prop.defaultValue; | ||
var jsxNode = params.nodesLookup[propDefault.content.key]; | ||
if (jsxNode === undefined) { | ||
throw Error("Prop ".concat(node.content.id, " is of type element \n\n The JSXNode of the prop-").concat(node.content.id, " is missing from the nodesLookup")); | ||
} | ||
return [ | ||
types.logicalExpression('??', prefix === '' | ||
? types.identifier(node.content.id) | ||
: types.memberExpression(types.identifier(prefix), types.identifier(node.content.id)), jsxNode), | ||
]; | ||
} | ||
return [(0, utils_1.createDynamicValueExpression)(node, options)]; | ||
} | ||
return [ | ||
types.logicalExpression('??', prefix === '' | ||
? types.identifier(node.content.id) | ||
: types.memberExpression(types.identifier(prefix), types.identifier(node.content.id)), jsxNode), | ||
]; | ||
case 'locale': { | ||
// Locale is not handled the same in all frameworks. | ||
// So, we need to handle it differently using individual plugins for each framework. | ||
var emptyExpression = types.jsxEmptyExpression(); | ||
emptyExpression.innerComments = [ | ||
{ | ||
type: 'CommentBlock', | ||
value: "locale-".concat(node.content.id), | ||
}, | ||
]; | ||
var expression = types.jsxExpressionContainer(emptyExpression); | ||
var jsxTag = (0, ast_builders_1.createJSXTag)('span'); | ||
(0, ast_utils_1.addChildJSXTag)(jsxTag, expression); | ||
params.localeReferences.push(jsxTag); | ||
return [jsxTag]; | ||
} | ||
case 'global': { | ||
params.globalReferences.push(node.content.id); | ||
return [(0, utils_1.createDynamicValueExpression)(node, options)]; | ||
} | ||
default: | ||
return [(0, utils_1.createDynamicValueExpression)(node, options)]; | ||
} | ||
return [(0, utils_1.createDynamicValueExpression)(node, options)]; | ||
case 'cms-item': | ||
@@ -182,0 +207,0 @@ case 'cms-list': |
import * as types from '@babel/types'; | ||
import { UIDLPropDefinition, UIDLDependency, UIDLStateDefinition } from '@teleporthq/teleport-types'; | ||
import { UIDLPropDefinition, UIDLDependency, UIDLStateDefinition, UIDLGlobalReference } from '@teleporthq/teleport-types'; | ||
export interface JSXGenerationParams { | ||
propDefinitions: Record<string, UIDLPropDefinition>; | ||
stateDefinitions: Record<string, UIDLStateDefinition>; | ||
nodesLookup: Record<string, types.JSXElement>; | ||
nodesLookup: Record<string, types.JSXElement | types.JSXExpressionContainer>; | ||
dependencies: Record<string, UIDLDependency>; | ||
windowImports: Record<string, types.ExpressionStatement>; | ||
localeReferences: types.JSXElement[]; | ||
globalReferences: Array<UIDLGlobalReference['content']['id']>; | ||
} | ||
@@ -24,4 +26,2 @@ export interface JSXGenerationOptions { | ||
export type JSXASTReturnType = string | types.JSXExpressionContainer | types.JSXElement | types.LogicalExpression | types.Identifier | types.MemberExpression; | ||
export type BinaryOperator = '===' | '+' | '-' | '/' | '%' | '*' | '**' | '&' | '|' | '>>' | '>>>' | '<<' | '^' | '==' | '!=' | '!==' | 'in' | 'instanceof' | '>' | '<' | '>=' | '<='; | ||
export type UnaryOperation = '+' | '-' | 'void' | 'throw' | 'delete' | '!' | '~' | 'typeof'; | ||
export interface ConditionalIdentifier { | ||
@@ -28,0 +28,0 @@ key: string; |
import * as types from '@babel/types'; | ||
import { UIDLAttributeValue, UIDLDynamicReference, UIDLEventHandlerStatement, UIDLConditionalExpression, UIDLExpressionValue } from '@teleporthq/teleport-types'; | ||
import { UIDLAttributeValue, UIDLDynamicReference, UIDLEventHandlerStatement, UIDLConditionalExpression, UIDLExpressionValue, UIDLGlobalReference } from '@teleporthq/teleport-types'; | ||
import { JSXASTReturnType, ConditionalIdentifier, JSXGenerationParams, JSXGenerationOptions } from './types'; | ||
export declare const addEventHandlerToTag: (tag: types.JSXElement, eventKey: string, eventHandlerStatements: UIDLEventHandlerStatement[], params: JSXGenerationParams, options: JSXGenerationOptions, t?: typeof types) => void; | ||
export declare const createDynamicValueExpression: (identifier: UIDLDynamicReference, options: JSXGenerationOptions, t?: typeof types) => types.Identifier | types.MemberExpression; | ||
export declare const createDynamicValueExpression: (identifier: UIDLDynamicReference | UIDLGlobalReference, options: JSXGenerationOptions, t?: typeof types) => types.Identifier | types.MemberExpression; | ||
export declare const createConditionIdentifier: (dynamicReference: UIDLDynamicReference | UIDLExpressionValue, params: JSXGenerationParams, options: JSXGenerationOptions) => ConditionalIdentifier; | ||
@@ -7,0 +7,0 @@ export declare const createConditionalJSXExpression: (content: JSXASTReturnType, conditionalExpression: UIDLConditionalExpression, conditionalIdentifier: ConditionalIdentifier, t?: typeof types) => types.LogicalExpression; |
@@ -121,3 +121,8 @@ "use strict"; | ||
var identifierContent = identifier.content; | ||
var referenceType = identifierContent.referenceType, id = identifierContent.id; | ||
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)); | ||
}); | ||
if (referenceType === 'attr' || referenceType === 'children' || referenceType === 'token') { | ||
@@ -218,31 +223,9 @@ throw new Error("Dynamic reference type \"".concat(referenceType, "\" is not supported yet")); | ||
var stateValueIdentifier = (0, ast_utils_1.convertValueToLiteral)(operand, conditionalIdentifier.type); | ||
return t.binaryExpression(convertToBinaryOperator(operation), identifier, stateValueIdentifier); | ||
return t.binaryExpression((0, ast_utils_1.convertToBinaryOperator)(operation), identifier, stateValueIdentifier); | ||
} | ||
else { | ||
return operation ? t.unaryExpression(convertToUnaryOperator(operation), identifier) : identifier; | ||
return operation ? t.unaryExpression((0, ast_utils_1.convertToUnaryOperator)(operation), identifier) : identifier; | ||
} | ||
}; | ||
exports.createBinaryExpression = createBinaryExpression; | ||
/** | ||
* Because of the restrictions of the AST Types we need to have a clear subset of binary operators we can use | ||
* @param operation - the operation defined in the UIDL for the current state branch | ||
*/ | ||
var convertToBinaryOperator = function (operation) { | ||
var allowedOperations = ['===', '!==', '>=', '<=', '>', '<']; | ||
if (allowedOperations.includes(operation)) { | ||
return operation; | ||
} | ||
else { | ||
return '==='; | ||
} | ||
}; | ||
var convertToUnaryOperator = function (operation) { | ||
var allowedOperations = ['!']; | ||
if (allowedOperations.includes(operation)) { | ||
return operation; | ||
} | ||
else { | ||
return '!'; | ||
} | ||
}; | ||
var getRepeatSourceIdentifier = function (dataSource, options) { | ||
@@ -249,0 +232,0 @@ switch (dataSource.type) { |
import * as types from '@babel/types'; | ||
import ParsedASTNode from './parsed-ast'; | ||
import { UIDLStateDefinition, UIDLPropDefinition, UIDLRawValue, UIDLStaticValue, UIDLResourceItem, UIDLPropValue, UIDLExpressionValue, UIDLStateValue, HastNode } from '@teleporthq/teleport-types'; | ||
import { UnaryOperation, BinaryOperator } from './types'; | ||
/** | ||
@@ -53,3 +54,9 @@ * Adds a class definition string to an existing string of classes | ||
export declare const isJSXElement: (value: types.JSXElement | HastNode) => value is types.JSXElement; | ||
/** | ||
* Because of the restrictions of the AST Types we need to have a clear subset of binary operators we can use | ||
* @param operation - the operation defined in the UIDL for the current state branch | ||
*/ | ||
export declare const convertToBinaryOperator: (operation: string) => BinaryOperator; | ||
export declare const convertToUnaryOperator: (operation: string) => UnaryOperation; | ||
export {}; | ||
//# sourceMappingURL=ast-utils.d.ts.map |
@@ -38,3 +38,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.isJSXElement = exports.getExpressionFromUIDLExpressionNode = exports.resolveObjectValue = exports.computeFetchUrl = exports.generateURLParamsAST = exports.generateMemberExpressionASTFromPath = exports.generateMemberExpressionASTFromBase = exports.generateRemoteResourceASTs = exports.wrapObjectPropertiesWithExpression = exports.generateDynamicWindowImport = exports.createStateHookAST = exports.createReturnExpressionSyntax = exports.createPureComponent = exports.createClassComponent = exports.removeAttributeByName = exports.findAttributeByName = exports.getTSAnnotationForType = exports.addPropertyToASTObject = exports.convertValueToLiteral = exports.objectToObjectExpression = exports.renameJSXTag = exports.addSpreadAttributeToJSXTag = exports.addChildJSXText = exports.addChildJSXTag = exports.addRawAttributeToJSXTag = exports.addAttributeToJSXTag = exports.stringAsTemplateLiteral = exports.addMultipleDynamicAttributesToJSXTag = exports.addDynamicExpressionAttributeToJSXTag = exports.addDynamicAttributeToJSXTag = exports.addClassStringOnJSXTag = void 0; | ||
exports.convertToUnaryOperator = exports.convertToBinaryOperator = exports.isJSXElement = exports.getExpressionFromUIDLExpressionNode = exports.resolveObjectValue = exports.computeFetchUrl = exports.generateURLParamsAST = exports.generateMemberExpressionASTFromPath = exports.generateMemberExpressionASTFromBase = exports.generateRemoteResourceASTs = exports.wrapObjectPropertiesWithExpression = exports.generateDynamicWindowImport = exports.createStateHookAST = exports.createReturnExpressionSyntax = exports.createPureComponent = exports.createClassComponent = exports.removeAttributeByName = exports.findAttributeByName = exports.getTSAnnotationForType = exports.addPropertyToASTObject = exports.convertValueToLiteral = exports.objectToObjectExpression = exports.renameJSXTag = exports.addSpreadAttributeToJSXTag = exports.addChildJSXText = exports.addChildJSXTag = exports.addRawAttributeToJSXTag = exports.addAttributeToJSXTag = exports.stringAsTemplateLiteral = exports.addMultipleDynamicAttributesToJSXTag = exports.addDynamicExpressionAttributeToJSXTag = exports.addDynamicAttributeToJSXTag = exports.addClassStringOnJSXTag = void 0; | ||
var types = __importStar(require("@babel/types")); | ||
@@ -734,2 +734,26 @@ var core_1 = require("@babel/core"); | ||
exports.isJSXElement = isJSXElement; | ||
/** | ||
* Because of the restrictions of the AST Types we need to have a clear subset of binary operators we can use | ||
* @param operation - the operation defined in the UIDL for the current state branch | ||
*/ | ||
var convertToBinaryOperator = function (operation) { | ||
var allowedOperations = ['===', '!==', '>=', '<=', '>', '<']; | ||
if (allowedOperations.includes(operation)) { | ||
return operation; | ||
} | ||
else { | ||
return '==='; | ||
} | ||
}; | ||
exports.convertToBinaryOperator = convertToBinaryOperator; | ||
var convertToUnaryOperator = function (operation) { | ||
var allowedOperations = ['!']; | ||
if (allowedOperations.includes(operation)) { | ||
return operation; | ||
} | ||
else { | ||
return '!'; | ||
} | ||
}; | ||
exports.convertToUnaryOperator = convertToUnaryOperator; | ||
//# sourceMappingURL=ast-utils.js.map |
@@ -93,2 +93,4 @@ var __assign = (this && this.__assign) || function () { | ||
return generateSlotNode(node, params, templateSyntax); | ||
case 'expr': | ||
return 'Expression nodes are not supported'; | ||
default: | ||
@@ -99,2 +101,8 @@ throw new Error("generateHTMLSyntax encountered a node of unsupported type: ".concat(JSON.stringify(node, null, 2))); | ||
var generateDynamicNode = function (node, params, templateSyntax) { | ||
if (node.content.referenceType === 'global') { | ||
// TODO: Check this in the future. Not throwing an error for now | ||
console.info("Global dynamic values are not supported in the HTML generator"); | ||
var spanNode = createHTMLNode('span'); | ||
return spanNode; | ||
} | ||
var usedPropType = params.propDefinitions[node.content.id]; | ||
@@ -101,0 +109,0 @@ if (usedPropType && usedPropType.type === 'element') { |
@@ -58,3 +58,4 @@ import * as hastUtils from '../../utils/hast-utils'; | ||
case 'expr': | ||
throw new Error("Expressions are not supported in HTML templates"); | ||
// TODO: Check this in the future. Not throwing an error for now | ||
console.info("Expressions are not supported in HTML templates"); | ||
break; | ||
@@ -119,2 +120,5 @@ default: | ||
var operation = _a.operation, operand = _a.operand; | ||
// @todo | ||
// unlike jsx code generation, we are not converting the operand to binary or unary operation. | ||
// Please refer to https://github.com/teleporthq/teleport-code-generators/blob/development/packages/teleport-plugin-common/src/node-handlers/node-to-jsx/utils.ts#L303-L319 | ||
return "(".concat(stringifyConditionalExpression(conditionalKey, operation, operand), ")"); | ||
@@ -121,0 +125,0 @@ }); |
@@ -130,4 +130,3 @@ var __assign = (this && this.__assign) || function () { | ||
if (node === null || node === void 0 ? void 0 : node.dependency) { | ||
/* tslint:disable:no-string-literal */ | ||
params.dependencies['Script'] = node.dependency; | ||
params.dependencies.Script = node.dependency; | ||
} | ||
@@ -138,19 +137,45 @@ return [node.content.toString()]; | ||
case 'dynamic': | ||
// If the dynamic node is a prop and has a default value, | ||
// we should use it with a logical expression. And the most used case is for named-slots. | ||
var prop = params.propDefinitions[node.content.id]; | ||
if ((prop === null || prop === void 0 ? void 0 : prop.type) === 'element' && prop.defaultValue) { | ||
var prefix = options.dynamicReferencePrefixMap[node.content.referenceType] || ''; | ||
var propDefault = prop.defaultValue; | ||
var jsxNode = params.nodesLookup[propDefault.content.key]; | ||
if (jsxNode === undefined) { | ||
throw Error("Prop ".concat(node.content.id, " is of type element \n\n The JSXNode of the prop-").concat(node.content.id, " is missing from the nodesLookup")); | ||
switch (node.content.referenceType) { | ||
case 'prop': { | ||
// If the dynamic node is a prop and has a default value of type UIDLElementNode, | ||
// we should use it with a logical expression. | ||
var prop = params.propDefinitions[node.content.id]; | ||
if ((prop === null || prop === void 0 ? void 0 : prop.type) === 'element' && prop.defaultValue) { | ||
var prefix = options.dynamicReferencePrefixMap[node.content.referenceType] || ''; | ||
var propDefault = prop.defaultValue; | ||
var jsxNode = params.nodesLookup[propDefault.content.key]; | ||
if (jsxNode === undefined) { | ||
throw Error("Prop ".concat(node.content.id, " is of type element \n\n The JSXNode of the prop-").concat(node.content.id, " is missing from the nodesLookup")); | ||
} | ||
return [ | ||
types.logicalExpression('??', prefix === '' | ||
? types.identifier(node.content.id) | ||
: types.memberExpression(types.identifier(prefix), types.identifier(node.content.id)), jsxNode), | ||
]; | ||
} | ||
return [createDynamicValueExpression(node, options)]; | ||
} | ||
return [ | ||
types.logicalExpression('??', prefix === '' | ||
? types.identifier(node.content.id) | ||
: types.memberExpression(types.identifier(prefix), types.identifier(node.content.id)), jsxNode), | ||
]; | ||
case 'locale': { | ||
// Locale is not handled the same in all frameworks. | ||
// So, we need to handle it differently using individual plugins for each framework. | ||
var emptyExpression = types.jsxEmptyExpression(); | ||
emptyExpression.innerComments = [ | ||
{ | ||
type: 'CommentBlock', | ||
value: "locale-".concat(node.content.id), | ||
}, | ||
]; | ||
var expression = types.jsxExpressionContainer(emptyExpression); | ||
var jsxTag = createJSXTag('span'); | ||
addChildJSXTag(jsxTag, expression); | ||
params.localeReferences.push(jsxTag); | ||
return [jsxTag]; | ||
} | ||
case 'global': { | ||
params.globalReferences.push(node.content.id); | ||
return [createDynamicValueExpression(node, options)]; | ||
} | ||
default: | ||
return [createDynamicValueExpression(node, options)]; | ||
} | ||
return [createDynamicValueExpression(node, options)]; | ||
case 'cms-item': | ||
@@ -157,0 +182,0 @@ case 'cms-list': |
import * as types from '@babel/types'; | ||
import { UIDLPropDefinition, UIDLDependency, UIDLStateDefinition } from '@teleporthq/teleport-types'; | ||
import { UIDLPropDefinition, UIDLDependency, UIDLStateDefinition, UIDLGlobalReference } from '@teleporthq/teleport-types'; | ||
export interface JSXGenerationParams { | ||
propDefinitions: Record<string, UIDLPropDefinition>; | ||
stateDefinitions: Record<string, UIDLStateDefinition>; | ||
nodesLookup: Record<string, types.JSXElement>; | ||
nodesLookup: Record<string, types.JSXElement | types.JSXExpressionContainer>; | ||
dependencies: Record<string, UIDLDependency>; | ||
windowImports: Record<string, types.ExpressionStatement>; | ||
localeReferences: types.JSXElement[]; | ||
globalReferences: Array<UIDLGlobalReference['content']['id']>; | ||
} | ||
@@ -24,4 +26,2 @@ export interface JSXGenerationOptions { | ||
export type JSXASTReturnType = string | types.JSXExpressionContainer | types.JSXElement | types.LogicalExpression | types.Identifier | types.MemberExpression; | ||
export type BinaryOperator = '===' | '+' | '-' | '/' | '%' | '*' | '**' | '&' | '|' | '>>' | '>>>' | '<<' | '^' | '==' | '!=' | '!==' | 'in' | 'instanceof' | '>' | '<' | '>=' | '<='; | ||
export type UnaryOperation = '+' | '-' | 'void' | 'throw' | 'delete' | '!' | '~' | 'typeof'; | ||
export interface ConditionalIdentifier { | ||
@@ -28,0 +28,0 @@ key: string; |
import * as types from '@babel/types'; | ||
import { UIDLAttributeValue, UIDLDynamicReference, UIDLEventHandlerStatement, UIDLConditionalExpression, UIDLExpressionValue } from '@teleporthq/teleport-types'; | ||
import { UIDLAttributeValue, UIDLDynamicReference, UIDLEventHandlerStatement, UIDLConditionalExpression, UIDLExpressionValue, UIDLGlobalReference } from '@teleporthq/teleport-types'; | ||
import { JSXASTReturnType, ConditionalIdentifier, JSXGenerationParams, JSXGenerationOptions } from './types'; | ||
export declare const addEventHandlerToTag: (tag: types.JSXElement, eventKey: string, eventHandlerStatements: UIDLEventHandlerStatement[], params: JSXGenerationParams, options: JSXGenerationOptions, t?: typeof types) => void; | ||
export declare const createDynamicValueExpression: (identifier: UIDLDynamicReference, options: JSXGenerationOptions, t?: typeof types) => types.Identifier | types.MemberExpression; | ||
export declare const createDynamicValueExpression: (identifier: UIDLDynamicReference | UIDLGlobalReference, options: JSXGenerationOptions, t?: typeof types) => types.Identifier | types.MemberExpression; | ||
export declare const createConditionIdentifier: (dynamicReference: UIDLDynamicReference | UIDLExpressionValue, params: JSXGenerationParams, options: JSXGenerationOptions) => ConditionalIdentifier; | ||
@@ -7,0 +7,0 @@ export declare const createConditionalJSXExpression: (content: JSXASTReturnType, conditionalExpression: UIDLConditionalExpression, conditionalIdentifier: ConditionalIdentifier, t?: typeof types) => types.LogicalExpression; |
@@ -11,3 +11,3 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { | ||
import * as types from '@babel/types'; | ||
import { convertValueToLiteral } from '../../utils/ast-utils'; | ||
import { convertToBinaryOperator, convertToUnaryOperator, convertValueToLiteral, } from '../../utils/ast-utils'; | ||
import { StringUtils } from '@teleporthq/teleport-shared'; | ||
@@ -95,3 +95,8 @@ // Adds all the event handlers and all the instructions for each event handler | ||
var identifierContent = identifier.content; | ||
var referenceType = identifierContent.referenceType, id = identifierContent.id; | ||
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)); | ||
}); | ||
if (referenceType === 'attr' || referenceType === 'children' || referenceType === 'token') { | ||
@@ -195,24 +200,2 @@ throw new Error("Dynamic reference type \"".concat(referenceType, "\" is not supported yet")); | ||
}; | ||
/** | ||
* Because of the restrictions of the AST Types we need to have a clear subset of binary operators we can use | ||
* @param operation - the operation defined in the UIDL for the current state branch | ||
*/ | ||
var convertToBinaryOperator = function (operation) { | ||
var allowedOperations = ['===', '!==', '>=', '<=', '>', '<']; | ||
if (allowedOperations.includes(operation)) { | ||
return operation; | ||
} | ||
else { | ||
return '==='; | ||
} | ||
}; | ||
var convertToUnaryOperator = function (operation) { | ||
var allowedOperations = ['!']; | ||
if (allowedOperations.includes(operation)) { | ||
return operation; | ||
} | ||
else { | ||
return '!'; | ||
} | ||
}; | ||
export var getRepeatSourceIdentifier = function (dataSource, options) { | ||
@@ -219,0 +202,0 @@ switch (dataSource.type) { |
import * as types from '@babel/types'; | ||
import ParsedASTNode from './parsed-ast'; | ||
import { UIDLStateDefinition, UIDLPropDefinition, UIDLRawValue, UIDLStaticValue, UIDLResourceItem, UIDLPropValue, UIDLExpressionValue, UIDLStateValue, HastNode } from '@teleporthq/teleport-types'; | ||
import { UnaryOperation, BinaryOperator } from './types'; | ||
/** | ||
@@ -53,3 +54,9 @@ * Adds a class definition string to an existing string of classes | ||
export declare const isJSXElement: (value: types.JSXElement | HastNode) => value is types.JSXElement; | ||
/** | ||
* Because of the restrictions of the AST Types we need to have a clear subset of binary operators we can use | ||
* @param operation - the operation defined in the UIDL for the current state branch | ||
*/ | ||
export declare const convertToBinaryOperator: (operation: string) => BinaryOperator; | ||
export declare const convertToUnaryOperator: (operation: string) => UnaryOperation; | ||
export {}; | ||
//# sourceMappingURL=ast-utils.d.ts.map |
@@ -673,2 +673,24 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { | ||
}; | ||
/** | ||
* Because of the restrictions of the AST Types we need to have a clear subset of binary operators we can use | ||
* @param operation - the operation defined in the UIDL for the current state branch | ||
*/ | ||
export var convertToBinaryOperator = function (operation) { | ||
var allowedOperations = ['===', '!==', '>=', '<=', '>', '<']; | ||
if (allowedOperations.includes(operation)) { | ||
return operation; | ||
} | ||
else { | ||
return '==='; | ||
} | ||
}; | ||
export var convertToUnaryOperator = function (operation) { | ||
var allowedOperations = ['!']; | ||
if (allowedOperations.includes(operation)) { | ||
return operation; | ||
} | ||
else { | ||
return '!'; | ||
} | ||
}; | ||
//# sourceMappingURL=ast-utils.js.map |
{ | ||
"name": "@teleporthq/teleport-plugin-common", | ||
"version": "0.36.6", | ||
"version": "0.37.2", | ||
"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.36.6", | ||
"@teleporthq/teleport-types": "^0.36.6", | ||
"@teleporthq/teleport-shared": "^0.37.2", | ||
"@teleporthq/teleport-types": "^0.37.2", | ||
"jss": "^10.0.0", | ||
"jss-preset-default": "^10.0.0" | ||
}, | ||
"gitHead": "64a55c39dba17f13dc58805a7a28f74cd96dd03a" | ||
"gitHead": "bfc4e6229bcd9b3e5ce4077dbf6fd19666935187" | ||
} |
@@ -125,2 +125,5 @@ import * as hastUtils from '../../utils/hast-utils' | ||
case 'expr': | ||
return 'Expression nodes are not supported' | ||
default: | ||
@@ -142,2 +145,9 @@ throw new Error( | ||
) => { | ||
if (node.content.referenceType === 'global') { | ||
// TODO: Check this in the future. Not throwing an error for now | ||
console.info(`Global dynamic values are not supported in the HTML generator`) | ||
const spanNode = createHTMLNode('span') | ||
return spanNode | ||
} | ||
const usedPropType = params.propDefinitions[node.content.id] | ||
@@ -144,0 +154,0 @@ if (usedPropType && usedPropType.type === 'element') { |
@@ -88,3 +88,4 @@ import * as hastUtils from '../../utils/hast-utils' | ||
case 'expr': | ||
throw new Error(`Expressions are not supported in HTML templates`) | ||
// TODO: Check this in the future. Not throwing an error for now | ||
console.info(`Expressions are not supported in HTML templates`) | ||
break | ||
@@ -185,2 +186,5 @@ | ||
const stringConditions = conditions.map(({ operation, operand }) => { | ||
// @todo | ||
// unlike jsx code generation, we are not converting the operand to binary or unary operation. | ||
// Please refer to https://github.com/teleporthq/teleport-code-generators/blob/development/packages/teleport-plugin-common/src/node-handlers/node-to-jsx/utils.ts#L303-L319 | ||
return `(${stringifyConditionalExpression(conditionalKey, operation, operand)})` | ||
@@ -187,0 +191,0 @@ }) |
@@ -208,4 +208,3 @@ import * as types from '@babel/types' | ||
if (node?.dependency) { | ||
/* tslint:disable:no-string-literal */ | ||
params.dependencies['Script'] = node.dependency | ||
params.dependencies.Script = node.dependency | ||
} | ||
@@ -218,32 +217,62 @@ return [node.content.toString()] | ||
case 'dynamic': | ||
// If the dynamic node is a prop and has a default value, | ||
// we should use it with a logical expression. And the most used case is for named-slots. | ||
const prop = params.propDefinitions[node.content.id] | ||
if (prop?.type === 'element' && prop.defaultValue) { | ||
const prefix = | ||
options.dynamicReferencePrefixMap[ | ||
node.content.referenceType as 'prop' | 'state' | 'local' | ||
] || '' | ||
switch (node.content.referenceType) { | ||
case 'prop': { | ||
// If the dynamic node is a prop and has a default value of type UIDLElementNode, | ||
// we should use it with a logical expression. | ||
const prop = params.propDefinitions[node.content.id] | ||
if (prop?.type === 'element' && prop.defaultValue) { | ||
const prefix = options.dynamicReferencePrefixMap[node.content.referenceType] || '' | ||
const propDefault = prop.defaultValue as UIDLElementNode | ||
const jsxNode = params.nodesLookup[propDefault.content.key] | ||
const propDefault = prop.defaultValue as UIDLElementNode | ||
const jsxNode = params.nodesLookup[propDefault.content.key] | ||
if (jsxNode === undefined) { | ||
throw Error(`Prop ${node.content.id} is of type element \n | ||
The JSXNode of the prop-${node.content.id} is missing from the nodesLookup`) | ||
if (jsxNode === undefined) { | ||
throw Error(`Prop ${node.content.id} is of type element \n | ||
The JSXNode of the prop-${node.content.id} is missing from the nodesLookup`) | ||
} | ||
return [ | ||
types.logicalExpression( | ||
'??', | ||
prefix === '' | ||
? types.identifier(node.content.id) | ||
: types.memberExpression( | ||
types.identifier(prefix), | ||
types.identifier(node.content.id) | ||
), | ||
jsxNode as types.JSXElement | ||
), | ||
] | ||
} | ||
return [createDynamicValueExpression(node, options)] | ||
} | ||
return [ | ||
types.logicalExpression( | ||
'??', | ||
prefix === '' | ||
? types.identifier(node.content.id) | ||
: types.memberExpression(types.identifier(prefix), types.identifier(node.content.id)), | ||
jsxNode as types.JSXElement | ||
), | ||
] | ||
case 'locale': { | ||
// Locale is not handled the same in all frameworks. | ||
// So, we need to handle it differently using individual plugins for each framework. | ||
const emptyExpression = types.jsxEmptyExpression() | ||
emptyExpression.innerComments = [ | ||
{ | ||
type: 'CommentBlock', | ||
value: `locale-${node.content.id}`, | ||
}, | ||
] | ||
const expression = types.jsxExpressionContainer(emptyExpression) | ||
const jsxTag = createJSXTag('span') | ||
addChildJSXTag(jsxTag, expression) | ||
params.localeReferences.push(jsxTag) | ||
return [jsxTag] | ||
} | ||
case 'global': { | ||
params.globalReferences.push(node.content.id) | ||
return [createDynamicValueExpression(node, options)] | ||
} | ||
default: | ||
return [createDynamicValueExpression(node, options)] | ||
} | ||
return [createDynamicValueExpression(node, options)] | ||
case 'cms-item': | ||
@@ -250,0 +279,0 @@ case 'cms-list': |
import * as types from '@babel/types' | ||
import { UIDLPropDefinition, UIDLDependency, UIDLStateDefinition } from '@teleporthq/teleport-types' | ||
import { | ||
UIDLPropDefinition, | ||
UIDLDependency, | ||
UIDLStateDefinition, | ||
UIDLGlobalReference, | ||
} from '@teleporthq/teleport-types' | ||
@@ -8,5 +13,7 @@ export interface JSXGenerationParams { | ||
stateDefinitions: Record<string, UIDLStateDefinition> | ||
nodesLookup: Record<string, types.JSXElement> | ||
nodesLookup: Record<string, types.JSXElement | types.JSXExpressionContainer> | ||
dependencies: Record<string, UIDLDependency> | ||
windowImports: Record<string, types.ExpressionStatement> | ||
localeReferences: types.JSXElement[] | ||
globalReferences: Array<UIDLGlobalReference['content']['id']> | ||
} | ||
@@ -69,28 +76,2 @@ | ||
export type BinaryOperator = | ||
| '===' | ||
| '+' | ||
| '-' | ||
| '/' | ||
| '%' | ||
| '*' | ||
| '**' | ||
| '&' | ||
| '|' | ||
| '>>' | ||
| '>>>' | ||
| '<<' | ||
| '^' | ||
| '==' | ||
| '!=' | ||
| '!==' | ||
| 'in' | ||
| 'instanceof' | ||
| '>' | ||
| '<' | ||
| '>=' | ||
| '<=' | ||
export type UnaryOperation = '+' | '-' | 'void' | 'throw' | 'delete' | '!' | '~' | 'typeof' | ||
export interface ConditionalIdentifier { | ||
@@ -97,0 +78,0 @@ key: string |
import * as types from '@babel/types' | ||
import { convertValueToLiteral } from '../../utils/ast-utils' | ||
import { | ||
convertToBinaryOperator, | ||
convertToUnaryOperator, | ||
convertValueToLiteral, | ||
} from '../../utils/ast-utils' | ||
import { StringUtils } from '@teleporthq/teleport-shared' | ||
@@ -15,7 +19,6 @@ import { | ||
UIDLExpressionValue, | ||
UIDLGlobalReference, | ||
} from '@teleporthq/teleport-types' | ||
import { | ||
BinaryOperator, | ||
UnaryOperation, | ||
JSXASTReturnType, | ||
@@ -148,3 +151,3 @@ ConditionalIdentifier, | ||
export const createDynamicValueExpression = ( | ||
identifier: UIDLDynamicReference, | ||
identifier: UIDLDynamicReference | UIDLGlobalReference, | ||
options: JSXGenerationOptions, | ||
@@ -154,4 +157,10 @@ t = types | ||
const identifierContent = identifier.content | ||
const { referenceType, id } = identifierContent | ||
const refPath = identifier.content.refPath || [] | ||
const { referenceType } = identifierContent | ||
let id = identifierContent.id | ||
refPath?.forEach((pathItem) => { | ||
id = id.concat(`?.${pathItem}`) | ||
}) | ||
if (referenceType === 'attr' || referenceType === 'children' || referenceType === 'token') { | ||
@@ -302,24 +311,2 @@ throw new Error(`Dynamic reference type "${referenceType}" is not supported yet`) | ||
/** | ||
* Because of the restrictions of the AST Types we need to have a clear subset of binary operators we can use | ||
* @param operation - the operation defined in the UIDL for the current state branch | ||
*/ | ||
const convertToBinaryOperator = (operation: string): BinaryOperator => { | ||
const allowedOperations = ['===', '!==', '>=', '<=', '>', '<'] | ||
if (allowedOperations.includes(operation)) { | ||
return operation as BinaryOperator | ||
} else { | ||
return '===' | ||
} | ||
} | ||
const convertToUnaryOperator = (operation: string): UnaryOperation => { | ||
const allowedOperations = ['!'] | ||
if (allowedOperations.includes(operation)) { | ||
return operation as UnaryOperation | ||
} else { | ||
return '!' | ||
} | ||
} | ||
export const getRepeatSourceIdentifier = ( | ||
@@ -326,0 +313,0 @@ dataSource: UIDLAttributeValue, |
@@ -18,2 +18,3 @@ import * as types from '@babel/types' | ||
import babelPresetReact from '@babel/preset-react' | ||
import { UnaryOperation, BinaryOperator } from './types' | ||
@@ -1092,1 +1093,23 @@ /** | ||
value.type === 'JSXElement' | ||
/** | ||
* Because of the restrictions of the AST Types we need to have a clear subset of binary operators we can use | ||
* @param operation - the operation defined in the UIDL for the current state branch | ||
*/ | ||
export const convertToBinaryOperator = (operation: string): BinaryOperator => { | ||
const allowedOperations = ['===', '!==', '>=', '<=', '>', '<'] | ||
if (allowedOperations.includes(operation)) { | ||
return operation as BinaryOperator | ||
} else { | ||
return '===' | ||
} | ||
} | ||
export const convertToUnaryOperator = (operation: string): UnaryOperation => { | ||
const allowedOperations = ['!'] | ||
if (allowedOperations.includes(operation)) { | ||
return operation as UnaryOperation | ||
} else { | ||
return '!' | ||
} | ||
} |
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
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
669536
169
8902
+ Added@teleporthq/teleport-shared@0.37.6(transitive)
+ Added@teleporthq/teleport-types@0.37.6(transitive)
+ Addedelectron-to-chromium@1.5.113(transitive)
- Removed@teleporthq/teleport-shared@0.36.6(transitive)
- Removed@teleporthq/teleport-types@0.36.6(transitive)
- Removedelectron-to-chromium@1.5.114(transitive)