@teambit/typescript 0.0.880 → 0.0.882

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.
Files changed (78) hide show
  1. package/dist/cmds/check-types.cmd.js +0 -21
  2. package/dist/cmds/check-types.cmd.js.map +1 -1
  3. package/dist/cmds/write-tsconfig.cmd.js +2 -25
  4. package/dist/cmds/write-tsconfig.cmd.js.map +1 -1
  5. package/dist/dedupe-path.spec.js +6 -8
  6. package/dist/dedupe-path.spec.js.map +1 -1
  7. package/dist/exceptions/index.js +0 -3
  8. package/dist/exceptions/index.js.map +1 -1
  9. package/dist/exceptions/transformer-not-found.js +0 -7
  10. package/dist/exceptions/transformer-not-found.js.map +1 -1
  11. package/dist/export-identifier.js +0 -3
  12. package/dist/export-identifier.js.map +1 -1
  13. package/dist/export-list.js +0 -4
  14. package/dist/export-list.js.map +1 -1
  15. package/dist/index.js +0 -9
  16. package/dist/index.js.map +1 -1
  17. package/dist/schema-extractor-context.js +16 -120
  18. package/dist/schema-extractor-context.js.map +1 -1
  19. package/dist/schema-transformer.plugin.js +0 -8
  20. package/dist/schema-transformer.plugin.js.map +1 -1
  21. package/dist/transformers/binding-element.js +0 -23
  22. package/dist/transformers/binding-element.js.map +1 -1
  23. package/dist/transformers/class-deceleration.js +2 -34
  24. package/dist/transformers/class-deceleration.js.map +1 -1
  25. package/dist/transformers/enum-declaration.js +0 -16
  26. package/dist/transformers/enum-declaration.js.map +1 -1
  27. package/dist/transformers/export-declaration.js +8 -47
  28. package/dist/transformers/export-declaration.js.map +1 -1
  29. package/dist/transformers/function-declaration.js +2 -19
  30. package/dist/transformers/function-declaration.js.map +1 -1
  31. package/dist/transformers/index.js +0 -30
  32. package/dist/transformers/index.js.map +1 -1
  33. package/dist/transformers/interface-declaration.js +0 -25
  34. package/dist/transformers/interface-declaration.js.map +1 -1
  35. package/dist/transformers/source-file-transformer.js +3 -24
  36. package/dist/transformers/source-file-transformer.js.map +1 -1
  37. package/dist/transformers/type-alias.js +0 -23
  38. package/dist/transformers/type-alias.js.map +1 -1
  39. package/dist/transformers/utils/class-element-to-schema.js +0 -35
  40. package/dist/transformers/utils/class-element-to-schema.js.map +1 -1
  41. package/dist/transformers/utils/get-params.js +6 -33
  42. package/dist/transformers/utils/get-params.js.map +1 -1
  43. package/dist/transformers/utils/jsdoc-to-doc-schema.js +4 -49
  44. package/dist/transformers/utils/jsdoc-to-doc-schema.js.map +1 -1
  45. package/dist/transformers/utils/parse-type-from-quick-info.js +4 -16
  46. package/dist/transformers/utils/parse-type-from-quick-info.js.map +1 -1
  47. package/dist/transformers/utils/to-function-like-schema.js +0 -15
  48. package/dist/transformers/utils/to-function-like-schema.js.map +1 -1
  49. package/dist/transformers/utils/type-element-to-schema.js +0 -37
  50. package/dist/transformers/utils/type-element-to-schema.js.map +1 -1
  51. package/dist/transformers/utils/type-node-to-schema.js +5 -71
  52. package/dist/transformers/utils/type-node-to-schema.js.map +1 -1
  53. package/dist/transformers/variable-declaration.js +0 -30
  54. package/dist/transformers/variable-declaration.js.map +1 -1
  55. package/dist/transformers/variable-statement.js +0 -22
  56. package/dist/transformers/variable-statement.js.map +1 -1
  57. package/dist/tsconfig-writer.js +8 -58
  58. package/dist/tsconfig-writer.js.map +1 -1
  59. package/dist/typescript.aspect.js +0 -5
  60. package/dist/typescript.aspect.js.map +1 -1
  61. package/dist/typescript.compiler.js +16 -83
  62. package/dist/typescript.compiler.js.map +1 -1
  63. package/dist/typescript.compiler.spec.js +0 -25
  64. package/dist/typescript.compiler.spec.js.map +1 -1
  65. package/dist/typescript.composition.js +0 -6
  66. package/dist/typescript.composition.js.map +1 -1
  67. package/dist/typescript.extractor.js +6 -50
  68. package/dist/typescript.extractor.js.map +1 -1
  69. package/dist/typescript.main.runtime.js +10 -114
  70. package/dist/typescript.main.runtime.js.map +1 -1
  71. package/dist/typescript.parser.js +3 -30
  72. package/dist/typescript.parser.js.map +1 -1
  73. package/dist/typescript.parser.spec.js +0 -22
  74. package/dist/typescript.parser.spec.js.map +1 -1
  75. package/package-tar/teambit-typescript-0.0.882.tgz +0 -0
  76. package/package.json +15 -15
  77. package/{preview-1666312873430.js → preview-1666496165706.js} +2 -2
  78. package/package-tar/teambit-typescript-0.0.880.tgz +0 -0
