custom-functions-metadata
Advanced tools
Comparing version 1.6.2 to 1.7.0
@@ -44,2 +44,22 @@ // Copyright (c) Microsoft Corporation. All rights reserved. | ||
const TYPE_CUSTOM_FUNCTION_INVOCATION = "customfunctions.invocation"; | ||
// These does not work if the developer is using namespace/type alias. To support that, we'd need to | ||
// use typeChecker.getFullyQualifiedName, which in turns requires creating a program out of the source file | ||
// (which we don't do now) and passing the type checker all the way down to getParamType. This would be a | ||
// larger refactor (e.g., rewriting parseTree into a class) and is tracked as a work item. | ||
const CELLVALUETYPE_MAPPINGS = { | ||
"Excel.CellValue": "cellvalue", | ||
"Excel.BooleanCellValue": "booleancellvalue", | ||
"Excel.DoubleCellValue": "doublecellvalue", | ||
"Excel.EntityCellValue": "entitycellvalue", | ||
"Excel.ErrorCellValue": "errorcellvalue", | ||
"Excel.FormattedNumberCellValue": "formattednumbercellvalue", | ||
"Excel.LinkedEntityCellValue": "linkedentitycellvalue", | ||
"Excel.LocalImageCellValue": "localimagecellvalue", | ||
"Excel.StringCellValue": "stringcellvalue", | ||
"Excel.WebImageCellValue": "webimagecellvalue", | ||
"Excel.ArrayCellValue": "unsupported", | ||
"Excel.EmptyCellValue": "unsupported", | ||
"Excel.ReferenceCellValue": "unsupported", | ||
"Excel.ValueTypeNotAvailableCellValue": "unsupported", | ||
}; | ||
/** | ||
@@ -430,2 +450,6 @@ * Takes the sourceCode and attempts to parse the functions information | ||
}; | ||
// We convert cell value types to "any". | ||
if (Object.values(CELLVALUETYPE_MAPPINGS).includes(resultType)) { | ||
resultType = "any"; | ||
} | ||
// Only return dimensionality = matrix. Default assumed scalar | ||
@@ -780,38 +804,68 @@ if (resultDim === "scalar") { | ||
* Gets the parameter type of the node | ||
* @param t TypeNode | ||
* @param node TypeNode | ||
*/ | ||
function getParamType(t, extra, enumList) { | ||
function getParamType(node, extra, enumList) { | ||
let type = "any"; | ||
// Only get type for typescript files. js files will return any for all types | ||
if (t) { | ||
let kind = t.kind; | ||
const typePosition = getPosition(t); | ||
if (ts.isTypeReferenceNode(t) || ts.isArrayTypeNode(t)) { | ||
let arrayType = { | ||
dimensionality: 0, | ||
type: ts.SyntaxKind.AnyKeyword, | ||
}; | ||
if (ts.isTypeReferenceNode(t)) { | ||
const array = t; | ||
if (enumList.indexOf(array.typeName.getText()) >= 0) { | ||
// Type found in the enumList | ||
return type; | ||
} | ||
if (array.typeName.getText() !== "Array") { | ||
extra.errors.push(logError("Invalid type: " + array.typeName.getText(), typePosition)); | ||
return type; | ||
} | ||
} | ||
arrayType = getArrayDimensionalityAndType(t); | ||
kind = arrayType.type; | ||
// Only get type for typescript files. js files will return "any" for all types | ||
if (!node) { | ||
return type; | ||
} | ||
const typePosition = getPosition(node); | ||
// Get the inner type node if it is an array | ||
if (typeNodeIsArray(node)) { | ||
let arrayType = { | ||
dimensionality: 0, | ||
node, | ||
}; | ||
arrayType = getArrayDimensionalityAndType(node); | ||
node = arrayType.node; | ||
} | ||
// We currently accept the following types of reference node: enum will be converted to "any", | ||
// Excel.CellValue will be converted accordingly. Anything else is invalid. (Array reference node has already been covered above.) | ||
if (ts.isTypeReferenceNode(node)) { | ||
const typeReferenceNode = node; | ||
let nodeTypeName = typeReferenceNode.typeName.getText(); | ||
if (enumList.indexOf(nodeTypeName) >= 0) { | ||
// Type found in the enumList | ||
return type; | ||
} | ||
// @ts-ignore | ||
type = TYPE_MAPPINGS[kind]; | ||
if (!type) { | ||
extra.errors.push(logError("Type doesn't match mappings", typePosition)); | ||
if (CELLVALUETYPE_MAPPINGS[nodeTypeName]) { | ||
// @ts-ignore | ||
let cellValue = CELLVALUETYPE_MAPPINGS[nodeTypeName]; | ||
if (cellValue === "unsupported") { | ||
const errorString = `Custom function does not support cell value type: ${typeReferenceNode.typeName.getText()}`; | ||
extra.errors.push(logError(errorString, typePosition)); | ||
return type; | ||
} | ||
return cellValue; | ||
} | ||
const errorString = `Custom function does not support type "${typeReferenceNode.typeName.getText()}" as input or return parameter.`; | ||
extra.errors.push(logError(errorString, typePosition)); | ||
return type; | ||
} | ||
// @ts-ignore | ||
type = TYPE_MAPPINGS[node.kind]; | ||
if (!type) { | ||
extra.errors.push(logError("Type doesn't match mappings", typePosition)); | ||
} | ||
return type; | ||
} | ||
/** | ||
* Helper function that checks whether a TypeNode is an array. | ||
* There are two cases: Array<sometype> or sometype[]. | ||
* @param node TypeNode | ||
*/ | ||
function typeNodeIsArray(node) { | ||
// [] case | ||
if (ts.isArrayTypeNode(node)) { | ||
return true; | ||
} | ||
// Array<sometype> case | ||
if (ts.isTypeReferenceNode(node)) { | ||
return node.typeName.getText() === "Array"; | ||
} | ||
return false; | ||
} | ||
/** | ||
* Wrapper function which will return the dimensionality and type of the array | ||
@@ -823,3 +877,3 @@ * @param node TypeNode | ||
dimensionality: 0, | ||
type: ts.SyntaxKind.AnyKeyword, | ||
node, | ||
}; | ||
@@ -835,3 +889,3 @@ if (ts.isArrayTypeNode(node)) { | ||
/** | ||
* Returns the dimensionality and type of array for TypeNode | ||
* Returns the dimensionality and type of array for TypeNode (sometype[] or higher dimension) | ||
* @param node TypeNode | ||
@@ -842,10 +896,10 @@ */ | ||
dimensionality: 1, | ||
type: ts.SyntaxKind.AnyKeyword, | ||
node, | ||
}; | ||
let nodeCheck = node.elementType; | ||
array.type = nodeCheck.kind; | ||
array.node = nodeCheck; | ||
while (ts.isArrayTypeNode(nodeCheck)) { | ||
array.dimensionality++; | ||
++array.dimensionality; | ||
nodeCheck = nodeCheck.elementType; | ||
array.type = nodeCheck.kind; | ||
array.node = nodeCheck; | ||
} | ||
@@ -855,3 +909,3 @@ return array; | ||
/** | ||
* Returns the dimensionality and type of array for ReferenceNode | ||
* Returns the dimensionality and type of array for ReferenceNode (Array<sometype> or higher dimension) | ||
* @param node TypeReferenceNode | ||
@@ -862,17 +916,15 @@ */ | ||
dimensionality: 0, | ||
type: ts.SyntaxKind.AnyKeyword, | ||
node, | ||
}; | ||
if (node.typeArguments && node.typeArguments.length === 1) { | ||
let nodeCheck = node; | ||
let dimensionalityCount = 1; | ||
while (nodeCheck.typeArguments) { | ||
// @ts-ignore | ||
if (TYPE_MAPPINGS_SIMPLE[nodeCheck.typeArguments[0].kind]) { | ||
array.dimensionality = dimensionalityCount; | ||
array.type = nodeCheck.typeArguments[0].kind; | ||
} | ||
nodeCheck = nodeCheck.typeArguments[0]; | ||
dimensionalityCount++; | ||
} | ||
let nodeCheck = node; | ||
let dimensionalityCount = 0; | ||
while (ts.isTypeReferenceNode(nodeCheck) && | ||
nodeCheck.typeName.getText() === "Array" && | ||
nodeCheck.typeArguments && | ||
nodeCheck.typeArguments.length === 1) { | ||
nodeCheck = nodeCheck.typeArguments[0]; | ||
++dimensionalityCount; | ||
} | ||
array.dimensionality = dimensionalityCount; | ||
array.node = nodeCheck; | ||
return array; | ||
@@ -879,0 +931,0 @@ } |
{ | ||
"name": "custom-functions-metadata", | ||
"version": "1.6.2", | ||
"version": "1.7.0", | ||
"description": "Generate metadata for Excel Custom Functions.", | ||
@@ -31,3 +31,3 @@ "main": "./lib/main.js", | ||
"nconf": "^0.12.0", | ||
"office-addin-usage-data": "^1.6.11", | ||
"office-addin-usage-data": "^1.6.12", | ||
"xregexp": "^4.3.0" | ||
@@ -43,4 +43,4 @@ }, | ||
"mocha": "^9.1.1", | ||
"office-addin-lint": "^2.3.2", | ||
"office-addin-prettier-config": "^1.2.0", | ||
"office-addin-lint": "^2.3.3", | ||
"office-addin-prettier-config": "^1.2.1", | ||
"rimraf": "^3.0.2", | ||
@@ -58,3 +58,3 @@ "ts-node": "^10.9.1", | ||
"prettier": "office-addin-prettier-config", | ||
"gitHead": "fb1b94d0e3654680aa67088d098f4719e94484cd" | ||
"gitHead": "223e80a361c4a9e6176f34a0b48a801bcc9b6f89" | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
90473
1234