This commit is contained in:
Garrett Serack 2019-04-15 05:57:18 -07:00
Родитель 3837363d33
Коммит 0d4cc775dc
7 изменённых файлов: 75 добавлений и 67 удалений

Просмотреть файл

@ -5,7 +5,7 @@
import { CommaChar } from '@microsoft.azure/codegen';
import { Class } from './class';
import { Expression, toExpression, valueOf } from './expression';
import { Expression, toExpression, valueOf, isAnExpression } from './expression';
import { Method } from './method';
export class Constructor extends Method {
@ -13,7 +13,7 @@ export class Constructor extends Method {
super(containingClass.name);
this.apply(objectIntializer);
if (this.body) {
if (this.body && !isAnExpression(this.body)) {
this.add(this.body);
}
}

Просмотреть файл

@ -12,6 +12,7 @@ import { Property } from './property';
import { TypeDeclaration } from './type-declaration';
import { Local, Variable } from './variable';
import { Class } from './class';
import { IInterface } from './type-container';
export class ClassType implements TypeDeclaration {
private get fullName() {
@ -280,15 +281,29 @@ export const System = intersect(system, {
Collections: intersect(collections, {
Hashtable: new ClassType(collections, 'Hashtable'),
IDictionary: new ClassType(collections, 'IDictionary'),
IEnumerator: new ClassType(collections, 'IEnumerator'),
Generic: intersect(generic, {
Dictionary(keyType: TypeDeclaration, valueType: TypeDeclaration): ClassType {
return new ClassType(generic, `Dictionary<${keyType.declaration},${valueType.declaration}>`);
},
IDictionary(keyType: TypeDeclaration, valueType: TypeDeclaration): IInterface {
return {
fullName: `IDictionary<${keyType.declaration},${valueType.declaration}>`,
allProperties: []
};
},
KeyValuePair(keyType: TypeDeclaration, valueType: TypeDeclaration): ClassType {
return new ClassType(generic, `KeyValuePair<${keyType.declaration},${valueType.declaration}>`);
},
IEnumerable(type: TypeDeclaration): ClassType {
return new ClassType(generic, `IEnumerable<${type.declaration}>`);
},
IEnumerator(type: TypeDeclaration): ClassType {
return new ClassType(generic, `IEnumerator<${type.declaration}>`);
},
ICollection(type: TypeDeclaration): ClassType {
return new ClassType(generic, `ICollection<${type.declaration}>`);
}
})
}),
@ -321,5 +336,6 @@ export const dotnet = {
False: new LiteralExpression('false'),
Null: new LiteralExpression('null'),
This: new LiteralExpression('this'),
Array: (type: TypeDeclaration) => ({ declaration: `${type.declaration}[]` })
};

Просмотреть файл

@ -27,6 +27,10 @@ export interface Expression {
Cast(toType: TypeDeclaration): Expression;
}
export function isAnExpression(item: any): item is Expression {
return item && item.value ? true : false;
}
export function Or(expression1: ExpressionOrLiteral, expression2: ExpressionOrLiteral) {
return `(${expression1} || ${expression2})`;
}

Просмотреть файл

@ -20,6 +20,8 @@ export class Field extends Variable {
public 'readonly': ReadOnly = Modifier.None;
public volitile: Volitile = Modifier.None;
public attributes = new Array<Attribute>();
public initialValue?: ExpressionOrLiteral;
protected get attributeDeclaration(): string {
return this.attributes.length > 0 ? `${this.attributes.joinWith(each => `${each.value}`, EOL)}${EOL}` : '';
}
@ -36,8 +38,10 @@ export class Field extends Variable {
}
public get declaration(): string {
const initializer = this.initialValue ? ` = ${valueOf(this.initialValue)}` : ``;
return `${docComment(xmlize('summary', this.description))}
${this.attributeDeclaration}${this.new}${this.access} ${this.static} ${this.readonly} ${this.volitile} ${this.type.declaration} ${this.name};`.slim();
${this.attributeDeclaration}${this.new}${this.access} ${this.static} ${this.readonly} ${this.volitile} ${this.type.declaration} ${this.name}${initializer};`.slim();
}
public get value(): string {
@ -65,15 +69,3 @@ export class Field extends Variable {
}
}
export class InitializedField extends Field {
constructor(name: string, type: TypeDeclaration, public valueExpression: ExpressionOrLiteral, objectInitializer?: Partial<InitializedField>) {
super(name, type);
this.apply(objectInitializer);
}
public get declaration(): string {
return `${docComment(xmlize('summary', this.description))}
${this.attributeDeclaration}${this.new}${this.access} ${this.static} ${this.readonly} ${this.volitile} ${this.type.declaration} ${this.name} = ${valueOf(this.valueExpression)};`.slim();
}
}

Просмотреть файл

@ -8,7 +8,7 @@ import { Abstract, Access, Async, Extern, Modifier, New, Override, Sealed, Stati
import { Class } from './class';
import { summary, xmlize } from './doc-comments';
import { Expression, toExpression, valueOf } from './expression';
import { Expression, toExpression, valueOf, isAnExpression } from './expression';
import { Parameter } from './parameter';
import { StatementPossibilities, Statements } from './statements/statement';
import { TypeDeclaration } from './type-declaration';
@ -31,15 +31,17 @@ export class Method extends Statements {
public isPartial = false;
public description: string = '';
public returnsDescription: string = '';
public body?: StatementPossibilities;
public body?: StatementPossibilities | Expression;
constructor(public name: string, protected returnType: TypeDeclaration = Void, objectIntializer?: Partial<Method>) {
super();
this.apply(objectIntializer);
// easy access to allow statements in the initalizer.
if (this.body) {
if (this.body && !isAnExpression(this.body)) {
this.add(this.body);
}
if (!this.description.trim()) {
this.description = `FIXME: Method ${name} is MISSING DESCRIPTION`;
}
@ -89,6 +91,11 @@ ${this.returnType.declaration} ${this.name}(${parameterDeclaration});
}
public get implementation(): string {
if (isAnExpression(this.body)) {
return `
${this.declaration} => ${this.body.value};
`.trim();
}
return `
${this.declaration}
{

Просмотреть файл

@ -18,6 +18,7 @@ export class Parameter extends Variable {
public modifier: ParameterModifier = ParameterModifier.None;
public defaultInitializer?: Expression;
public attributes = new Array<Attribute>();
protected get attributeDeclaration(): string {
return this.attributes.length > 0 ? `${this.attributes.joinWith(each => `${each.value}`, ' ')} ` : '';
}

Просмотреть файл

@ -9,11 +9,12 @@ import { docComment, EOL, indent } from '@microsoft.azure/codegen';
import { Abstract, Access, Extern, highestAccess, Modifier, New, Override, Sealed, Static, Virtual } from './access-modifier';
import { Attribute } from './attribute';
import { summary } from './doc-comments';
import { Expression, ExpressionOrLiteral, toExpression, valueOf } from './expression';
import { Expression, ExpressionOrLiteral, toExpression, valueOf, isAnExpression } from './expression';
import { OneOrMoreStatements, Statement, Statements, StatementPossibilities } from './statements/statement';
import { TypeDeclaration } from './type-declaration';
import { ExpressionStatement, Instance, Variable } from './variable';
export class Property extends Variable implements Instance {
public 'new': New = Modifier.None;
public getAccess = Access.Public;
@ -28,6 +29,9 @@ export class Property extends Variable implements Instance {
public metadata: Dictionary<any> = {};
public description: string = '';
public get?: StatementPossibilities | Expression;
public set?: StatementPossibilities | Expression;
protected get visibility(): Access {
return highestAccess(this.getAccess, this.setAccess);
}
@ -59,19 +63,44 @@ export class Property extends Variable implements Instance {
protected get setterDeclaration(): string {
return this.setAccess === this.visibility ? 'set' : `${this.setAccess} set`;
}
protected get getter(): string {
return `${this.getterDeclaration};`;
if (!this.get) {
// if there is a set expression/body then this can't bet auto
return this.set ? '' : `${this.getterDeclaration};`;
}
if (isAnExpression(this.get)) {
return `${this.getterDeclaration} => ${valueOf(this.get)};`;
}
return `${this.getterDeclaration}
{
${indent(new Statements(this.get).implementation, 2)}
}`.trim();
}
protected get setter(): string {
return `${this.setterDeclaration};`;
if (!this.set) {
// if there is a get expression/body then this can't bet auto
return this.get ? '' : `${this.setterDeclaration};`;
}
if (isAnExpression(this.set)) {
return `${this.getterDeclaration} => ${valueOf(this.set)};`;
}
return `${this.setterDeclaration}
{
${indent(new Statements(this.set).implementation, 2)}
}`.trim();
}
public get declaration(): string {
return `
${docComment(summary(this.description))}
${this.attributeDeclaration}${this.new}${this.visibility} ${this.static} ${this.virtual} ${this.sealed} ${this.override} ${this.abstract} ${this.extern} ${this.type.declaration} ${this.name} {${this.getter}${this.setter}}
`.slim();
${this.attributeDeclaration}${this.new}${this.visibility} ${this.static} ${this.virtual} ${this.sealed} ${this.override} ${this.abstract} ${this.extern} ${this.type.declaration} ${this.name} {
${this.getter}
${this.setter}
}`.slim();
}
public get value(): string {
return `${this.name}`;
@ -102,47 +131,6 @@ ${this.attributeDeclaration}${this.new}${this.visibility} ${this.static} ${this.
}
export class ImplementedProperty extends Property {
getterStatements?: StatementPossibilities;
setterStatements?: StatementPossibilities;
constructor(public name: string, public type: TypeDeclaration, objectInitializer?: Partial<ImplementedProperty>) {
super(name, type);
this.apply(objectInitializer);
}
public get declaration(): string {
return (`
${docComment(summary(this.description))}
${this.attributeDeclaration}${this.new}${this.visibility} ${this.static} ${this.virtual} ${this.sealed} ${this.override} ${this.abstract} ${this.extern} ${this.type.declaration} ${this.name}`.slim() +
`
{
${this.getter}
${this.setter}
}
`).trim();
}
protected get getter(): string {
if (!this.getterStatements) {
return '';
}
return `${this.getterDeclaration}
{
${indent(new Statements(this.getterStatements).implementation, 2)}
}`.trim();
}
protected get setter(): string {
if (!this.setterStatements) {
return '';
}
return `${this.setterDeclaration}
{
${indent(new Statements(this.setterStatements).implementation, 2)}
}`.trim();
}
}
export class LambdaProperty extends Property {
constructor(public name: string, public type: TypeDeclaration, public expression: Expression, objectInitializer?: Partial<LambdaProperty>) {
@ -181,14 +169,14 @@ ${this.attributeDeclaration}${this.new}${this.visibility} ${this.static} ${this.
}
}
export class BackedProperty extends ImplementedProperty {
export class BackedProperty extends Property {
public backingName: string;
public initializer?: ExpressionOrLiteral;
constructor(name: string, type: TypeDeclaration, objectInitializer?: Partial<BackedProperty>) {
const backingName = `_${name.uncapitalize()}`;
super(name, type, {
getterStatements: new Statements(`return this.${backingName};`),
setterStatements: new Statements(`this.${backingName} = value;`)
get: new Statements(`return this.${backingName};`),
set: new Statements(`this.${backingName} = value;`)
});
this.backingName = backingName;