@@ -1,86 +1,61 @@
1
1
  "use strict";
2
2
 
3
3
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
-
5
4
  require("core-js/modules/es.array.iterator.js");
6
-
7
5
  require("core-js/modules/es.promise.js");
8
-
9
6
  Object.defineProperty(exports, "__esModule", {
10
7
  value: true
11
8
  });
12
9
  exports.TypeScriptExtractor = void 0;
13
-
14
10
  function _defineProperty2() {
15
11
  const data = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
16
-
17
12
  _defineProperty2 = function () {
18
13
  return data;
19
14
  };
20
-
21
15
  return data;
22
16
  }
23
-
24
17
  function _typescript() {
25
18
  const data = _interopRequireDefault(require("typescript"));
26
-
27
19
  _typescript = function () {
28
20
  return data;
29
21
  };
30
-
31
22
  return data;
32
23
  }
33
-
34
24
  function _semanticsEntities() {
35
25
  const data = require("@teambit/semantics.entities.semantic-schema");
36
-
37
26
  _semanticsEntities = function () {
38
27
  return data;
39
28
  };
40
-
41
29
  return data;
42
30
  }
43
-
44
31
  function _lodash() {
45
32
  const data = require("lodash");
46
-
47
33
  _lodash = function () {
48
34
  return data;
49
35
  };
50
-
51
36
  return data;
52
37
  }
53
-
54
38
  function _exceptions() {
55
39
  const data = require("./exceptions");
56
-
57
40
  _exceptions = function () {
58
41
  return data;
59
42
  };
60
-
61
43
  return data;
62
44
  }
63
-
64
45
  function _schemaExtractorContext() {
65
46
  const data = require("./schema-extractor-context");
66
-
67
47
  _schemaExtractorContext = function () {
68
48
  return data;
69
49
  };
70
-
71
50
  return data;
72
51
  }
73
-
74
52
  function _exportList() {
75
53
  const data = require("./export-list");
76
-
77
54
  _exportList = function () {
78
55
  return data;
79
56
  };
80
-
81
57
  return data;
82
58
  }
