@accordproject/template-engine 1.0.0 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +5 -2
- package/.github/workflows/npm-publish.yml +0 -37
- package/assets/template-compilation.png +0 -0
- package/assets/template-interpreter.png +0 -0
- package/src/Common.ts +0 -58
- package/src/TemplateMarkInterpreter.ts +0 -420
- package/src/TemplateMarkToJavaScriptCompiler.ts +0 -99
- package/src/TemplateMarkToTypeScriptCompiler.ts +0 -276
- package/src/TypeScriptCompilationContext.ts +0 -73
- package/src/TypeScriptRuntime.ts +0 -64
- package/src/TypeScriptToJavaScriptCompiler.ts +0 -59
- package/src/compilers/Clause.ts +0 -63
- package/src/compilers/Common.ts +0 -215
- package/src/compilers/Conditional.ts +0 -53
- package/src/compilers/ContractDefinition.ts +0 -31
- package/src/compilers/FormattedVariable.ts +0 -41
- package/src/compilers/Formula.ts +0 -36
- package/src/compilers/Join.ts +0 -33
- package/src/compilers/ListBlock.ts +0 -55
- package/src/compilers/NodeCompilers.ts +0 -90
- package/src/compilers/Optional.ts +0 -65
- package/src/compilers/Variable.ts +0 -40
- package/src/compilers/With.ts +0 -37
- package/src/drafting/Boolean/index.ts +0 -26
- package/src/drafting/DateTime/index.ts +0 -28
- package/src/drafting/Double/format.ts +0 -50
- package/src/drafting/Double/index.ts +0 -30
- package/src/drafting/Integer/format.ts +0 -41
- package/src/drafting/Integer/index.ts +0 -32
- package/src/drafting/Long/index.ts +0 -30
- package/src/drafting/MonetaryAmount/currencycode.ts +0 -49
- package/src/drafting/MonetaryAmount/index.ts +0 -73
- package/src/drafting/String/index.ts +0 -22
- package/src/drafting/index.ts +0 -31
- package/src/index.ts +0 -15
- package/src/markdown-common.d.ts +0 -14
- package/src/model-gen/concerto.metamodel@1.0.0.ts +0 -267
- package/src/model-gen/concerto@1.0.0.ts +0 -71
- package/src/model-gen/org.accordproject.ciceromark@0.6.0.ts +0 -82
- package/src/model-gen/org.accordproject.commonmark@0.5.0.ts +0 -159
- package/src/model-gen/org.accordproject.templatemark@0.4.0.ts +0 -119
- package/tsconfig.build.json +0 -4
- package/tsconfig.json +0 -101
|
@@ -1,276 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
3
|
-
* you may not use this file except in compliance with the License.
|
|
4
|
-
* You may obtain a copy of the License at
|
|
5
|
-
*
|
|
6
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
7
|
-
*
|
|
8
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
9
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
10
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
11
|
-
* See the License for the specific language governing permissions and
|
|
12
|
-
* limitations under the License.
|
|
13
|
-
*/
|
|
14
|
-
import traverse from 'traverse';
|
|
15
|
-
import { copySync } from 'fs-extra';
|
|
16
|
-
|
|
17
|
-
import { TemplateMarkModel, CiceroMarkModel, CommonMarkModel, ConcertoMetaModel } from '@accordproject/markdown-common';
|
|
18
|
-
|
|
19
|
-
import { ClassDeclaration, ModelManager, ModelUtil, Property, Resource } from '@accordproject/concerto-core';
|
|
20
|
-
import { CodeGen } from '@accordproject/concerto-tools';
|
|
21
|
-
import { FileWriter } from '@accordproject/concerto-util';
|
|
22
|
-
import { getCompiler } from './compilers/NodeCompilers';
|
|
23
|
-
import { RUNTIME_DIR, writeEpilog, writeImports, writeProlog } from './compilers/Common';
|
|
24
|
-
import { getTemplateClassDeclaration } from './Common';
|
|
25
|
-
import dayjs from 'dayjs';
|
|
26
|
-
|
|
27
|
-
export type ProcessingFunction = (fw:FileWriter, level:number, resource:any) => void;
|
|
28
|
-
|
|
29
|
-
export type CompilationOptions = {
|
|
30
|
-
skipGenerator?: boolean;
|
|
31
|
-
skipCopyRuntime?: boolean;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Converts TemplateMark DOM to a Typescript function, that, when run with JSON data
|
|
36
|
-
* that conforms to the template model will produce AgreementMark JSON output.
|
|
37
|
-
*/
|
|
38
|
-
export class TemplateMarkToTypeScriptCompiler {
|
|
39
|
-
modelManager: ModelManager;
|
|
40
|
-
templateClass: ClassDeclaration;
|
|
41
|
-
|
|
42
|
-
constructor(modelManager: ModelManager) {
|
|
43
|
-
this.modelManager = modelManager;
|
|
44
|
-
this.templateClass = getTemplateClassDeclaration(this.modelManager);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
writeFunctionToString(functionName: string, returnType:string, code: string) : string {
|
|
48
|
-
let result = '';
|
|
49
|
-
result += '/// ---cut---\n';
|
|
50
|
-
result += `export function ${functionName}(data:TemplateModel.I${this.templateClass.getName()}, library:any, now:dayjs.Dayjs) : ${returnType} {\n`;
|
|
51
|
-
this.templateClass.getProperties().forEach((p: Property) => {
|
|
52
|
-
result += ` const ${p.getName()} = data.${p.getName()};\n`;
|
|
53
|
-
});
|
|
54
|
-
result += ' ' + code.trim() + '\n';
|
|
55
|
-
result += '}\n';
|
|
56
|
-
result += '\n';
|
|
57
|
-
|
|
58
|
-
return result;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
writeFunction(fw: FileWriter, functionName: string, returnType:string, code: string) {
|
|
62
|
-
fw.writeLine(0, this.writeFunctionToString(functionName, returnType, code));
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Get a model manager with templatemark model
|
|
67
|
-
* @param {string} outputDir the output directory
|
|
68
|
-
* @returns {ModelManager} the model manager
|
|
69
|
-
*/
|
|
70
|
-
getTemplateMarkModelManager(): ModelManager {
|
|
71
|
-
const modelManager = new ModelManager({ strict: true });
|
|
72
|
-
modelManager.addCTOModel(ConcertoMetaModel.MODEL, 'concertometamodel.cto');
|
|
73
|
-
modelManager.addCTOModel(CommonMarkModel.MODEL, 'commonmark.cto');
|
|
74
|
-
modelManager.addCTOModel(TemplateMarkModel.MODEL, 'templatemark.cto');
|
|
75
|
-
return modelManager;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* Get a model manager with templatemark model
|
|
80
|
-
* @param {string} outputDir the output directory
|
|
81
|
-
* @returns {ModelManager} the model manager
|
|
82
|
-
*/
|
|
83
|
-
getCiceroMarkModelManager(): ModelManager {
|
|
84
|
-
const modelManager = new ModelManager({ strict: true });
|
|
85
|
-
modelManager.addCTOModel(ConcertoMetaModel.MODEL, 'concertometamodel.cto');
|
|
86
|
-
modelManager.addCTOModel(CommonMarkModel.MODEL, 'commonmark.cto');
|
|
87
|
-
modelManager.addCTOModel(CiceroMarkModel.MODEL, 'ciceromark.cto');
|
|
88
|
-
return modelManager;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
generateTypeScript(modelManager: ModelManager, outputDir: string) {
|
|
92
|
-
// generate the TS code for the template model
|
|
93
|
-
const tsVisitor = new CodeGen.TypescriptVisitor();
|
|
94
|
-
const parameters = {
|
|
95
|
-
fileWriter: new FileWriter(outputDir)
|
|
96
|
-
};
|
|
97
|
-
modelManager.accept(tsVisitor, parameters);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* The main iteration function over templatemark. This function
|
|
102
|
-
* looks up an INodeCompiler that can handle each templatemark node
|
|
103
|
-
* and delegates to it for code generation.
|
|
104
|
-
* Note that the result of code generation (a CiceroMark node) is left in
|
|
105
|
-
* the $result global varianble.
|
|
106
|
-
* @param {FileWriter} fw the file writer to use
|
|
107
|
-
* @param {number} level the indentation level
|
|
108
|
-
* @param {object} templateMark the templatemark node to iterate over
|
|
109
|
-
*/
|
|
110
|
-
static doIt(fw:FileWriter, level:number, templateMark: any) {
|
|
111
|
-
fw.writeLine(level, `// start processing ${ModelUtil.getShortName(templateMark.$class)}`);
|
|
112
|
-
traverse(templateMark).forEach(function (context: any) {
|
|
113
|
-
if (typeof context === 'object' && context.$class && typeof context.$class === 'string') {
|
|
114
|
-
const nodeClass = context.$class as string;
|
|
115
|
-
|
|
116
|
-
const compiler = getCompiler(nodeClass);
|
|
117
|
-
const sourceLevel = level+1;
|
|
118
|
-
|
|
119
|
-
this.before( () => {
|
|
120
|
-
compiler.enter(fw, sourceLevel, context);
|
|
121
|
-
compiler.generate(fw, sourceLevel, context, TemplateMarkToTypeScriptCompiler.doIt);
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
this.after( () => {
|
|
125
|
-
compiler.exit(fw, sourceLevel, context);
|
|
126
|
-
});
|
|
127
|
-
}
|
|
128
|
-
});
|
|
129
|
-
fw.writeLine(level, `// end processing ${ModelUtil.getShortName(templateMark.$class)}`);
|
|
130
|
-
}
|
|
131
|
-
/**
|
|
132
|
-
* Converts a TemplateMark document to typescript code that is parameterised
|
|
133
|
-
* by the 'data' argument.
|
|
134
|
-
*
|
|
135
|
-
* The strategy is to traverse the template mark json, converting each node into
|
|
136
|
-
* typescript statements that add an equivalent ciceromark node. Variables will
|
|
137
|
-
* pull values from 'data'.
|
|
138
|
-
*
|
|
139
|
-
* Conditionals and formulae will call the functions produced by
|
|
140
|
-
* compileUserCode.
|
|
141
|
-
*
|
|
142
|
-
* Note that code generation process is itself recursive, for example, when
|
|
143
|
-
* a list definition node is encountered the list must be iterated over
|
|
144
|
-
* based on a variable value coming from data, and code must be
|
|
145
|
-
* generated to produce each list item.
|
|
146
|
-
*
|
|
147
|
-
* All internal variables are prefixed with '$' to ensure (?)
|
|
148
|
-
* that they cannot collide with end user variable names.
|
|
149
|
-
*
|
|
150
|
-
* @param {IDocument} templateMark the TemplateMark Concerto resource
|
|
151
|
-
* @param {string} outputDir the output directory for code
|
|
152
|
-
*/
|
|
153
|
-
compileGenerator(templateMark: Resource, outputDir: string) : void {
|
|
154
|
-
const fw = new FileWriter(outputDir);
|
|
155
|
-
|
|
156
|
-
fw.openFile('generator.ts');
|
|
157
|
-
fw.writeLine(0, '// generated code, do not modify');
|
|
158
|
-
fw.writeLine(0, '');
|
|
159
|
-
|
|
160
|
-
fw.writeLine(0, '// IMPORTS');
|
|
161
|
-
writeImports(fw, 0, this.templateClass);
|
|
162
|
-
fw.writeLine(0, '');
|
|
163
|
-
|
|
164
|
-
fw.writeLine(0, '// GENERATOR');
|
|
165
|
-
writeProlog(fw, 0, this.templateClass);
|
|
166
|
-
|
|
167
|
-
TemplateMarkToTypeScriptCompiler.doIt(fw, 1, templateMark);
|
|
168
|
-
|
|
169
|
-
writeEpilog(fw,0);
|
|
170
|
-
fw.closeFile();
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
copyRuntime(outputDir: string) {
|
|
174
|
-
copySync('./src/drafting', `${outputDir}/${RUNTIME_DIR}/drafting`);
|
|
175
|
-
copySync('./src/TypeScriptRuntime.ts', `${outputDir}/${RUNTIME_DIR}/TypeScriptRuntime.ts`);
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
compile(templateMark: Resource, outputDir: string, options?:CompilationOptions) {
|
|
179
|
-
// generate the model
|
|
180
|
-
this.generateTypeScript(this.modelManager, `${outputDir}`);
|
|
181
|
-
this.generateTypeScript(this.getTemplateMarkModelManager(), `${outputDir}`);
|
|
182
|
-
this.generateTypeScript(this.getCiceroMarkModelManager(), `${outputDir}`);
|
|
183
|
-
|
|
184
|
-
// generate names for all the nodes containing user code
|
|
185
|
-
const namedTemplateMark = TemplateMarkToTypeScriptCompiler.nameUserCode(templateMark.toJSON());
|
|
186
|
-
|
|
187
|
-
// compile the user code
|
|
188
|
-
this.compileUserCode(namedTemplateMark, outputDir);
|
|
189
|
-
|
|
190
|
-
// compile the generator
|
|
191
|
-
if(!options?.skipCopyRuntime) {
|
|
192
|
-
this.copyRuntime(outputDir);
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
// compile the generator
|
|
196
|
-
if(!options?.skipGenerator) {
|
|
197
|
-
this.compileGenerator(namedTemplateMark, outputDir);
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
static nameUserCode(templateMarkDom: any) {
|
|
202
|
-
return traverse(templateMarkDom).map(function (x) {
|
|
203
|
-
if (x && ((x.$class === `${TemplateMarkModel.NAMESPACE}.ConditionalDefinition` && x.condition) ||
|
|
204
|
-
(`${TemplateMarkModel.NAMESPACE}.ClauseDefinition` && x.condition))) {
|
|
205
|
-
x.functionName = `condition_${this.path.join('_')}`;
|
|
206
|
-
}
|
|
207
|
-
this.update(x);
|
|
208
|
-
});
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
compileUserCode(templateMark: Resource, outputDir: string) {
|
|
212
|
-
const fw = new FileWriter(outputDir);
|
|
213
|
-
|
|
214
|
-
fw.openFile('usercode.ts');
|
|
215
|
-
fw.writeLine(0, '// generated code, do not modify');
|
|
216
|
-
fw.writeLine(0, '');
|
|
217
|
-
|
|
218
|
-
fw.writeLine(0, '// IMPORTS');
|
|
219
|
-
fw.writeLine(0, `import * as TemplateModel from './${this.templateClass.getNamespace()}';`);
|
|
220
|
-
fw.writeLine(0, 'import dayjs from \'dayjs\';');
|
|
221
|
-
fw.writeLine(0, 'import jp from \'jsonpath\';');
|
|
222
|
-
fw.writeLine(0, '');
|
|
223
|
-
|
|
224
|
-
const templateMarkDom = templateMark;
|
|
225
|
-
|
|
226
|
-
const functionNodes: [] = traverse(templateMarkDom).reduce(function (acc, x) {
|
|
227
|
-
if (x && x.$class === `${TemplateMarkModel.NAMESPACE}.FormulaDefinition`) {
|
|
228
|
-
acc.push({
|
|
229
|
-
name: x.name,
|
|
230
|
-
code: x.code,
|
|
231
|
-
});
|
|
232
|
-
}
|
|
233
|
-
return acc;
|
|
234
|
-
}, []);
|
|
235
|
-
|
|
236
|
-
const conditionalNodes: [] = traverse(templateMarkDom).reduce(function (acc, x) {
|
|
237
|
-
if (x && x.$class === `${TemplateMarkModel.NAMESPACE}.ConditionalDefinition` && x.condition) {
|
|
238
|
-
acc.push({
|
|
239
|
-
name: x.functionName,
|
|
240
|
-
code: x.condition,
|
|
241
|
-
});
|
|
242
|
-
}
|
|
243
|
-
return acc;
|
|
244
|
-
}, []);
|
|
245
|
-
|
|
246
|
-
const clauseNodes: [] = traverse(templateMarkDom).reduce(function (acc, x) {
|
|
247
|
-
if (x && x.$class === `${TemplateMarkModel.NAMESPACE}.ClauseDefinition` && x.condition) {
|
|
248
|
-
acc.push({
|
|
249
|
-
name: x.functionName,
|
|
250
|
-
code: x.condition,
|
|
251
|
-
});
|
|
252
|
-
}
|
|
253
|
-
return acc;
|
|
254
|
-
}, []);
|
|
255
|
-
|
|
256
|
-
fw.writeLine(0, '// FORMULAE');
|
|
257
|
-
functionNodes.forEach((fun: any) => {
|
|
258
|
-
this.writeFunction(fw, `${fun.name}`, 'any', fun.code.contents);
|
|
259
|
-
});
|
|
260
|
-
fw.writeLine(0, '');
|
|
261
|
-
|
|
262
|
-
fw.writeLine(0, '// CONDITIONALS');
|
|
263
|
-
conditionalNodes.forEach((fun: any) => {
|
|
264
|
-
this.writeFunction(fw, `${fun.name}`, 'boolean', fun.code.contents);
|
|
265
|
-
});
|
|
266
|
-
fw.writeLine(0, '');
|
|
267
|
-
|
|
268
|
-
fw.writeLine(0, '// CLAUSES');
|
|
269
|
-
clauseNodes.forEach((fun: any) => {
|
|
270
|
-
this.writeFunction(fw, `${fun.name}`, 'boolean', fun.code.contents);
|
|
271
|
-
});
|
|
272
|
-
fw.writeLine(0, '');
|
|
273
|
-
|
|
274
|
-
fw.closeFile();
|
|
275
|
-
}
|
|
276
|
-
}
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
3
|
-
* you may not use this file except in compliance with the License.
|
|
4
|
-
* You may obtain a copy of the License at
|
|
5
|
-
*
|
|
6
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
7
|
-
*
|
|
8
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
9
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
10
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
11
|
-
* See the License for the specific language governing permissions and
|
|
12
|
-
* limitations under the License.
|
|
13
|
-
*/
|
|
14
|
-
import { ClassDeclaration, ModelManager } from '@accordproject/concerto-core';
|
|
15
|
-
import { CodeGen } from '@accordproject/concerto-tools';
|
|
16
|
-
import { InMemoryWriter } from '@accordproject/concerto-util';
|
|
17
|
-
import { getTemplateClassDeclaration } from './Common';
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* This class creates the typescript types
|
|
21
|
-
* required to compile Typescript expressions (used in
|
|
22
|
-
* formulae, conditions and clauses) to JavaScript. It uses
|
|
23
|
-
* these to create a compilation context for '@typescript/twoslash'
|
|
24
|
-
* which is used to compile the typescript code.
|
|
25
|
-
*/
|
|
26
|
-
export class TypeScriptCompilationContext {
|
|
27
|
-
|
|
28
|
-
modelManager:ModelManager;
|
|
29
|
-
templateClass:ClassDeclaration;
|
|
30
|
-
|
|
31
|
-
constructor(modelManager:ModelManager) {
|
|
32
|
-
this.modelManager = modelManager;
|
|
33
|
-
this.templateClass = getTemplateClassDeclaration(this.modelManager);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
getTypeScriptFiles() : Record<string,string> {
|
|
37
|
-
const result:Record<string,string> = {};
|
|
38
|
-
|
|
39
|
-
const visitor = new CodeGen.TypescriptVisitor();
|
|
40
|
-
const writer = new InMemoryWriter();
|
|
41
|
-
|
|
42
|
-
const params = {
|
|
43
|
-
fileWriter: writer
|
|
44
|
-
};
|
|
45
|
-
this.modelManager.accept(visitor, params);
|
|
46
|
-
writer.getFilesInMemory().forEach( (value, key) => {
|
|
47
|
-
result[key] = value;
|
|
48
|
-
});
|
|
49
|
-
return result;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
getCompilationContext() : string {
|
|
53
|
-
const files = this.getTypeScriptFiles();
|
|
54
|
-
|
|
55
|
-
let result = '';
|
|
56
|
-
|
|
57
|
-
Object.keys(files).forEach( key => {
|
|
58
|
-
const content = files[key];
|
|
59
|
-
result += `
|
|
60
|
-
// @filename: ${key}
|
|
61
|
-
${content}
|
|
62
|
-
`;
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
result += `
|
|
66
|
-
// @filename: code.ts
|
|
67
|
-
import * as TemplateModel from './${this.templateClass.getNamespace()}';
|
|
68
|
-
import dayjs from 'dayjs';
|
|
69
|
-
import jp from 'jsonpath';
|
|
70
|
-
`;
|
|
71
|
-
return result;
|
|
72
|
-
}
|
|
73
|
-
}
|
package/src/TypeScriptRuntime.ts
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
3
|
-
* you may not use this file except in compliance with the License.
|
|
4
|
-
* You may obtain a copy of the License at
|
|
5
|
-
*
|
|
6
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
7
|
-
*
|
|
8
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
9
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
10
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
11
|
-
* See the License for the specific language governing permissions and
|
|
12
|
-
* limitations under the License.
|
|
13
|
-
*/
|
|
14
|
-
const DEBUG = false;
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* These utility functions are used by the typescript code that
|
|
18
|
-
* is generated from TemplateMark. They manage a stack to improve
|
|
19
|
-
* debuggability.
|
|
20
|
-
*/
|
|
21
|
-
|
|
22
|
-
function dump(op:string, $data:any[]) {
|
|
23
|
-
if(DEBUG) {
|
|
24
|
-
console.log(`${op}: ${JSON.stringify($data)}`);
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export function peek($data:any[]) : any {
|
|
29
|
-
if($data.length <= 0) {
|
|
30
|
-
throw new Error('Empty array');
|
|
31
|
-
}
|
|
32
|
-
const result = $data[$data.length-1];
|
|
33
|
-
dump('peek', $data);
|
|
34
|
-
return result;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export function peekProperty($data:any[], propertyName:string, allowUndefined=false) : any {
|
|
38
|
-
const head = peek($data);
|
|
39
|
-
const result = head[propertyName];
|
|
40
|
-
if(!allowUndefined && result === undefined) {
|
|
41
|
-
throw new Error(`Undefined property ${propertyName}. Current stack is ${JSON.stringify($data)}`);
|
|
42
|
-
}
|
|
43
|
-
return result;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export function push($data:any[], item:any) : number {
|
|
47
|
-
const result = $data.push(item);
|
|
48
|
-
dump('push', $data);
|
|
49
|
-
return result;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export function addChild($data:any[], item:any) {
|
|
53
|
-
const node = peek($data);
|
|
54
|
-
node.nodes ? node.nodes.push(item) : node.nodes = [item];
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export function pop($data:any[]) : any {
|
|
58
|
-
if($data.length <= 0) {
|
|
59
|
-
throw new Error('Empty array');
|
|
60
|
-
}
|
|
61
|
-
const result = $data.pop();
|
|
62
|
-
dump('pop', $data);
|
|
63
|
-
return result;
|
|
64
|
-
}
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
3
|
-
* you may not use this file except in compliance with the License.
|
|
4
|
-
* You may obtain a copy of the License at
|
|
5
|
-
*
|
|
6
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
7
|
-
*
|
|
8
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
9
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
10
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
11
|
-
* See the License for the specific language governing permissions and
|
|
12
|
-
* limitations under the License.
|
|
13
|
-
*/
|
|
14
|
-
import ts from 'typescript';
|
|
15
|
-
import { createDefaultMapFromNodeModules } from '@typescript/vfs';
|
|
16
|
-
import { twoslasher, TwoSlashOptions, TwoSlashReturn } from '@typescript/twoslash';
|
|
17
|
-
import { readFileSync } from 'fs-extra';
|
|
18
|
-
import { ModelManager } from '@accordproject/concerto-core';
|
|
19
|
-
import { TypeScriptCompilationContext } from './TypeScriptCompilationContext';
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Compiles user Typescript code to JavaScript, This uses the '@typescript/twoslash'
|
|
23
|
-
* project which is maintained by the Typescript team and powers their web playground.
|
|
24
|
-
* It uses the TypeScriptCompilationContext class to construct a payload for twoslash
|
|
25
|
-
* that is composed of multiple TS files to compile, along with their 3rd-party module
|
|
26
|
-
* dependencies.
|
|
27
|
-
*/
|
|
28
|
-
export class TypeScriptToJavaScriptCompiler {
|
|
29
|
-
context: string;
|
|
30
|
-
|
|
31
|
-
constructor(modelManager: ModelManager) {
|
|
32
|
-
this.context = new TypeScriptCompilationContext(modelManager).getCompilationContext();
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
compile(typescript: string): TwoSlashReturn {
|
|
36
|
-
const fsMap = createDefaultMapFromNodeModules({
|
|
37
|
-
target: ts.ScriptTarget.ES2020,
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
fsMap.set('/node_modules/@types/dayjs/index.d.ts', readFileSync('./node_modules/dayjs/index.d.ts', 'utf-8'));
|
|
41
|
-
fsMap.set('/node_modules/@types/jsonpath/index.d.ts', readFileSync('./node_modules/@types/jsonpath/index.d.ts', 'utf-8'));
|
|
42
|
-
|
|
43
|
-
const twoSlashCode =`
|
|
44
|
-
${this.context}
|
|
45
|
-
${typescript}
|
|
46
|
-
`;
|
|
47
|
-
|
|
48
|
-
const options: TwoSlashOptions = {
|
|
49
|
-
fsMap,
|
|
50
|
-
defaultOptions: {
|
|
51
|
-
showEmit: true,
|
|
52
|
-
noErrorValidation: true,
|
|
53
|
-
showEmittedFile: 'code.js'
|
|
54
|
-
}
|
|
55
|
-
};
|
|
56
|
-
const result = twoslasher(twoSlashCode, 'ts', options);
|
|
57
|
-
return result;
|
|
58
|
-
}
|
|
59
|
-
}
|
package/src/compilers/Clause.ts
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
3
|
-
* you may not use this file except in compliance with the License.
|
|
4
|
-
* You may obtain a copy of the License at
|
|
5
|
-
*
|
|
6
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
7
|
-
*
|
|
8
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
9
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
10
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
11
|
-
* See the License for the specific language governing permissions and
|
|
12
|
-
* limitations under the License.
|
|
13
|
-
*/
|
|
14
|
-
import { FileWriter } from '@accordproject/concerto-util';
|
|
15
|
-
import { TemplateMarkModel,CommonMarkModel } from '@accordproject/markdown-common';
|
|
16
|
-
import { IClause } from '../model-gen/org.accordproject.ciceromark@0.6.0';
|
|
17
|
-
import { IParagraph } from '../model-gen/org.accordproject.commonmark@0.5.0';
|
|
18
|
-
import { IClauseDefinition, IWithDefinition } from '../model-gen/org.accordproject.templatemark@0.4.0';
|
|
19
|
-
import { AbstractComplexCompiler, getTypeScriptType, makeCiceroMark, writeCloseDataScope, writeCloseGenerateScope, writeCloseNodeScope, writeDebug, writeOpenDataScope, writeOpenGenerateScope, writeOpenNodeScope } from './Common';
|
|
20
|
-
|
|
21
|
-
interface INamedClauseDefinition extends IClauseDefinition {
|
|
22
|
-
readonly functionName: string;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export class Clause extends AbstractComplexCompiler {
|
|
26
|
-
static TYPE = `${TemplateMarkModel.NAMESPACE}.ClauseDefinition`;
|
|
27
|
-
constructor() {
|
|
28
|
-
super(false);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
enter(fw:FileWriter, level:number,templateMarkNode:IWithDefinition) {
|
|
32
|
-
writeDebug(fw, level, templateMarkNode);
|
|
33
|
-
writeOpenNodeScope(fw,level);
|
|
34
|
-
writeOpenDataScope(fw,level,templateMarkNode.name);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
generate(fw:FileWriter, level:number, templateMarkNode:INamedClauseDefinition) {
|
|
38
|
-
const clone = makeCiceroMark(templateMarkNode) as IClause;
|
|
39
|
-
delete (clone as any).condition;
|
|
40
|
-
delete (clone as any).functionName;
|
|
41
|
-
const para = {$class: `${CommonMarkModel.NAMESPACE}.Paragraph`} as IParagraph;
|
|
42
|
-
writeOpenGenerateScope(fw, level);
|
|
43
|
-
if(templateMarkNode.condition) {
|
|
44
|
-
fw.writeLine(level, `const isTrue:boolean = UserCode.${templateMarkNode.functionName}(data,library,now)`);
|
|
45
|
-
}
|
|
46
|
-
else {
|
|
47
|
-
fw.writeLine(level, 'const isTrue:boolean = true;');
|
|
48
|
-
}
|
|
49
|
-
fw.writeLine(level, 'if(isTrue) {');
|
|
50
|
-
fw.writeLine(level, `return ${JSON.stringify(clone)} as ${getTypeScriptType(clone.$class)};`);
|
|
51
|
-
fw.writeLine(level, '}');
|
|
52
|
-
fw.writeLine(level, 'else {');
|
|
53
|
-
fw.writeLine(level, `return ${JSON.stringify(para)} as ${getTypeScriptType(para.$class)};`);
|
|
54
|
-
fw.writeLine(level, '}');
|
|
55
|
-
writeCloseGenerateScope(fw, level);
|
|
56
|
-
delete templateMarkNode.condition;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
exit(fw:FileWriter, level:number) {
|
|
60
|
-
writeCloseDataScope(fw,level);
|
|
61
|
-
writeCloseNodeScope(fw,level);
|
|
62
|
-
}
|
|
63
|
-
}
|