@nova-energy/openamx 0.15.0 → 0.18.0

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.
@@ -137,6 +137,74 @@ function expressionReferencesIdentifier(expression, name) {
137
137
  default: return false;
138
138
  }
139
139
  }
140
+ function identifierSource(expression, name) {
141
+ if (expression.type === 'identifier')
142
+ return expression.name === name ? expression.source : undefined;
143
+ switch (expression.type) {
144
+ case 'binaryExpression': return identifierSource(expression.left, name) ?? identifierSource(expression.right, name);
145
+ case 'typeTestExpression':
146
+ case 'measurementAttachment':
147
+ case 'measurementConversion':
148
+ return identifierSource(expression.value, name);
149
+ case 'unaryExpression': return identifierSource(expression.argument, name);
150
+ case 'conditionalExpression': return identifierSource(expression.test, name)
151
+ ?? identifierSource(expression.consequent, name) ?? identifierSource(expression.alternate, name);
152
+ case 'bracedIfExpression': return identifierSource(expression.test, name)
153
+ ?? bodyIdentifierSource(expression.consequent, name) ?? bodyIdentifierSource(expression.alternate, name);
154
+ case 'stringInterpolation':
155
+ return expression.parts.find((part) => typeof part !== 'string')
156
+ ? expression.parts.reduce((source, part) => source ?? (typeof part === 'string' ? undefined : identifierSource(part, name)), undefined)
157
+ : undefined;
158
+ case 'matchExpression': return identifierSource(expression.expression, name)
159
+ ?? expression.cases.reduce((source, item) => source ?? identifierSource(item.expression, name), undefined)
160
+ ?? identifierSource(expression.defaultExpression, name);
161
+ case 'rangeExpression': return identifierSource(expression.start, name) ?? identifierSource(expression.end, name);
162
+ case 'functionCall': return expression.arguments.reduce((source, argument) => source ?? identifierSource(argument, name), undefined);
163
+ case 'listLiteral': return expression.elements.reduce((source, item) => source ?? identifierSource(item, name), undefined);
164
+ case 'forExpression': return identifierSource(expression.iterable, name) ?? bodyIdentifierSource(expression.body, name);
165
+ case 'recordConstructor': return expression.fields.reduce((source, field) => source ?? identifierSource(field.expression, name), undefined);
166
+ case 'fieldAccess': return identifierSource(expression.receiver, name);
167
+ case 'listAccess': return identifierSource(expression.receiver, name) ?? identifierSource(expression.index, name);
168
+ default: return undefined;
169
+ }
170
+ }
171
+ function bodyIdentifierSource(statements, name) {
172
+ for (const statement of statements) {
173
+ let source;
174
+ switch (statement.type) {
175
+ case 'variableDeclaration':
176
+ source = identifierSource(statement.expression, name);
177
+ break;
178
+ case 'assignmentStatement':
179
+ case 'compoundAssignmentStatement':
180
+ source = identifierSource(statement.expression, name);
181
+ break;
182
+ case 'returnStatement':
183
+ source = identifierSource(statement.expression, name);
184
+ break;
185
+ case 'forStatement':
186
+ source = identifierSource(statement.iterable, name) ?? bodyIdentifierSource(statement.body, name);
187
+ break;
188
+ case 'bracedIfStatement':
189
+ source = identifierSource(statement.test, name)
190
+ ?? bodyIdentifierSource(statement.consequent, name)
191
+ ?? (statement.alternate ? bodyIdentifierSource(statement.alternate, name) : undefined);
192
+ break;
193
+ case 'addStatement':
194
+ source = identifierSource(statement.value, name)
195
+ ?? (statement.index ? identifierSource(statement.index, name) : undefined);
196
+ break;
197
+ case 'removeStatement':
198
+ source = identifierSource(statement.count, name)
199
+ ?? (statement.index ? identifierSource(statement.index, name) : undefined);
200
+ break;
201
+ default: source = undefined;
202
+ }
203
+ if (source)
204
+ return source;
205
+ }
206
+ return undefined;
207
+ }
140
208
  function bodyReferencesIdentifier(statements, name) {
141
209
  return statements.some(statement => {
142
210
  switch (statement.type) {
@@ -1097,6 +1165,60 @@ export function checkDocument(doc, file, context) {
1097
1165
  fail('AMX3021', 'Every possible path in an if expression must return a value', closingSource ?? expressionSource);
1098
1166
  return result;
1099
1167
  }
1168
+ function checkFunctionSequence(statements, returnType) {
1169
+ let canFallThrough = true;
1170
+ for (const statement of statements) {
1171
+ const statementCanFallThrough = checkFunctionStatement(statement, returnType);
1172
+ if (canFallThrough)
1173
+ canFallThrough = statementCanFallThrough;
1174
+ }
1175
+ return canFallThrough;
1176
+ }
1177
+ function checkFunctionScopedSequence(statements, returnType) {
1178
+ const original = bindings;
1179
+ bindings = new Map(original);
1180
+ try {
1181
+ return checkFunctionSequence(statements, returnType);
1182
+ }
1183
+ finally {
1184
+ bindings = original;
1185
+ }
1186
+ }
1187
+ function checkFunctionStatement(statement, returnType) {
1188
+ switch (statement.type) {
1189
+ case 'returnStatement': {
1190
+ const actual = infer(statement.expression, returnType);
1191
+ requireType(actual, returnType, statement.expression.source ?? statement.source);
1192
+ return false;
1193
+ }
1194
+ case 'bracedIfStatement': {
1195
+ requireType(infer(statement.test), named('Boolean'), statement.test.source);
1196
+ const consequentCanFallThrough = checkFunctionScopedSequence(statement.consequent, returnType);
1197
+ if (!statement.alternate)
1198
+ return true;
1199
+ const alternateCanFallThrough = checkFunctionScopedSequence(statement.alternate, returnType);
1200
+ return consequentCanFallThrough || alternateCanFallThrough;
1201
+ }
1202
+ case 'forStatement': {
1203
+ const iterable = infer(statement.iterable);
1204
+ if (iterable.kind !== 'list')
1205
+ fail(iterable.kind === 'measurement' ? 'AMX3007' : 'AMX3003', 'For loop needs a list or range', statement.iterable.source ?? statement.source);
1206
+ const original = bindings;
1207
+ bindings = new Map(original);
1208
+ bindings.set(statement.variable, iterable.element);
1209
+ try {
1210
+ checkFunctionSequence(statement.body, returnType);
1211
+ }
1212
+ finally {
1213
+ bindings = original;
1214
+ }
1215
+ return true;
1216
+ }
1217
+ default:
1218
+ checkStatement(statement);
1219
+ return true;
1220
+ }
1221
+ }
1100
1222
  function checkDefault(expression, target) {
1101
1223
  if (expression.type === 'listLiteral') {
1102
1224
  for (const item of expression.elements)
@@ -1351,7 +1473,44 @@ export function checkDocument(doc, file, context) {
1351
1473
  localBindings.set(time.name, named('Number'));
1352
1474
  bindings = localBindings;
1353
1475
  try {
1354
- for (const element of diagram.elements) {
1476
+ function checkDiagramContent(content) {
1477
+ for (const node of content) {
1478
+ if (node.type === 'diagramElement') {
1479
+ checkDiagramElement(node);
1480
+ continue;
1481
+ }
1482
+ if (node.type === 'diagramForBlock') {
1483
+ if (node.variable === time.name) {
1484
+ fail('AMXD6001', `Variable [${time.name}] is already used to control the diagrams timeline. Use a different variable name.`, node.variableSource ?? node.source);
1485
+ }
1486
+ if (expressionReferencesIdentifier(node.iterable, time.name)) {
1487
+ fail('AMXD6001', `Variable [${time.name}] is already used to control the diagrams timeline. Use a different variable name.`, identifierSource(node.iterable, time.name) ?? node.iterableSource ?? node.source);
1488
+ }
1489
+ const iterable = infer(node.iterable);
1490
+ if (iterable.kind !== 'list') {
1491
+ fail(iterable.kind === 'measurement' ? 'AMX3007' : 'AMX3003', 'For loop needs a list or range', node.iterableSource ?? node.source);
1492
+ }
1493
+ const outerBindings = bindings;
1494
+ bindings = new Map(outerBindings);
1495
+ bindings.set(node.variable, iterable.element);
1496
+ try {
1497
+ checkDiagramContent(node.content);
1498
+ }
1499
+ finally {
1500
+ bindings = outerBindings;
1501
+ }
1502
+ continue;
1503
+ }
1504
+ if (expressionReferencesIdentifier(node.test, time.name)) {
1505
+ fail('AMXD6001', `Variable [${time.name}] is already used to control the diagrams timeline. Use a different variable name.`, identifierSource(node.test, time.name) ?? node.testSource ?? node.source);
1506
+ }
1507
+ requireType(infer(node.test), named('Boolean'), node.testSource ?? node.source);
1508
+ checkDiagramContent(node.consequent);
1509
+ if (node.alternate)
1510
+ checkDiagramContent(node.alternate);
1511
+ }
1512
+ }
1513
+ function checkDiagramElement(element) {
1355
1514
  diagramElementCount++;
1356
1515
  if (diagramElementCount > 512) {
1357
1516
  fail('AMXD013', 'A single report can have up to 512 elements maximum.', element.source);
@@ -1411,6 +1570,7 @@ export function checkDocument(doc, file, context) {
1411
1570
  }
1412
1571
  }
1413
1572
  }
1573
+ checkDiagramContent(diagram.content);
1414
1574
  }
1415
1575
  finally {
1416
1576
  bindings = outerBindings;
@@ -1568,13 +1728,23 @@ export function checkDocument(doc, file, context) {
1568
1728
  paramScope.set(parameter.name, resolve(parameter.annotation));
1569
1729
  }
1570
1730
  const returnType = resolve(statement.returnType);
1571
- if (bodyContainsForExpression(statement.body))
1572
- fail('AMX3003', 'Function bodies cannot contain a for expression', statement.body.source ?? statement.source);
1573
1731
  const outerBindings = bindings;
1574
1732
  bindings = paramScope;
1575
1733
  try {
1576
- const actual = infer(statement.body, returnType);
1577
- requireType(actual, returnType, statement.body.source ?? statement.source);
1734
+ if (statement.body.kind === 'expression') {
1735
+ if (bodyContainsForExpression(statement.body.expression))
1736
+ fail('AMX3003', 'Function bodies cannot contain a for expression', statement.body.expression.source ?? statement.source);
1737
+ const actual = infer(statement.body.expression, returnType);
1738
+ requireType(actual, returnType, statement.body.expression.source ?? statement.source);
1739
+ }
1740
+ else {
1741
+ if (bodyContainsForStatements(statement.body.statements)) {
1742
+ fail('AMX3003', 'Function bodies cannot contain a for expression', statement.body.source ?? statement.source);
1743
+ }
1744
+ if (checkFunctionSequence(statement.body.statements, returnType)) {
1745
+ fail('AMXF4001', 'Missing return value from function', statement.body.closingSource);
1746
+ }
1747
+ }
1578
1748
  }
1579
1749
  finally {
1580
1750
  bindings = outerBindings;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nova-energy/openamx",
3
- "version": "0.15.0",
3
+ "version": "0.18.0",
4
4
  "description": "OpenAMX is an open-source, text-based, computable document format for engineering knowledge.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -13,13 +13,13 @@
13
13
  "dependencies": {
14
14
  "cac": "^6.7.14",
15
15
  "csv-parse": "^7.0.3",
16
- "docx": "^9.8.1",
16
+ "docx": "^9.14.0",
17
17
  "echarts": "6.1.0",
18
18
  "marked": "^14.1.4",
19
19
  "pdfmake": "0.3.11",
20
20
  "sanitize-html": "2.17.0",
21
21
  "sharp": "^0.35.5",
22
- "yaml": "^2.9.0"
22
+ "yaml": "^2.9.1"
23
23
  },
24
24
  "engines": {
25
25
  "bun": ">=1.0.0"