diff --git a/typescript/vrotsc/src/compiler/transformer/codeTransformers/shims.ts b/typescript/vrotsc/src/compiler/transformer/codeTransformers/shims.ts index f375e1df2..2c02568c0 100644 --- a/typescript/vrotsc/src/compiler/transformer/codeTransformers/shims.ts +++ b/typescript/vrotsc/src/compiler/transformer/codeTransformers/shims.ts @@ -27,31 +27,31 @@ import { createDeclarationPrologueStatements } from "./prologueStatements"; * Containst the names of the shims that are used to replace the built-in objects. */ const ShimReferences: Record = { - "Map": { - typeName: "MapConstructor", - facts: HierarchyFacts.ContainsMap, - }, - "WeakMap": { - typeName: "WeakMapConstructor", - facts: HierarchyFacts.ContainsWeakMap, - }, - "Set": { - typeName: "SetConstructor", - facts: HierarchyFacts.ContainsSet, - }, - "WeakSet": { - typeName: "WeakSetConstructor", - facts: HierarchyFacts.ContainsWeakSet, - }, - "Promise": { - typeName: "PromiseConstructor", - facts: HierarchyFacts.ContainsPromise, - }, + "Map": { + typeName: "MapConstructor", + facts: HierarchyFacts.ContainsMap, + }, + "WeakMap": { + typeName: "WeakMapConstructor", + facts: HierarchyFacts.ContainsWeakMap, + }, + "Set": { + typeName: "SetConstructor", + facts: HierarchyFacts.ContainsSet, + }, + "WeakSet": { + typeName: "WeakSetConstructor", + facts: HierarchyFacts.ContainsWeakSet, + }, + "Promise": { + typeName: "PromiseConstructor", + facts: HierarchyFacts.ContainsPromise, + }, }; interface ShimReference { - typeName: string; - facts: HierarchyFacts; + typeName: string; + facts: HierarchyFacts; } /** @@ -76,57 +76,57 @@ interface ShimReference { * @returns - The transformed source file. */ export function transformShims(sourceFile: ts.SourceFile, context: ScriptTransformationContext): ts.SourceFile { - const visitor = new NodeVisitor(visitNode, context); + const visitor = new NodeVisitor(visitNode, context); - return visitSourceFile(sourceFile); + return visitSourceFile(sourceFile); - function visitNode(node: ts.Node): ts.VisitResult { - switch (node.kind) { - case ts.SyntaxKind.CallExpression: - return visitCallExpression(node); + function visitNode(node: ts.Node): ts.VisitResult { + switch (node.kind) { + case ts.SyntaxKind.CallExpression: + return visitCallExpression(node); - case ts.SyntaxKind.Identifier: - return visitIdentifier(node); - } - } + case ts.SyntaxKind.Identifier: + return visitIdentifier(node); + } + } /** * Visits the entire source file and replaces variables declarations with the VROES shims. */ - function visitIdentifier(node: ts.Identifier): ts.Identifier { - const shimRef = ShimReferences[node.text]; - if (shimRef) { - const symbol = context.typeChecker.getSymbolAtLocation(node); - if (symbol && symbol.valueDeclaration && symbol.valueDeclaration.kind === ts.SyntaxKind.VariableDeclaration) { - const symbolVarDecl = symbol.valueDeclaration as ts.VariableDeclaration; - if (symbolVarDecl.type && symbolVarDecl.type.kind === ts.SyntaxKind.TypeReference) { - const typeRef = symbolVarDecl.type as ts.TypeReferenceNode; - if (getIdentifierTextOrNull(typeRef.typeName) === shimRef.typeName) { - context.file.hierarchyFacts |= shimRef.facts; - } - } - } - } + function visitIdentifier(node: ts.Identifier): ts.Identifier { + const shimRef = ShimReferences[node.text]; + if (shimRef) { + const symbol = context.typeChecker.getSymbolAtLocation(node); + if (symbol && symbol.valueDeclaration && symbol.valueDeclaration.kind === ts.SyntaxKind.VariableDeclaration) { + const symbolVarDecl = symbol.valueDeclaration as ts.VariableDeclaration; + if (symbolVarDecl.type && symbolVarDecl.type.kind === ts.SyntaxKind.TypeReference) { + const typeRef = symbolVarDecl.type as ts.TypeReferenceNode; + if (getIdentifierTextOrNull(typeRef.typeName) === shimRef.typeName) { + context.file.hierarchyFacts |= shimRef.facts; + } + } + } + } - return visitor.visitEachChild(node); - } + return visitor.visitEachChild(node); + } /** * Will place the prologue statements at the top of the file and replace the built-in objects with the VROES shims. */ - function visitSourceFile(node: ts.SourceFile): ts.SourceFile { - const statements = visitor.visitNodes(node.statements); - const prologue = createDeclarationPrologueStatements(context); + function visitSourceFile(node: ts.SourceFile): ts.SourceFile { + const statements = visitor.visitNodes(node.statements); + const prologue = createDeclarationPrologueStatements(context); - return ts.updateSourceFileNode( - node, - ts.setTextRange( - ts.createNodeArray([ - ...prologue, - ...statements, - ]), - node.statements)); - } + return ts.updateSourceFileNode( + node, + ts.setTextRange( + ts.createNodeArray([ + ...prologue, + ...statements, + ]), + node.statements)); + } /** * Visits the entire source file and replaces some built-in methods with the VROES shims. @@ -142,42 +142,42 @@ export function transformShims(sourceFile: ts.SourceFile, context: ScriptTransfo * @param node - The node to visit. * @returns - The result of visiting the node. */ - function visitCallExpression(node: ts.CallExpression): ts.Expression { - const symbol = context.typeChecker.getSymbolAtLocation(node.expression); - if (symbol) { - switch (context.typeChecker.getFullyQualifiedName(symbol)) { - case "String.startsWith": - return shimInstanceCall("stringStartsWith", node); - case "String.endsWith": - return shimInstanceCall("stringEndsWith", node); - case "String.includes": - return shimInstanceCall("stringIncludes", node); - case "String.repeat": - return shimInstanceCall("stringRepeat", node); - case "String.padStart": - return shimInstanceCall("stringPadStart", node); - case "String.padEnd": - return shimInstanceCall("stringPadEnd", node); - case "Array.find": - return shimInstanceCall("arrayFind", node); - case "Array.findIndex": - return shimInstanceCall("arrayFindIndex", node); - case "Array.fill": - return shimInstanceCall("arrayFill", node); - case "ArrayConstructor.from": - return shimStaticCall("arrayFrom", node); - case "ArrayConstructor.of": - return shimStaticCall("arrayOf", node); - case "ObjectConstructor.assign": - return shimStaticCall("objectAssign", node); - case "ObjectConstructor.setPrototypeOf": - return shimStaticCall("objectSetPrototypeOf", node); - } - } + function visitCallExpression(node: ts.CallExpression): ts.Expression { + const symbol = context.typeChecker.getSymbolAtLocation(node.expression); + if (symbol) { + switch (context.typeChecker.getFullyQualifiedName(symbol)) { + case "String.startsWith": + return shimInstanceCall("stringStartsWith", node); + case "String.endsWith": + return shimInstanceCall("stringEndsWith", node); + case "String.includes": + return shimInstanceCall("stringIncludes", node); + case "String.repeat": + return shimInstanceCall("stringRepeat", node); + case "String.padStart": + return shimInstanceCall("stringPadStart", node); + case "String.padEnd": + return shimInstanceCall("stringPadEnd", node); + case "Array.find": + return shimInstanceCall("arrayFind", node); + case "Array.findIndex": + return shimInstanceCall("arrayFindIndex", node); + case "Array.fill": + return shimInstanceCall("arrayFill", node); + case "ArrayConstructor.from": + return shimStaticCall("arrayFrom", node); + case "ArrayConstructor.of": + return shimStaticCall("arrayOf", node); + case "ObjectConstructor.assign": + return shimStaticCall("objectAssign", node); + case "ObjectConstructor.setPrototypeOf": + return shimStaticCall("objectSetPrototypeOf", node); + } + } - // Visit the children of the node. - return visitor.visitEachChild(node); - } + // Visit the children of the node. + return visitor.visitEachChild(node); + } /** @@ -186,42 +186,42 @@ export function transformShims(sourceFile: ts.SourceFile, context: ScriptTransfo * if the node is a call expression and the expression is a property access expression, * then it will be replaced with the VROES shims. */ - function shimInstanceCall(methodName: string, node: ts.CallExpression): ts.CallExpression { - if (node.expression.kind !== ts.SyntaxKind.PropertyAccessExpression) { - return visitor.visitEachChild(node); - } + function shimInstanceCall(methodName: string, node: ts.CallExpression): ts.CallExpression { + if (node.expression.kind !== ts.SyntaxKind.PropertyAccessExpression) { + return visitor.visitEachChild(node); + } - context.file.hierarchyFacts |= HierarchyFacts.ContainsPolyfills; + context.file.hierarchyFacts |= HierarchyFacts.ContainsPolyfills; - let args: ts.Expression[] = []; - args.push((node.expression).expression); - node.arguments.forEach(n => args.push(n)); + let args: ts.Expression[] = []; + args.push((node.expression).expression); + node.arguments.forEach(n => args.push(n)); - return ts.createCall( - ts.createPropertyAccess( - ts.createPropertyAccess( - ts.createIdentifier("VROES"), - "Shims"), - methodName), - undefined, - visitor.visitNodes(ts.createNodeArray(args, false))); - } + return ts.createCall( + ts.createPropertyAccess( + ts.createPropertyAccess( + ts.createIdentifier("VROES"), + "Shims"), + methodName), + undefined, + visitor.visitNodes(ts.createNodeArray(args, false))); + } /** * Same as shimInstanceCall but for static calls. */ - function shimStaticCall(methodName: string, node: ts.CallExpression): ts.CallExpression { - context.file.hierarchyFacts |= HierarchyFacts.ContainsPolyfills; + function shimStaticCall(methodName: string, node: ts.CallExpression): ts.CallExpression { + context.file.hierarchyFacts |= HierarchyFacts.ContainsPolyfills; - return ts.createCall( - ts.createPropertyAccess( - ts.createPropertyAccess( - ts.createIdentifier("VROES"), - "Shims"), - methodName), - undefined, - visitor.visitNodes(node.arguments)); - } + return ts.createCall( + ts.createPropertyAccess( + ts.createPropertyAccess( + ts.createIdentifier("VROES"), + "Shims"), + methodName), + undefined, + visitor.visitNodes(node.arguments)); + } } /** @@ -244,49 +244,49 @@ export function transformShims(sourceFile: ts.SourceFile, context: ScriptTransfo * @returns - The transformed source file. */ export function transformShimsBefore(sourceFile: ts.SourceFile, context: ScriptTransformationContext): ts.SourceFile { - const visitor = new NodeVisitor(visitNode, context); + const visitor = new NodeVisitor(visitNode, context); - return visitSourceFile(sourceFile); + return visitSourceFile(sourceFile); - function visitNode(node: ts.Node): ts.VisitResult { - switch (node.kind) { - case ts.SyntaxKind.ArrayLiteralExpression: - return visitArrayLiteralExpression(node); - case ts.SyntaxKind.ObjectLiteralExpression: - return visitObjectLiteralExpression(node); - } - } + function visitNode(node: ts.Node): ts.VisitResult { + switch (node.kind) { + case ts.SyntaxKind.ArrayLiteralExpression: + return visitArrayLiteralExpression(node); + case ts.SyntaxKind.ObjectLiteralExpression: + return visitObjectLiteralExpression(node); + } + } /** * Visits the entire source file and replaces the spread elements with the VROES shims. */ - function visitSourceFile(node: ts.SourceFile): ts.SourceFile { - const statements = visitor.visitNodes(node.statements); + function visitSourceFile(node: ts.SourceFile): ts.SourceFile { + const statements = visitor.visitNodes(node.statements); - return ts.updateSourceFileNode( - node, - ts.setTextRange( - ts.createNodeArray([ - ...statements, - ]), - node.statements)); - } + return ts.updateSourceFileNode( + node, + ts.setTextRange( + ts.createNodeArray([ + ...statements, + ]), + node.statements)); + } - function visitArrayLiteralExpression(node: ts.ArrayLiteralExpression): ts.Node { - const hasSpreadElement = node.elements.some(e => e.kind === ts.SyntaxKind.SpreadElement); - if (hasSpreadElement) { - context.file.hierarchyFacts |= HierarchyFacts.ContainsSpreadArray; - return transformSpreadElement(visitor, node); - } - return node; - } + function visitArrayLiteralExpression(node: ts.ArrayLiteralExpression): ts.Node { + const hasSpreadElement = node.elements.some(e => e.kind === ts.SyntaxKind.SpreadElement); + if (hasSpreadElement) { + context.file.hierarchyFacts |= HierarchyFacts.ContainsSpreadArray; + return transformSpreadElement(visitor, node); + } + return node; + } - function visitObjectLiteralExpression(node: ts.ObjectLiteralExpression): ts.Node { - const hasSpreadAssignment = node.properties.some(e => e.kind === ts.SyntaxKind.SpreadAssignment); - if (hasSpreadAssignment) { - context.file.hierarchyFacts |= HierarchyFacts.ContainsSpreadOperator; - return transformSpreadAssignment(visitor, node); - } - return node; - } + function visitObjectLiteralExpression(node: ts.ObjectLiteralExpression): ts.Node { + const hasSpreadAssignment = node.properties.some(e => e.kind === ts.SyntaxKind.SpreadAssignment); + if (hasSpreadAssignment) { + context.file.hierarchyFacts |= HierarchyFacts.ContainsSpreadOperator; + return transformSpreadAssignment(visitor, node); + } + return node; + } }