Skip to content
Merged
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
1 change: 0 additions & 1 deletion src.compiler/BuilderHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { indexOf } from 'lodash';
import * as ts from 'typescript';

export function addNewLines(stmts: ts.Statement[]) {
Expand Down
4 changes: 2 additions & 2 deletions src.compiler/TranspilerBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ export default function (emitters: Emitter[], handleErrors: boolean = false) {
let diagnostics = ts.sortAndDeduplicateDiagnostics(allDiagnostics);
let errorCount = 0;
let warningCount = 0;
diagnostics.forEach(d => {
for(const d of diagnostics) {
switch (d.category) {
case ts.DiagnosticCategory.Error: errorCount++; break;
case ts.DiagnosticCategory.Warning: warningCount++; break;
}
reportDiagnostic(d);
});
}

if (pretty) {
reportDiagnostic({
Expand Down
9 changes: 8 additions & 1 deletion src.compiler/csharp/CSharpAst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export enum SyntaxKind {

VariableDeclarationList,
VariableDeclaration,
DeconstructDeclaration,
CaseClause,
DefaultClause,
CatchClause,
Expand Down Expand Up @@ -264,7 +265,8 @@ export enum PrimitiveType {
Int,
Void,
Object,
Dynamic
Dynamic,
Var
}

export interface PrimitiveTypeNode extends TypeNode {
Expand Down Expand Up @@ -436,9 +438,14 @@ export interface VariableDeclarationList extends Node {
export interface VariableDeclaration extends Node {
type: TypeNode;
name: string;
deconstructNames?: string[];
initializer?: Expression;
}

export interface DeconstructDeclaration extends Node {
names: string[];
}

export interface ForStatement extends Statement {
initializer?: VariableDeclarationList | Expression;
condition?: Expression;
Expand Down
21 changes: 18 additions & 3 deletions src.compiler/csharp/CSharpAstPrinter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ export default class CSharpAstPrinter {
if (!this._isStartOfLine) {
this.writeLine();
}
lines.forEach(line => {
for (const line of lines) {
this.writeLine(`/// ${this.escapeXmlDoc(line)}`);
});
}
} else if (lines.length === 1) {
if (this._isStartOfLine) {
this.writeLine(`/// ${this.escapeXmlDoc(lines[0])}`);
Expand Down Expand Up @@ -598,6 +598,9 @@ export default class CSharpAstPrinter {
case cs.PrimitiveType.Void:
this.write('void');
break;
case cs.PrimitiveType.Var:
this.write('var');
break;
}
}

Expand Down Expand Up @@ -1257,7 +1260,19 @@ export default class CSharpAstPrinter {
this.write(', ');
}

this.write(d.name);
if (d.deconstructNames) {
this.write('(');
d.deconstructNames.forEach((v, i) => {
if (i > 0) {
this.write(', ');
}
this.write(v);
})
this.write(')');
} else {
this.write(d.name);
}

if (d.initializer) {
this.write(' = ');
this.writeExpression(d.initializer);
Expand Down
64 changes: 45 additions & 19 deletions src.compiler/csharp/CSharpAstTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,19 @@ export default class CSharpAstTransformer {
return unresolved;
}

private createVarTypeNode(
parent: cs.Node | null,
tsNode: ts.Node,
): cs.PrimitiveTypeNode {
const varNode = {
nodeType: cs.SyntaxKind.PrimitiveTypeNode,
tsNode: tsNode,
parent: parent,
type: cs.PrimitiveType.Var
} as cs.PrimitiveTypeNode;
return varNode;
}

public visitTestClass(d: ts.CallExpression): void {
const csClass: cs.ClassDeclaration = {
visibility: cs.Visibility.Public,
Expand Down Expand Up @@ -1366,34 +1379,47 @@ export default class CSharpAstTransformer {
return variableStatement;
}
private visitVariableDeclaration(parent: cs.Node, s: ts.VariableDeclaration): cs.VariableDeclaration {
const symbol = this._context.typeChecker.getSymbolAtLocation(s.name);
const type = this._context.typeChecker.getTypeOfSymbolAtLocation(symbol!, s);

const variableStatement = {
nodeType: cs.SyntaxKind.VariableDeclaration,
parent: parent,
tsNode: s,
name: (s.name as ts.Identifier).text,
name: '',
type: {} as cs.TypeNode
} as cs.VariableDeclaration;

if (parent.nodeType === cs.SyntaxKind.CatchClause) {
variableStatement.type = {
nodeType: cs.SyntaxKind.TypeReference,
parent: variableStatement,
tsNode: s,
reference: 'AlphaTab.Core.EcmaScript.Error'
} as cs.TypeReference;
} else {
variableStatement.type = this.createUnresolvedTypeNode(variableStatement, s.type ?? s, type);
}
if (ts.isIdentifier(s.name)) {
const symbol = this._context.typeChecker.getSymbolAtLocation(s.name);
const type = this._context.typeChecker.getTypeOfSymbolAtLocation(symbol!, s);

variableStatement.name = s.name.text;
if (parent.nodeType === cs.SyntaxKind.CatchClause) {
variableStatement.type = {
nodeType: cs.SyntaxKind.TypeReference,
parent: variableStatement,
tsNode: s,
reference: 'AlphaTab.Core.EcmaScript.Error'
} as cs.TypeReference;
} else {
variableStatement.type = this.createUnresolvedTypeNode(variableStatement, s.type ?? s, type);
}

variableStatement.type.parent = variableStatement;
variableStatement.type.parent = variableStatement;

if (s.initializer) {
this._declarationOrAssignmentTypeStack.push(type);
variableStatement.initializer = this.visitExpression(variableStatement, s.initializer) ?? undefined;
this._declarationOrAssignmentTypeStack.pop();
if (s.initializer) {
this._declarationOrAssignmentTypeStack.push(type);
variableStatement.initializer = this.visitExpression(variableStatement, s.initializer) ?? undefined;
this._declarationOrAssignmentTypeStack.pop();
}
} else if (ts.isArrayBindingPattern(s.name)) {
variableStatement.type = this.createVarTypeNode(variableStatement, s.type ?? s);
variableStatement.deconstructNames = [];
for (const el of s.name.elements) {
if (ts.isOmittedExpression(el)) {
variableStatement.deconstructNames.push('_');
} else if (ts.isBindingElement(el)) {
variableStatement.deconstructNames.push((el.name as ts.Identifier).text);
}
}
}

return variableStatement;
Expand Down
84 changes: 44 additions & 40 deletions src.compiler/typescript/SerializerEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,12 +290,12 @@ function generateToJsonBody(
ts.factory.createStringLiteral(jsonName),
ts.factory.createAsExpression(
ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier('obj'), fieldName),
type.isNullable
? ts.factory.createUnionTypeNode([
ts.factory.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword),
ts.factory.createLiteralTypeNode(ts.factory.createNull())
])
: ts.factory.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword)
type.isNullable
? ts.factory.createUnionTypeNode([
ts.factory.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword),
ts.factory.createLiteralTypeNode(ts.factory.createNull())
])
: ts.factory.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword)
)
]
)));
Expand Down Expand Up @@ -344,7 +344,7 @@ function generateToJsonBody(
let writeValue: ts.Expression;
if (isPrimitiveToJson(mapType.typeArguments![1], typeChecker)) {
writeValue = ts.factory.createIdentifier('v');
} else if(isEnumType(mapType.typeArguments![1])) {
} else if (isEnumType(mapType.typeArguments![1])) {
writeValue = ts.factory.createAsExpression(
ts.factory.createIdentifier('v'),
ts.factory.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword)
Expand All @@ -358,7 +358,7 @@ function generateToJsonBody(
ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier(itemSerializer), 'toJson'),
undefined,
[
ts.factory.createIdentifier('v')
ts.factory.createIdentifier('v'),
]
);
}
Expand Down Expand Up @@ -387,42 +387,46 @@ function generateToJsonBody(
]
)),

ts.factory.createExpressionStatement(
ts.factory.createCallExpression(
ts.factory.createPropertyAccessExpression(
ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier('obj'), fieldName),
'forEach'
),
undefined,
[
ts.factory.createArrowFunction(
undefined,
ts.factory.createForOfStatement(
undefined,
ts.factory.createVariableDeclarationList([
ts.factory.createVariableDeclaration(
ts.factory.createArrayBindingPattern([
ts.factory.createBindingElement(
undefined,
undefined,
'k'
),
ts.factory.createBindingElement(
undefined,
undefined,
'v'
),
])
)],
ts.NodeFlags.Const
),
ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier('obj'), fieldName),
ts.factory.createBlock([
ts.factory.createExpressionStatement(
ts.factory.createCallExpression(
ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier('m'), 'set'),
undefined,
[
ts.factory.createParameterDeclaration(undefined, undefined, undefined, 'v'),
ts.factory.createParameterDeclaration(undefined, undefined, undefined, 'k')
],
undefined,
ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken),
ts.factory.createCallExpression(
ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier('m'), 'set'),
undefined,
[
// todo: key to string
ts.factory.createCallExpression(
ts.factory.createPropertyAccessExpression(
ts.factory.createIdentifier('k'),
'toString'
),
undefined,
[]
// todo: key to string
ts.factory.createCallExpression(
ts.factory.createPropertyAccessExpression(
ts.factory.createIdentifier('k'),
'toString'
),
writeValue
]
)
undefined,
[]
),
writeValue
]
)
]
)
)
])
)
]));
} else if (isImmutable(type.type)) {
Expand Down
6 changes: 6 additions & 0 deletions src.csharp/AlphaTab/Core/EcmaScript/MapEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,11 @@ public MapEntry(TKey key, TValue value)
Key = key;
Value = value;
}

public void Deconstruct(out TKey key, out TValue value)
{
key = Key;
value = Value;
}
}
}
13 changes: 12 additions & 1 deletion src.csharp/AlphaTab/Core/EcmaScript/Set.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;

namespace AlphaTab.Core.EcmaScript
{
public class Set<T>
public class Set<T> : IEnumerable<T>
{
private readonly HashSet<T> _data = new HashSet<T>();

Expand All @@ -28,5 +29,15 @@ public void ForEach(Action<T> action)
action(i);
}
}

public IEnumerator<T> GetEnumerator()
{
return _data.GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}
Loading