ts2workflows 0.3.0 → 0.4.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.
@@ -1,67 +1,68 @@
1
- /* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-argument */
2
- import * as parser from '@typescript-eslint/typescript-estree';
3
- import { AST_NODE_TYPES } from '@typescript-eslint/utils';
1
+ import { parse, AST_NODE_TYPES, } from '@typescript-eslint/typescript-estree';
4
2
  import * as YAML from 'yaml';
5
3
  import { SubworkflowAST } from '../ast/steps.js';
6
4
  import { WorkflowSyntaxError } from '../errors.js';
7
5
  import { generateStepNames } from '../ast/stepnames.js';
8
- import { assertType } from './asserts.js';
9
- import { parseBlockStatement } from './statements.js';
10
- const { AssignmentPattern, ExportNamedDeclaration, FunctionDeclaration, Identifier, ImportDeclaration, ImportDefaultSpecifier, ImportNamespaceSpecifier, Literal, Program, TSDeclareFunction, TSTypeAliasDeclaration, TSInterfaceDeclaration, } = AST_NODE_TYPES;
6
+ import { parseStatement } from './statements.js';
11
7
  export function transpile(code) {
12
8
  const parserOptions = {
13
9
  jsDocParsingMode: 'none',
14
10
  loc: true,
15
11
  range: false,
16
12
  };
17
- const ast = parser.parse(code, parserOptions);
18
- assertType(ast, Program);
13
+ const ast = parse(code, parserOptions);
19
14
  const workflowAst = { subworkflows: ast.body.flatMap(parseTopLevelStatement) };
20
15
  const workflow = generateStepNames(workflowAst);
21
16
  return YAML.stringify(workflow.render(), { lineWidth: 100 });
22
17
  }
