Skip to content

Commit

Permalink
style: Fixed tabs > spaces in shims.ts
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Genov <stefan.genov@broadcom.com>
  • Loading branch information
Michaelpalacce committed May 28, 2024
1 parent 6e32c21 commit f77c9ee
Showing 1 changed file with 159 additions and 159 deletions.
318 changes: 159 additions & 159 deletions typescript/vrotsc/src/compiler/transformer/codeTransformers/shims.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, ShimReference> = {
"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;
}

/**
Expand All @@ -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<ts.Node> {
switch (node.kind) {
case ts.SyntaxKind.CallExpression:
return visitCallExpression(<ts.CallExpression>node);
function visitNode(node: ts.Node): ts.VisitResult<ts.Node> {
switch (node.kind) {
case ts.SyntaxKind.CallExpression:
return visitCallExpression(<ts.CallExpression>node);

case ts.SyntaxKind.Identifier:
return visitIdentifier(<ts.Identifier>node);
}
}
case ts.SyntaxKind.Identifier:
return visitIdentifier(<ts.Identifier>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.
Expand All @@ -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);
}


/**
Expand All @@ -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((<ts.PropertyAccessExpression>node.expression).expression);
node.arguments.forEach(n => args.push(n));
let args: ts.Expression[] = [];
args.push((<ts.PropertyAccessExpression>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));
}
}

/**
Expand All @@ -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<ts.Node> {
switch (node.kind) {
case ts.SyntaxKind.ArrayLiteralExpression:
return visitArrayLiteralExpression(<ts.ArrayLiteralExpression>node);
case ts.SyntaxKind.ObjectLiteralExpression:
return visitObjectLiteralExpression(<ts.ObjectLiteralExpression>node);
}
}
function visitNode(node: ts.Node): ts.VisitResult<ts.Node> {
switch (node.kind) {
case ts.SyntaxKind.ArrayLiteralExpression:
return visitArrayLiteralExpression(<ts.ArrayLiteralExpression>node);
case ts.SyntaxKind.ObjectLiteralExpression:
return visitObjectLiteralExpression(<ts.ObjectLiteralExpression>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;
}
}

0 comments on commit f77c9ee

Please sign in to comment.