Skip to content

Commit 9858023

Browse files
committed
first fully working version of TransferMoney example, using nested context for Account
1 parent b24a384 commit 9858023

File tree

8 files changed

+105
-64
lines changed

8 files changed

+105
-64
lines changed

bin/tsc.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28822,24 +28822,27 @@ var TypeScript;
2882228822
if (this.thisRoleNode) {
2882328823
if (this.thisRoleNode && operand1 instanceof TypeScript.ThisExpression) {
2882428824
roleName = this.thisRoleNode.name.actualText;
28825-
isCallToRoleMethod = true;
2882628825
isCallToSelf = true;
28827-
} else {
28828-
roleName = operand1.actualText;
28829-
if (roleName in dciContext.roleDeclarations) {
28830-
isCallToRoleMethod = true;
28831-
}
2883228826
}
28833-
} else {
28827+
}
28828+
28829+
if (!isCallToSelf) {
2883428830
roleName = operand1.actualText;
2883528831
if (roleName in dciContext.roleDeclarations) {
28836-
isCallToRoleMethod = true;
28832+
var methodName = operand2.actualText;
28833+
var roleDecl = dciContext.roleDeclarations[roleName];
28834+
roleDecl.members.members.forEach(function (member) {
28835+
if ((member).name.actualText == methodName) {
28836+
isCallToRoleMethod = true;
28837+
return false;
28838+
}
28839+
});
2883728840
}
2883828841
}
2883928842
}
2884028843
}
2884128844

28842-
if (isCallToRoleMethod) {
28845+
if (isCallToRoleMethod || isCallToSelf) {
2884328846
if (isCallToSelf) {
2884428847
this.writeToOutput("__dci_internal__.callMethodOnSelf");
2884528848

@@ -40195,7 +40198,9 @@ var TypeScript;
4019540198
}
4019640199

4019740200
if (!nameSymbol) {
40198-
context.postError(this.unitPath, dottedNameAST.operand2.minChar, dottedNameAST.operand2.getLength(), TypeScript.DiagnosticCode.The_property_0_does_not_exist_on_value_of_type_1, [(dottedNameAST.operand2).actualText, lhsType.toString(enclosingDecl ? enclosingDecl.getSymbol() : null)], enclosingDecl);
40201+
if (lhs.kind != TypeScript.PullElementKind.Role) {
40202+
context.postError(this.unitPath, dottedNameAST.operand2.minChar, dottedNameAST.operand2.getLength(), TypeScript.DiagnosticCode.The_property_0_does_not_exist_on_value_of_type_1, [(dottedNameAST.operand2).actualText, lhsType.toString(enclosingDecl ? enclosingDecl.getSymbol() : null)], enclosingDecl);
40203+
}
4019940204
return this.getNewErrorTypeSymbol(null, rhsName);
4020040205
}
4020140206
}

bin/typescript.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28822,24 +28822,27 @@ var TypeScript;
2882228822
if (this.thisRoleNode) {
2882328823
if (this.thisRoleNode && operand1 instanceof TypeScript.ThisExpression) {
2882428824
roleName = this.thisRoleNode.name.actualText;
28825-
isCallToRoleMethod = true;
2882628825
isCallToSelf = true;
28827-
} else {
28828-
roleName = operand1.actualText;
28829-
if (roleName in dciContext.roleDeclarations) {
28830-
isCallToRoleMethod = true;
28831-
}
2883228826
}
28833-
} else {
28827+
}
28828+
28829+
if (!isCallToSelf) {
2883428830
roleName = operand1.actualText;
2883528831
if (roleName in dciContext.roleDeclarations) {
28836-
isCallToRoleMethod = true;
28832+
var methodName = operand2.actualText;
28833+
var roleDecl = dciContext.roleDeclarations[roleName];
28834+
roleDecl.members.members.forEach(function (member) {
28835+
if ((member).name.actualText == methodName) {
28836+
isCallToRoleMethod = true;
28837+
return false;
28838+
}
28839+
});
2883728840
}
2883828841
}
2883928842
}
2884028843
}
2884128844

