Skip to content

[refactoring] Use string interpolation where possible #1761

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2863,7 +2863,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,
Expand All @@ -2879,10 +2879,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);
Expand All @@ -2900,7 +2897,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<ExpressionRef>(1 + numStatements);
stmts[0] = currentBlock;
let count = 1;
Expand Down Expand Up @@ -6800,7 +6797,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<string>(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;
Expand Down Expand Up @@ -7400,7 +7397,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,
Expand Down
31 changes: 8 additions & 23 deletions src/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -912,7 +912,7 @@ export class Flow {
let key = _keys[i];
let leftFlags = changetype<FieldFlags>(leftFieldFlags.get(key));
if (
(leftFlags & FieldFlags.INITIALIZED) != 0 && rightFieldFlags.has(key) &&
(leftFlags & FieldFlags.INITIALIZED) != 0 && rightFieldFlags.has(key) &&
(changetype<FieldFlags>(rightFieldFlags.get(key)) & FieldFlags.INITIALIZED)
) {
newFieldFlags.set(key, FieldFlags.INITIALIZED);
Expand Down Expand Up @@ -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})[${levels}] ${sb.join(" ")}`;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2972,7 +2972,7 @@ export class SwitchBuilder {
var entry = new Array<ExpressionRef>(1 + numValues + 1);
var labels = new Array<string>(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) {
Expand Down
8 changes: 3 additions & 5 deletions src/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2842,7 +2842,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}`;
}
}

Expand Down Expand Up @@ -3674,9 +3674,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,
Expand Down Expand Up @@ -4763,7 +4761,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];
}
4 changes: 2 additions & 2 deletions src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2760,7 +2760,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,
Expand Down Expand Up @@ -2868,7 +2868,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, <InterfacePrototype>prototype, typeArguments);
} else {
Expand Down
6 changes: 3 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
}
Expand Down