From c09be25485f442d0b15fdfc87cea2a5767f27e75 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Sat, 27 Mar 2021 14:01:39 +0200 Subject: [PATCH 1/2] use string interpolations --- src/compiler.ts | 17 +++++++---------- src/diagnostics.ts | 31 ++++++++----------------------- src/flow.ts | 6 +++--- src/module.ts | 2 +- src/program.ts | 12 +++++------- src/resolver.ts | 4 ++-- src/types.ts | 6 +++--- 7 files changed, 29 insertions(+), 49 deletions(-) diff --git a/src/compiler.ts b/src/compiler.ts index 30d9fcbbdf..b9436cb0d5 100644 --- a/src/compiler.ts +++ b/src/compiler.ts @@ -1509,7 +1509,7 @@ export class Compiler extends DiagnosticEmitter { if (!this.compileFunctionBody(instance, stmts)) { stmts.push(module.unreachable()); } - + this.currentFlow = previousFlow; // create the function @@ -2857,7 +2857,7 @@ export class Compiler extends DiagnosticEmitter { let case_ = cases[i]; let label = case_.label; if (label) { - breaks[breakIndex++] = module.br("case" + i.toString() + "|" + context, + breaks[breakIndex++] = module.br(`case${i}|${context}`, module.binary(BinaryOp.EqI32, module.local_get(tempLocalIndex, NativeType.I32), this.compileExpression(label, Type.u32, @@ -2873,10 +2873,7 @@ export class Compiler extends DiagnosticEmitter { outerFlow.freeTempLocal(tempLocal); // otherwise br to default respectively out of the switch if there is no default case - breaks[breakIndex] = module.br((defaultIndex >= 0 - ? "case" + defaultIndex.toString() - : "break" - ) + "|" + context); + breaks[breakIndex] = module.br(`${(defaultIndex >= 0 ? `case${defaultIndex}` : "break" )}|${context}`); // nest blocks in order var currentBlock = module.block("case0|" + context, breaks, NativeType.None); @@ -2894,7 +2891,7 @@ export class Compiler extends DiagnosticEmitter { innerFlow.breakLabel = breakLabel; let isLast = i == numCases - 1; - let nextLabel = isLast ? breakLabel : "case" + (i + 1).toString() + "|" + context; + let nextLabel = isLast ? breakLabel : `case${i + 1}|${context}`; let stmts = new Array(1 + numStatements); stmts[0] = currentBlock; let count = 1; @@ -6781,7 +6778,7 @@ export class Compiler extends DiagnosticEmitter { // create a br_table switching over the number of optional parameters provided var numNames = numOptional + 1; // incl. outer block var names = new Array(numNames); - var ofN = "of" + numOptional.toString(); + var ofN = `of${numOptional}`; for (let i = 0; i < numNames; ++i) { let label = i.toString() + ofN; names[i] = label; @@ -7381,7 +7378,7 @@ export class Compiler extends DiagnosticEmitter { var isSemanticallyAnonymous = !isNamed || contextualType != Type.void; var prototype = new FunctionPrototype( isSemanticallyAnonymous - ? (isNamed ? declaration.name.text + "|" : "anonymous|") + (actualFunction.nextAnonymousId++).toString() + ? `${isNamed ? declaration.name.text + "|" : "anonymous|"}${actualFunction.nextAnonymousId++}` : declaration.name.text, actualFunction, declaration, @@ -7533,7 +7530,7 @@ export class Compiler extends DiagnosticEmitter { expr = module.local_tee(local.index, expr, ftype.isManaged); } } - + return expr; } diff --git a/src/diagnostics.ts b/src/diagnostics.ts index 3fe2ecb821..80ff2bc8e0 100644 --- a/src/diagnostics.ts +++ b/src/diagnostics.ts @@ -142,33 +142,18 @@ export class DiagnosticMessage { /** Converts this message to a string. */ toString(): string { + var category = diagnosticCategoryToString(this.category); var range = this.range; if (range) { let source = range.source; - return ( - diagnosticCategoryToString(this.category) + - " " + - this.code.toString() + - ": \"" + - this.message + - "\" in " + - source.normalizedPath + - "(" + - source.lineAt(range.start).toString() + - "," + - source.columnAt().toString() + - "+" + - (range.end - range.start).toString() + - ")" - ); + let path = source.normalizedPath; + let line = source.lineAt(range.start); + let column = source.columnAt(); + let len = range.end - range.start; + + return `${category} ${this.code}: "${this.message}" in ${path}(${line},${column}+${len})`; } - return ( - diagnosticCategoryToString(this.category) + - " " + - this.code.toString() + - ": " + - this.message - ); + return `${category} ${this.code}: ${this.message}`; } } diff --git a/src/flow.ts b/src/flow.ts index 7e1547b26a..cca68c6cdb 100644 --- a/src/flow.ts +++ b/src/flow.ts @@ -198,7 +198,7 @@ export class Flow { static createInline(parentFunction: Function, inlineFunction: Function): Flow { var flow = new Flow(parentFunction); flow.inlineFunction = inlineFunction; - flow.inlineReturnLabel = inlineFunction.internalName + "|inlined." + (inlineFunction.nextInlineId++).toString(); + flow.inlineReturnLabel = `${inlineFunction.internalName}|inlined.${inlineFunction.nextInlineId++}`; if (inlineFunction.is(CommonFlags.CONSTRUCTOR)) { flow.initThisFieldFlags(); } @@ -912,7 +912,7 @@ export class Flow { let key = _keys[i]; let leftFlags = changetype(leftFieldFlags.get(key)); if ( - (leftFlags & FieldFlags.INITIALIZED) != 0 && rightFieldFlags.has(key) && + (leftFlags & FieldFlags.INITIALIZED) != 0 && rightFieldFlags.has(key) && (changetype(rightFieldFlags.get(key)) & FieldFlags.INITIALIZED) ) { newFieldFlags.set(key, FieldFlags.INITIALIZED); @@ -1456,7 +1456,7 @@ export class Flow { if (this.is(FlowFlags.CONDITIONALLY_CONTINUES)) sb.push("CONDITIONALLY_CONTINUES"); if (this.is(FlowFlags.CONDITIONALLY_ACCESSES_THIS)) sb.push("CONDITIONALLY_ACCESSES_THIS"); if (this.is(FlowFlags.MAY_RETURN_NONTHIS)) sb.push("MAY_RETURN_NONTHIS"); - return "Flow(" + this.actualFunction.toString() + ")[" + levels.toString() + "] " + sb.join(" "); + return `Flow(${this.actualFunction.toString()})[${levels}] ${sb.join(" ")}`; } } diff --git a/src/module.ts b/src/module.ts index b44922c97d..4db619c1d5 100644 --- a/src/module.ts +++ b/src/module.ts @@ -2545,7 +2545,7 @@ export class SwitchBuilder { var entry = new Array(1 + numValues + 1); var labels = new Array(numCases); for (let i = 0; i < numCases; ++i) { - labels[i] = "case" + i.toString() + labelPostfix; + labels[i] = `case${i}${labelPostfix}`; } entry[0] = module.local_set(localIndex, this.condition, false); // u32 for (let i = 0; i < numValues; ++i) { diff --git a/src/program.ts b/src/program.ts index 833a9682d4..8e57daf4ed 100644 --- a/src/program.ts +++ b/src/program.ts @@ -2826,7 +2826,7 @@ export abstract class Element { /** Returns a string representation of this element. */ toString(): string { - return this.internalName + ", kind=" + this.kind.toString(); + return `${this.internalName}, kind=${this.kind}`; } } @@ -3084,8 +3084,8 @@ export class File extends Element { /** Creates an imported namespace from this file. */ asAliasNamespace( - name: string, - parent: Element, + name: string, + parent: Element, localIdentifier: IdentifierExpression ): Namespace { var declaration = this.program.makeNativeNamespaceDeclaration(name); @@ -3658,9 +3658,7 @@ export class Function extends TypedElement { // if it has a name, check previously as this method will throw otherwise var localIndex = this.signature.parameterTypes.length + this.additionalLocals.length; if (this.is(CommonFlags.INSTANCE)) ++localIndex; - var localName = name !== null - ? name - : "var$" + localIndex.toString(); + var localName = name !== null ? name : `var$${localIndex}`; if (!declaration) declaration = this.program.makeNativeVariableDeclaration(localName); var local = new Local( localName, @@ -4747,7 +4745,7 @@ var cachedDefaultParameterNames: string[] = []; /** Gets the cached default parameter name for the specified index. */ export function getDefaultParameterName(index: i32): string { for (let i = cachedDefaultParameterNames.length; i <= index; ++i) { - cachedDefaultParameterNames.push("$" + i.toString()); + cachedDefaultParameterNames.push(`$${i}`); } return cachedDefaultParameterNames[index]; } diff --git a/src/resolver.ts b/src/resolver.ts index e2d4b1dd25..7020829014 100644 --- a/src/resolver.ts +++ b/src/resolver.ts @@ -2739,7 +2739,7 @@ export class Resolver extends DiagnosticEmitter { signature.requiredParameters = requiredParameters; var nameInclTypeParameters = prototype.name; - if (instanceKey.length) nameInclTypeParameters += "<" + instanceKey + ">"; + if (instanceKey.length) nameInclTypeParameters += `<${instanceKey}>`; var instance = new Function( nameInclTypeParameters, prototype, @@ -2847,7 +2847,7 @@ export class Resolver extends DiagnosticEmitter { // Otherwise create var nameInclTypeParamters = prototype.name; - if (instanceKey.length) nameInclTypeParamters += "<" + instanceKey + ">"; + if (instanceKey.length) nameInclTypeParamters += `<${instanceKey}>`; if (prototype.kind == ElementKind.INTERFACE_PROTOTYPE) { instance = new Interface(nameInclTypeParamters, prototype, typeArguments); } else { diff --git a/src/types.ts b/src/types.ts index 935d2733e5..4ca93a60b9 100644 --- a/src/types.ts +++ b/src/types.ts @@ -222,7 +222,7 @@ export class Type { get isFloatValue(): bool { return this.is(TypeFlags.FLOAT | TypeFlags.VALUE); } - + /** Tests if this type represents a numeric (integer or floating point) value. */ get isNumericValue(): bool { return this.isIntegerValue || this.isFloatValue; @@ -237,7 +237,7 @@ export class Type { get isVectorValue(): bool { return this.is(TypeFlags.VECTOR | TypeFlags.VALUE); } - + /** Tests if this type represents an internal or external reference. */ get isReference(): bool { return this.is(TypeFlags.REFERENCE); @@ -479,7 +479,7 @@ export class Type { let signatureReference = this.getSignature(); if (signatureReference) { return this.isNullableReference - ? "(" + signatureReference.toString(validWat) + ")" + nullablePostfix + ? `(${signatureReference.toString(validWat)})${nullablePostfix}` : signatureReference.toString(validWat); } } From ace2bf28bb5f54325ecb0e0d60d0acd364c291d6 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Sat, 27 Mar 2021 14:09:40 +0200 Subject: [PATCH 2/2] simplify --- src/flow.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/flow.ts b/src/flow.ts index cca68c6cdb..cdbf625358 100644 --- a/src/flow.ts +++ b/src/flow.ts @@ -1456,7 +1456,7 @@ export class Flow { if (this.is(FlowFlags.CONDITIONALLY_CONTINUES)) sb.push("CONDITIONALLY_CONTINUES"); if (this.is(FlowFlags.CONDITIONALLY_ACCESSES_THIS)) sb.push("CONDITIONALLY_ACCESSES_THIS"); if (this.is(FlowFlags.MAY_RETURN_NONTHIS)) sb.push("MAY_RETURN_NONTHIS"); - return `Flow(${this.actualFunction.toString()})[${levels}] ${sb.join(" ")}`; + return `Flow(${this.actualFunction})[${levels}] ${sb.join(" ")}`; } }