28842-
if (isCallToRoleMethod) {
28845+
if (isCallToRoleMethod || isCallToSelf) {
2884328846
if (isCallToSelf) {
2884428847
this.writeToOutput("__dci_internal__.callMethodOnSelf");
2884528848

@@ -40195,7 +40198,9 @@ var TypeScript;
4019540198
}
4019640199

4019740200
if (!nameSymbol) {
40198-
context.postError(this.unitPath, dottedNameAST.operand2.minChar, dottedNameAST.operand2.getLength(), TypeScript.DiagnosticCode.The_property_0_does_not_exist_on_value_of_type_1, [(dottedNameAST.operand2).actualText, lhsType.toString(enclosingDecl ? enclosingDecl.getSymbol() : null)], enclosingDecl);
40201+
if (lhs.kind != TypeScript.PullElementKind.Role) {
40202+
context.postError(this.unitPath, dottedNameAST.operand2.minChar, dottedNameAST.operand2.getLength(), TypeScript.DiagnosticCode.The_property_0_does_not_exist_on_value_of_type_1, [(dottedNameAST.operand2).actualText, lhsType.toString(enclosingDecl ? enclosingDecl.getSymbol() : null)], enclosingDecl);
40203+
}
4019940204
return this.getNewErrorTypeSymbol(null, rhsName);
4020040205
}
4020140206
}

bin/typescriptServices.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28822,24 +28822,27 @@ var TypeScript;
2882228822
if (this.thisRoleNode) {
2882328823
if (this.thisRoleNode && operand1 instanceof TypeScript.ThisExpression) {
2882428824
roleName = this.thisRoleNode.name.actualText;
28825-
isCallToRoleMethod = true;
2882628825
isCallToSelf = true;
28827-
} else {
28828-
roleName = operand1.actualText;
28829-
if (roleName in dciContext.roleDeclarations) {
28830-
isCallToRoleMethod = true;
28831-
}
2883228826
}
28833-
} else {
28827+
}
28828+
28829+
if (!isCallToSelf) {
2883428830
roleName = operand1.actualText;
2883528831
if (roleName in dciContext.roleDeclarations) {
28836-
isCallToRoleMethod = true;
28832+
var methodName = operand2.actualText;
28833+
var roleDecl = dciContext.roleDeclarations[roleName];
28834+
roleDecl.members.members.forEach(function (member) {
28835+
if ((member).name.actualText == methodName) {
28836+
isCallToRoleMethod = true;
28837+
return false;
28838+
}
28839+
});
2883728840
}
2883828841
}
2883928842
}
2884028843
}
2884128844