23
18
  function parseTopLevelStatement(node) {
24
- switch (node?.type) {
25
- case FunctionDeclaration:
19
+ switch (node.type) {
20
+ case AST_NODE_TYPES.FunctionDeclaration:
26
21
  return [parseSubworkflows(node)];
27
- case ImportDeclaration:
28
- if (node.specifiers.some((spec) => spec.type === ImportNamespaceSpecifier ||
29
- spec.type === ImportDefaultSpecifier)) {
22
+ case AST_NODE_TYPES.ImportDeclaration:
23
+ if (node.specifiers.some((spec) => spec.type === AST_NODE_TYPES.ImportNamespaceSpecifier ||
24
+ spec.type === AST_NODE_TYPES.ImportDefaultSpecifier)) {
30
25
  throw new WorkflowSyntaxError('Only named imports are allowed', node.loc);
31
26
  }
32
27
  return [];
33
- case ExportNamedDeclaration:
28
+ case AST_NODE_TYPES.ExportNamedDeclaration:
34
29
  // "export" keyword is ignored, but a possible function declaration is transpiled.
35
- if (node?.declaration) {
30
+ if (node.declaration?.type === AST_NODE_TYPES.FunctionDeclaration &&
31
+ node.declaration.id?.type === AST_NODE_TYPES.Identifier) {
32
+ // Why is "as" needed here?
36
33
  return parseTopLevelStatement(node.declaration);
37
34
  }
38
35
  else {
39
36
  return [];
40
37
  }
41
- case TSInterfaceDeclaration:
42
- case TSTypeAliasDeclaration:
43
- case TSDeclareFunction:
38
+ case AST_NODE_TYPES.TSInterfaceDeclaration:
39
+ case AST_NODE_TYPES.TSTypeAliasDeclaration:
40
+ case AST_NODE_TYPES.TSDeclareFunction:
44
41
  // Ignore "type", "interface" and "declare function" at the top-level
45
42
  return [];
46
43
  default:
47
- throw new WorkflowSyntaxError(`Only function definitions, imports and type aliases allowed at the top level, encountered ${node?.type}`, node?.loc);
44
+ throw new WorkflowSyntaxError(`Only function definitions, imports and type aliases allowed at the top level, encountered ${node.type}`, node.loc);
48
45
  }
49
46
  }
50
47
  function parseSubworkflows(node) {
51
- assertType(node, FunctionDeclaration);
52
- if (node.id.type !== Identifier) {
53
- throw new WorkflowSyntaxError('Expected Identifier as a function name', node.id.loc);
54
- }
55
48
  const nodeParams = node.params;
56
49
  const workflowParams = nodeParams.map((param) => {
57
50
  switch (param.type) {
58
- case Identifier:
51
+ case AST_NODE_TYPES.Identifier:
59
52
  return { name: param.name };
60
- case AssignmentPattern:
61
- assertType(param.left, Identifier);
62
- if (param.right.type !== Literal) {
53
+ case AST_NODE_TYPES.AssignmentPattern:
54
+ if (param.left.type !== AST_NODE_TYPES.Identifier) {
55
+ throw new WorkflowSyntaxError('The default value must be an identifier', param.left.loc);
56
+ }
57
+ if (param.right.type !== AST_NODE_TYPES.Literal) {
63
58
  throw new WorkflowSyntaxError('The default value must be a literal', param.right.loc);
64
59
  }
60
+ if (!(typeof param.right.value === 'string' ||
61
+ typeof param.right.value === 'number' ||
62
+ typeof param.right.value === 'boolean' ||
63
+ param.right.value === null)) {
64
+ throw new WorkflowSyntaxError('The default value must be a string, number, boolean or null', param.left.loc);
65
+ }
65
66
  return {
66
67
  name: param.left.name,
67
68
  default: param.right.value,
@@ -70,6 +71,6 @@ function parseSubworkflows(node) {
70
71
  throw new WorkflowSyntaxError('Function parameter must be an identifier or an assignment', param.loc);
71
72
  }
72
73
  });
73
- const steps = parseBlockStatement(node.body, {});
74
+ const steps = parseStatement(node.body, {});
74
75
  return new SubworkflowAST(node.id.name, steps, workflowParams);
75
76
  }
@@ -1,7 +1,8 @@
1
+ import { TSESTree } from '@typescript-eslint/typescript-estree';
1
2
  import { StepName, WorkflowStepAST } from '../ast/steps.js';
2
3
  export interface ParsingContext {
3
4
  breakTarget?: StepName;
4
5
  continueTarget?: StepName;
5
6
  }
6
- export declare function parseBlockStatement(node: any, ctx: ParsingContext): WorkflowStepAST[];
7
+ export declare function parseStatement(node: TSESTree.Statement, ctx: ParsingContext, postSteps?: WorkflowStepAST[]): WorkflowStepAST[];
7
8
  //# sourceMappingURL=statements.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"statements.d.ts","sourceRoot":"","sources":["../../src/transpiler/statements.ts"],"names":[],"mappings":"AAEA,OAAO,EASL,QAAQ,EAMR,eAAe,EAEhB,MAAM,iBAAiB,CAAA;AAyDxB,MAAM,WAAW,cAAc;IAE7B,WAAW,CAAC,EAAE,QAAQ,CAAA;IAEtB,cAAc,CAAC,EAAE,QAAQ,CAAA;CAC1B;AAED,wBAAgB,mBAAmB,CACjC,IAAI,EAAE,GAAG,EACT,GAAG,EAAE,cAAc,GAClB,eAAe,EAAE,CAKnB"}
1
+ {"version":3,"file":"statements.d.ts","sourceRoot":"","sources":["../../src/transpiler/statements.ts"],"names":[],"mappings":"AAAA,OAAO,EAAkB,QAAQ,EAAE,MAAM,sCAAsC,CAAA;AAC/E,OAAO,EASL,QAAQ,EAMR,eAAe,EAChB,MAAM,iBAAiB,CAAA;AA0BxB,MAAM,WAAW,cAAc;IAE7B,WAAW,CAAC,EAAE,QAAQ,CAAA;IAEtB,cAAc,CAAC,EAAE,QAAQ,CAAA;CAC1B;AAED,wBAAgB,cAAc,CAC5B,IAAI,EAAE,QAAQ,CAAC,SAAS,EACxB,GAAG,EAAE,cAAc,EACnB,SAAS,CAAC,EAAE,eAAe,EAAE,GAC5B,eAAe,EAAE,CAGnB"}