83
-
84
59
  class TypeScriptExtractor {
85
60
  constructor(tsconfig, schemaTransformerSlot, tsMain, rootPath, depResolver, workspace) {
86
61
  this.tsconfig = tsconfig;
@@ -91,21 +66,17 @@ class TypeScriptExtractor {
91
66
  this.workspace = workspace;
92
67
  (0, _defineProperty2().default)(this, "tsserver", undefined);
93
68
  }
94
-
95
69
  parseSourceFile(file) {
96
70
  const sourceFile = _typescript().default.createSourceFile(file.path, file.contents.toString('utf8'), _typescript().default.ScriptTarget.Latest, true
97
- /** don't pass the scriptKind, it'll be determined automatically by typescript by the filepath */
98
- ); // leave this commented out, it's helpful when there are issues with ASTs. consider throwing in this case.
71
+ /** don't pass the scriptKind, it'll be determined automatically by typescript by the filepath */);
72
+ // leave this commented out, it's helpful when there are issues with ASTs. consider throwing in this case.
99
73
  // console.log("sourceFile Errors", file.path, sourceFile.parseDiagnostics);
100
-
101
-
102
74
  return sourceFile;
103
75
  }
76
+
104
77
  /**
105
78
  * extract a component schema.
106
79
  */
107
-
108
-
109
80
  async extract(component) {
110
81
  const tsserver = await this.getTsServer();
111
82
  const mainFile = component.mainFile;
@@ -119,72 +90,57 @@ class TypeScriptExtractor {
119
90
  const location = context.getLocation(mainAst);
120
91
  return new (_semanticsEntities().APISchema)(location, apiScheme, component.id);
121
92
  }
122
-
123
93
  async computeExportedIdentifiers(node, context) {
124
94
  const transformer = this.getTransformer(node, context);
125
-
126
95
  if (!transformer || !transformer.getIdentifiers) {
127
96
  throw new (_exceptions().TransformerNotFound)(node, context.component, context.getLocation(node));
128
97
  }
129
-
130
98
  return transformer.getIdentifiers(node, context);
131
99
  }
132
-
133
100
  async createContext(tsserver, component) {
134
101
  const componentDeps = await this.getComponentDeps(component);
135
102
  return new (_schemaExtractorContext().SchemaExtractorContext)(tsserver, component, this, componentDeps);
136
103
  }
137
-
138
104
  async getComponentDeps(component) {
139
105
  const deps = await this.depResolver.getDependencies(component);
140
106
  const componentDeps = deps.getComponentDependencies();
141
107
  return componentDeps;
142
108
  }
143
-
144
109
  async getTsServer() {
145
110
  if (!this.tsserver) {
146
111
  const tsserver = this.tsMain.getTsserverClient();
147
-
148
112
  if (tsserver) {
149
113
  this.tsserver = tsserver;
150
114
  return tsserver;
151
115
  }
152
-
153
116
  this.tsserver = await this.tsMain.initTsserverClient(this.rootPath);
154
117
  return this.tsserver;
155
118
  }
156
-
157
119
  return this.tsserver;
158
120
  }
159
-
160
121
  async computeSchema(node, context) {
161
- const transformer = this.getTransformer(node, context); // leave the next line commented out, it is used for debugging
122
+ const transformer = this.getTransformer(node, context);
123
+ // leave the next line commented out, it is used for debugging
162
124
  // console.log('transformer', transformer.constructor.name, node.getText());
163
-
164
125
  return transformer.transform(node, context);
165
126
  }
166
-
167
127
  async getComponentIDByPath(file) {
168
128
  if (!this.workspace) {
169
129
  return null;
170
130
  }
171
-
172
131
  return this.workspace.getComponentIdByPath(file);
173
132
  }
133
+
174
134
  /**
175
135
  * select the correct transformer for a node.
176
136
  */
177
-
178
-
179
137
  getTransformer(node, context) {
180
138
  const transformers = (0, _lodash().flatten)(this.schemaTransformerSlot.values());
181
139
  const transformer = transformers.find(singleTransformer => singleTransformer.predicate(node));
182
140
  if (!transformer) throw new (_exceptions().TransformerNotFound)(node, context.component, context.getLocation(node));
183
141
  return transformer;
184
142
  }
185
-
186
143
  }
187
-
188
144
  exports.TypeScriptExtractor = TypeScriptExtractor;
189
145
 
190
146
  //# sourceMappingURL=typescript.extractor.js.map
@@ -1 +1 @@
1
- {"version":3,"names":["TypeScriptExtractor","constructor","tsconfig","schemaTransformerSlot","tsMain","rootPath","depResolver","workspace","undefined","parseSourceFile","file","sourceFile","ts","createSourceFile","path","contents","toString","ScriptTarget","Latest","extract","component","tsserver","getTsServer","mainFile","mainAst","context","createContext","exportNames","computeExportedIdentifiers","setExports","ExportList","moduleSchema","computeSchema","flatExportsRecursively","apiScheme","location","getLocation","APISchema","id","node","transformer","getTransformer","getIdentifiers","TransformerNotFound","componentDeps","getComponentDeps","SchemaExtractorContext","deps","getDependencies","getComponentDependencies","getTsserverClient","initTsserverClient","transform","getComponentIDByPath","getComponentIdByPath","transformers","flatten","values","find","singleTransformer","predicate"],"sources":["typescript.extractor.ts"],"sourcesContent":["import ts, { Node, SourceFile } from 'typescript';\nimport { SchemaExtractor } from '@teambit/schema';\nimport { TsserverClient } from '@teambit/ts-server';\nimport type { Workspace } from '@teambit/workspace';\nimport { ComponentDependency, DependencyResolverMain } from '@teambit/dependency-resolver';\nimport { SchemaNode, APISchema, Module } from '@teambit/semantics.entities.semantic-schema';\nimport { Component } from '@teambit/component';\nimport { AbstractVinyl } from '@teambit/legacy/dist/consumer/component/sources';\nimport { flatten } from 'lodash';\nimport { TypescriptMain, SchemaTransformerSlot } from './typescript.main.runtime';\nimport { TransformerNotFound } from './exceptions';\nimport { SchemaExtractorContext } from './schema-extractor-context';\nimport { ExportList } from './export-list';\n\nexport class TypeScriptExtractor implements SchemaExtractor {\n constructor(\n private tsconfig: any,\n private schemaTransformerSlot: SchemaTransformerSlot,\n private tsMain: TypescriptMain,\n private rootPath: string,\n private depResolver: DependencyResolverMain,\n private workspace: Workspace | undefined\n ) {}\n\n parseSourceFile(file: AbstractVinyl): SourceFile {\n const sourceFile = ts.createSourceFile(\n file.path,\n file.contents.toString('utf8'),\n ts.ScriptTarget.Latest,\n true\n /** don't pass the scriptKind, it'll be determined automatically by typescript by the filepath */\n );\n // leave this commented out, it's helpful when there are issues with ASTs. consider throwing in this case.\n // console.log(\"sourceFile Errors\", file.path, sourceFile.parseDiagnostics);\n return sourceFile;\n }\n\n /**\n * extract a component schema.\n */\n async extract(component: Component): Promise<APISchema> {\n const tsserver = await this.getTsServer();\n const mainFile = component.mainFile;\n const mainAst = this.parseSourceFile(mainFile);\n const context = await this.createContext(tsserver, component);\n const exportNames = await this.computeExportedIdentifiers(mainAst, context);\n context.setExports(new ExportList(exportNames));\n const moduleSchema = (await this.computeSchema(mainAst, context)) as Module;\n moduleSchema.flatExportsRecursively();\n const apiScheme = moduleSchema;\n const location = context.getLocation(mainAst);\n\n return new APISchema(location, apiScheme, component.id);\n }\n\n async computeExportedIdentifiers(node: Node, context: SchemaExtractorContext) {\n const transformer = this.getTransformer(node, context);\n if (!transformer || !transformer.getIdentifiers) {\n throw new TransformerNotFound(node, context.component, context.getLocation(node));\n }\n return transformer.getIdentifiers(node, context);\n }\n\n private async createContext(tsserver: TsserverClient, component: Component): Promise<SchemaExtractorContext> {\n const componentDeps = await this.getComponentDeps(component);\n return new SchemaExtractorContext(tsserver, component, this, componentDeps);\n }\n\n private async getComponentDeps(component: Component): Promise<ComponentDependency[]> {\n const deps = await this.depResolver.getDependencies(component);\n const componentDeps = deps.getComponentDependencies();\n return componentDeps;\n }\n\n private tsserver: TsserverClient | undefined = undefined;\n\n private async getTsServer() {\n if (!this.tsserver) {\n const tsserver = this.tsMain.getTsserverClient();\n if (tsserver) {\n this.tsserver = tsserver;\n return tsserver;\n }\n\n this.tsserver = await this.tsMain.initTsserverClient(this.rootPath);\n return this.tsserver;\n }\n\n return this.tsserver;\n }\n\n async computeSchema(node: Node, context: SchemaExtractorContext): Promise<SchemaNode> {\n const transformer = this.getTransformer(node, context);\n // leave the next line commented out, it is used for debugging\n // console.log('transformer', transformer.constructor.name, node.getText());\n return transformer.transform(node, context);\n }\n\n async getComponentIDByPath(file: string) {\n if (!this.workspace) {\n return null;\n }\n return this.workspace.getComponentIdByPath(file);\n }\n\n /**\n * select the correct transformer for a node.\n */\n private getTransformer(node: Node, context: SchemaExtractorContext) {\n const transformers = flatten(this.schemaTransformerSlot.values());\n const transformer = transformers.find((singleTransformer) => singleTransformer.predicate(node));\n\n if (!transformer) throw new TransformerNotFound(node, context.component, context.getLocation(node));\n\n return transformer;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAAA;EAAA;;EAAA;IAAA;EAAA;;EAAA;AAAA;;AAKA;EAAA;;EAAA;IAAA;EAAA;;EAAA;AAAA;;AAGA;EAAA;;EAAA;IAAA;EAAA;;EAAA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MAAL,CAAY8C,iBAAZ,EAAjB;;MACA,IAAI7B,QAAJ,EAAc;QACZ,KAAKA,QAAL,GAAgBA,QAAhB;QACA,OAAOA,QAAP;MACD;;MAED,KAAKA,QAAL,GAAgB,MAAM,KAAKjB,MAAL,CAAY+C,kBAAZ,CAA+B,KAAK9C,QAApC,CAAtB;MACA,OAAO,KAAKgB,QAAZ;IACD;;IAED,OAAO,KAAKA,QAAZ;EACD;;EAEkB,MAAbW,aAAa,CAACO,IAAD,EAAad,OAAb,EAAmE;IACpF,MAAMe,WAAW,GAAG,KAAKC,cAAL,CAAoBF,IAApB,EAA0Bd,OAA1B,CAApB,CADoF,CAEpF;IACA;;IACA,OAAOe,WAAW,CAACY,SAAZ,CAAsBb,IAAtB,EAA4Bd,OAA5B,CAAP;EACD;;EAEyB,MAApB4B,oBAAoB,CAAC3C,IAAD,EAAe;IACvC,IAAI,CAAC,KAAKH,SAAV,EAAqB;MACnB,OAAO,IAAP;IACD;;IACD,OAAO,KAAKA,SAAL,CAAe+C,oBAAf,CAAoC5C,IAApC,CAAP;EACD;EAED;AACF;AACA;;;EACU+B,cAAc,CAACF,IAAD,EAAad,OAAb,EAA8C;IAClE,MAAM8B,YAAY,GAAG,IAAAC,iBAAA,EAAQ,KAAKrD,qBAAL,CAA2BsD,MAA3B,EAAR,CAArB;IACA,MAAMjB,WAAW,GAAGe,YAAY,CAACG,IAAb,CAAmBC,iBAAD,IAAuBA,iBAAiB,CAACC,SAAlB,CAA4BrB,IAA5B,CAAzC,CAApB;IAEA,IAAI,CAACC,WAAL,EAAkB,MAAM,KAAIG,iCAAJ,EAAwBJ,IAAxB,EAA8Bd,OAAO,CAACL,SAAtC,EAAiDK,OAAO,CAACW,WAAR,CAAoBG,IAApB,CAAjD,CAAN;IAElB,OAAOC,WAAP;EACD;;AArGyD"}
1
+ {"version":3,"names":["TypeScriptExtractor","constructor","tsconfig","schemaTransformerSlot","tsMain","rootPath","depResolver","workspace","undefined","parseSourceFile","file","sourceFile","ts","createSourceFile","path","contents","toString","ScriptTarget","Latest","extract","component","tsserver","getTsServer","mainFile","mainAst","context","createContext","exportNames","computeExportedIdentifiers","setExports","ExportList","moduleSchema","computeSchema","flatExportsRecursively","apiScheme","location","getLocation","APISchema","id","node","transformer","getTransformer","getIdentifiers","TransformerNotFound","componentDeps","getComponentDeps","SchemaExtractorContext","deps","getDependencies","getComponentDependencies","getTsserverClient","initTsserverClient","transform","getComponentIDByPath","getComponentIdByPath","transformers","flatten","values","find","singleTransformer","predicate"],"sources":["typescript.extractor.ts"],"sourcesContent":["import ts, { Node, SourceFile } from 'typescript';\nimport { SchemaExtractor } from '@teambit/schema';\nimport { TsserverClient } from '@teambit/ts-server';\nimport type { Workspace } from '@teambit/workspace';\nimport { ComponentDependency, DependencyResolverMain } from '@teambit/dependency-resolver';\nimport { SchemaNode, APISchema, Module } from '@teambit/semantics.entities.semantic-schema';\nimport { Component } from '@teambit/component';\nimport { AbstractVinyl } from '@teambit/legacy/dist/consumer/component/sources';\nimport { flatten } from 'lodash';\nimport { TypescriptMain, SchemaTransformerSlot } from './typescript.main.runtime';\nimport { TransformerNotFound } from './exceptions';\nimport { SchemaExtractorContext } from './schema-extractor-context';\nimport { ExportList } from './export-list';\n\nexport class TypeScriptExtractor implements SchemaExtractor {\n constructor(\n private tsconfig: any,\n private schemaTransformerSlot: SchemaTransformerSlot,\n private tsMain: TypescriptMain,\n private rootPath: string,\n private depResolver: DependencyResolverMain,\n private workspace: Workspace | undefined\n ) {}\n\n parseSourceFile(file: AbstractVinyl): SourceFile {\n const sourceFile = ts.createSourceFile(\n file.path,\n file.contents.toString('utf8'),\n ts.ScriptTarget.Latest,\n true\n /** don't pass the scriptKind, it'll be determined automatically by typescript by the filepath */\n );\n // leave this commented out, it's helpful when there are issues with ASTs. consider throwing in this case.\n // console.log(\"sourceFile Errors\", file.path, sourceFile.parseDiagnostics);\n return sourceFile;\n }\n\n /**\n * extract a component schema.\n */\n async extract(component: Component): Promise<APISchema> {\n const tsserver = await this.getTsServer();\n const mainFile = component.mainFile;\n const mainAst = this.parseSourceFile(mainFile);\n const context = await this.createContext(tsserver, component);\n const exportNames = await this.computeExportedIdentifiers(mainAst, context);\n context.setExports(new ExportList(exportNames));\n const moduleSchema = (await this.computeSchema(mainAst, context)) as Module;\n moduleSchema.flatExportsRecursively();\n const apiScheme = moduleSchema;\n const location = context.getLocation(mainAst);\n\n return new APISchema(location, apiScheme, component.id);\n }\n\n async computeExportedIdentifiers(node: Node, context: SchemaExtractorContext) {\n const transformer = this.getTransformer(node, context);\n if (!transformer || !transformer.getIdentifiers) {\n throw new TransformerNotFound(node, context.component, context.getLocation(node));\n }\n return transformer.getIdentifiers(node, context);\n }\n\n private async createContext(tsserver: TsserverClient, component: Component): Promise<SchemaExtractorContext> {\n const componentDeps = await this.getComponentDeps(component);\n return new SchemaExtractorContext(tsserver, component, this, componentDeps);\n }\n\n private async getComponentDeps(component: Component): Promise<ComponentDependency[]> {\n const deps = await this.depResolver.getDependencies(component);\n const componentDeps = deps.getComponentDependencies();\n return componentDeps;\n }\n\n private tsserver: TsserverClient | undefined = undefined;\n\n private async getTsServer() {\n if (!this.tsserver) {\n const tsserver = this.tsMain.getTsserverClient();\n if (tsserver) {\n this.tsserver = tsserver;\n return tsserver;\n }\n\n this.tsserver = await this.tsMain.initTsserverClient(this.rootPath);\n return this.tsserver;\n }\n\n return this.tsserver;\n }\n\n async computeSchema(node: Node, context: SchemaExtractorContext): Promise<SchemaNode> {\n const transformer = this.getTransformer(node, context);\n // leave the next line commented out, it is used for debugging\n // console.log('transformer', transformer.constructor.name, node.getText());\n return transformer.transform(node, context);\n }\n\n async getComponentIDByPath(file: string) {\n if (!this.workspace) {\n return null;\n }\n return this.workspace.getComponentIdByPath(file);\n }\n\n /**\n * select the correct transformer for a node.\n */\n private getTransformer(node: Node, context: SchemaExtractorContext) {\n const transformers = flatten(this.schemaTransformerSlot.values());\n const transformer = transformers.find((singleTransformer) => singleTransformer.predicate(node));\n\n if (!transformer) throw new TransformerNotFound(node, context.component, context.getLocation(node));\n\n return transformer;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAAA;EAAA;EAAA;IAAA;EAAA;EAAA;AAAA;AAKA;EAAA;EAAA;IAAA;EAAA;EAAA;AAAA;AAGA;EAAA;EAAA;IAAA;EAAA;EAAA;AAAA;AAEA;EAAA;EAAA;IAAA;EAAA;EAAA;AAAA;AACA;EAAA;EAAA;IAAA;EAAA;EAAA;AAAA;AACA;EAAA;EAAA;IAAA;EAAA;EAAA;AAAA;AAEO,MAAMA,mBAAmB,CAA4B;EAC1DC,WAAW,CACDC,QAAa,EACbC,qBAA4C,EAC5CC,MAAsB,EACtBC,QAAgB,EAChBC,WAAmC,EACnCC,SAAgC,EACxC;IAAA,KANQL,QAAa,GAAbA,QAAa;IAAA,KACbC,qBAA4C,GAA5CA,qBAA4C;IAAA,KAC5CC,MAAsB,GAAtBA,MAAsB;IAAA,KACtBC,QAAgB,GAAhBA,QAAgB;IAAA,KAChBC,WAAmC,GAAnCA,WAAmC;IAAA,KACnCC,SAAgC,GAAhCA,SAAgC;IAAA,kDAqDKC,SAAS;EApDrD;EAEHC,eAAe,CAACC,IAAmB,EAAc;IAC/C,MAAMC,UAAU,GAAGC,qBAAE,CAACC,gBAAgB,CACpCH,IAAI,CAACI,IAAI,EACTJ,IAAI,CAACK,QAAQ,CAACC,QAAQ,CAAC,MAAM,CAAC,EAC9BJ,qBAAE,CAACK,YAAY,CAACC,MAAM,EACtB;IACA,kGACD;IACD;IACA;IACA,OAAOP,UAAU;EACnB;;EAEA;AACF;AACA;EACE,MAAMQ,OAAO,CAACC,SAAoB,EAAsB;IACtD,MAAMC,QAAQ,GAAG,MAAM,IAAI,CAACC,WAAW,EAAE;IACzC,MAAMC,QAAQ,GAAGH,SAAS,CAACG,QAAQ;IACnC,MAAMC,OAAO,GAAG,IAAI,CAACf,eAAe,CAACc,QAAQ,CAAC;IAC9C,MAAME,OAAO,GAAG,MAAM,IAAI,CAAC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