28842-
if (isCallToRoleMethod) {
28845+
if (isCallToRoleMethod || isCallToSelf) {
2884328846
if (isCallToSelf) {
2884428847
this.writeToOutput("__dci_internal__.callMethodOnSelf");
2884528848

@@ -40195,7 +40198,9 @@ var TypeScript;
4019540198
}
4019640199

4019740200
if (!nameSymbol) {
40198-
context.postError(this.unitPath, dottedNameAST.operand2.minChar, dottedNameAST.operand2.getLength(), TypeScript.DiagnosticCode.The_property_0_does_not_exist_on_value_of_type_1, [(dottedNameAST.operand2).actualText, lhsType.toString(enclosingDecl ? enclosingDecl.getSymbol() : null)], enclosingDecl);
40201+
if (lhs.kind != TypeScript.PullElementKind.Role) {
40202+
context.postError(this.unitPath, dottedNameAST.operand2.minChar, dottedNameAST.operand2.getLength(), TypeScript.DiagnosticCode.The_property_0_does_not_exist_on_value_of_type_1, [(dottedNameAST.operand2).actualText, lhsType.toString(enclosingDecl ? enclosingDecl.getSymbol() : null)], enclosingDecl);
40203+
}
4019940204
return this.getNewErrorTypeSymbol(null, rhsName);
4020040205
}
4020140206
}

samples/dci/DCI.js

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,38 @@
1-
var exports = {
2-
Context: Context
1+
/*
2+
function A() {}
3+
(<Test> A).something = 123;
4+
5+
export var Context = {
6+
extend: function(callback): any {
7+
return function(...args : any[]) {
8+
var context = new callback();
9+
if (!context.bindRoles) throw new Error('bindRoles() method not found');
10+
11+
context.bindRoles.apply(callback, arguments);
12+
return context;
13+
}
14+
}
315
};
16+
*/
17+
var Context = (function () {
18+
function Context() {
19+
}
20+
Context.extend = //for plain JS version
21+
function (callback) {
22+
return function () {
23+
var args = [];
24+
for (var _i = 0; _i < (arguments.length - 0); _i++) {
25+
args[_i] = arguments[_i + 0];
26+
}
27+
var context = new callback();
28+
if (!context.bindRoles)
29+
throw new Error('bindRoles() method not found');
430

5-
module.exports = exports;
31+
context.bindRoles.apply(callback, arguments);
32+
return context;
33+
};
34+
};
35+
return Context;
36+
})();
37+
exports.Context = Context;
638

samples/dci/js/TransferMoney/Account.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
var __dci_internal__ = require('typescript-dci/dci');
2+
var DCI = require('../../DCI');
23

34

4-
var Account = Context.extend(function () {
5+
6+
var Account = DCI.Context.extend(function () {
57
var __context = this;
8+
//TypeScript doesn't support this syntax yet.
9+
//If it did, in ES5 environments, a native-like getter could be created:
10+
/*
11+
get balance() {
12+
return Ledgers.getBalance();
13+
}
14+
*/
615
this.__$Ledgers = { addEntry: function (message, amount) {
716
__context.Ledgers.push(new LedgerEntry(message, amount));
817
}
@@ -29,11 +38,7 @@ this.__$Ledgers = { addEntry: function (message, amount) {
2938
return __context.__$Ledgers.getBalance.call(__context.Ledgers);
3039
};
3140

32-
//In ES5 environments, a native-like getter could be created:
33-
//(TypeScript doesn't support this syntax yet)
34-
// get balance() {
35-
// return this._balance;
36-
// }
41+
3742
});
3843
function LedgerEntry(message, amount) {
3944
this.message = message;
@@ -42,8 +47,5 @@ function LedgerEntry(message, amount) {
4247

4348
//export the LedgerEntry constructor
4449
Account.LedgerEntry = LedgerEntry;
45-
46-
//TEMP
47-
var a = new Account();
4850
module.exports = Account;
4951

samples/dci/js/TransferMoney/Account.ts

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
///<reference path='../../DCI.d.ts'/>
2-
3-
//import DCI = require('../../DCI');
4-
1+
import DCI = require('../../DCI');
52

63
export = Account;
74

8-
var Account = Context.extend(function() {
5+
var Account = DCI.Context.extend(function() {
96

107
this.bindRoles = function(ledgers) {
118
if (!ledgers) ledgers = [];
@@ -24,6 +21,14 @@ var Account = Context.extend(function() {
2421
return Ledgers.getBalance();
2522
}
2623

24+
//TypeScript doesn't support this syntax yet.
25+
//If it did, in ES5 environments, a native-like getter could be created:
26+
/*
27+
get balance() {
28+
return Ledgers.getBalance();
29+
}
30+
*/
31+
2732
role Ledgers {
2833
addEntry(message, amount) {
2934
Ledgers.push(new LedgerEntry(message, amount));
@@ -37,13 +42,6 @@ var Account = Context.extend(function() {
3742
return sum;
3843
}
3944
}
40-
41-
//In ES5 environments, a native-like getter could be created:
42-
//(TypeScript doesn't support this syntax yet)
43-
// get balance() {
44-
// return this._balance;
45-
// }
46-
4745
});
4846

4947
function LedgerEntry(message, amount) {
@@ -54,10 +52,6 @@ function LedgerEntry(message, amount) {
5452
//export the LedgerEntry constructor
5553
Account.LedgerEntry = LedgerEntry;
5654

57-
//TEMP
58-
var a = new Account();
59-
60-
6155

6256
/*
6357
function Account(initialBalance) {

samples/dci/js/TransferMoney/TransferMoney.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var __dci_internal__ = require('typescript-dci/dci');
2+
var DCI = require('../../DCI');
23

34

45
/**
@@ -10,7 +11,7 @@ var __dci_internal__ = require('typescript-dci/dci');
1011
* @param {Account} destination
1112
* @param {number} amount
1213
*/
13-
var TransferMoney = Context.extend(function () {
14+
var TransferMoney = DCI.Context.extend(function () {
1415
var __context = this;
1516
this.__$SourceAccount = { transferOut: function () {
1617
__dci_internal__.callMethodOnSelf(__context, this, 'SourceAccount', 'withdraw');

samples/dci/js/TransferMoney/TransferMoney.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
///<reference path='../../DCI.d.ts'/>
2-
3-
//import DCI = require('../../DCI');
4-
1+
import DCI = require('../../DCI');
52
export = TransferMoney;
63

74
/**
@@ -13,7 +10,7 @@ export = TransferMoney;
1310
* @param {Account} destination
1411
* @param {number} amount
1512
*/
16-
var TransferMoney = Context.extend(function() {
13+
var TransferMoney = DCI.Context.extend(function() {
1714
this.bindRoles = function(sourceAcct, destinationAcct, amount) {
1815
SourceAccount <- sourceAcct;
1916
DestinationAccount <- destinationAcct;

0 commit comments

Comments
 (0)