@teambit/typescript 0.0.523 → 0.0.527

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.
@@ -149,8 +149,8 @@ class TypescriptMain {
149
149
  }
150
150
 
151
151
  static async provider([schema, loggerExt]) {
152
- schema.registerParser(new (_typescript5().TypeScriptParser)());
153
152
  const logger = loggerExt.createLogger(_typescript3().TypescriptAspect.id);
153
+ schema.registerParser(new (_typescript5().TypeScriptParser)(logger));
154
154
  return new TypescriptMain(logger);
155
155
  }
156
156
 
@@ -1 +1 @@
1
- {"version":3,"sources":["typescript.main.runtime.ts"],"names":["TypescriptMain","constructor","logger","createCompiler","options","transformers","tsModule","ts","configMutator","TypescriptConfigMutator","transformerContext","afterMutation","runTransformersWithContext","clone","TypescriptCompiler","TypescriptAspect","id","raw","createSchemaExtractor","tsconfig","TypeScriptExtractor","getPackageJsonProps","main","types","provider","schema","loggerExt","registerParser","TypeScriptParser","createLogger","MainRuntime","SchemaAspect","LoggerAspect","addRuntime","config","context","Array","isArray","newConfig","reduce","acc","transformer"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AACA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AAEA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AACA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AAEA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AAEA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AAEA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AACA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AACA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AAaO,MAAMA,cAAN,CAAqB;AAC1BC,EAAAA,WAAW,CAASC,MAAT,EAAyB;AAAA,SAAhBA,MAAgB,GAAhBA,MAAgB;AAAE;AACtC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;AAClC,QAAMI,SAAS,GAAGjC,YAAY,CAACkC,MAAb,CAAoB,CAACC,GAAD,EAAMC,WAAN,KAAsB;AAC1D,WAAOA,WAAW,CAACD,GAAD,EAAML,OAAN,CAAlB;AACD,GAFiB,EAEfD,MAFe,CAAlB;AAGA,SAAOI,SAAP;AACD","sourcesContent":["import ts, { TsConfigSourceFile } from 'typescript';\nimport { MainRuntime } from '@teambit/cli';\nimport { Compiler } from '@teambit/compiler';\nimport { Logger, LoggerAspect, LoggerMain } from '@teambit/logger';\nimport { SchemaAspect, SchemaExtractor, SchemaMain } from '@teambit/schema';\nimport { PackageJsonProps } from '@teambit/pkg';\nimport { TypescriptConfigMutator } from '@teambit/typescript.modules.ts-config-mutator';\n\nimport { TypeScriptExtractor } from './typescript.extractor';\nimport { TypeScriptCompilerOptions } from './compiler-options';\nimport { TypescriptAspect } from './typescript.aspect';\nimport { TypescriptCompiler } from './typescript.compiler';\nimport { TypeScriptParser } from './typescript.parser';\n\nexport type TsMode = 'build' | 'dev';\n\nexport type TsConfigTransformContext = {\n // mode: TsMode;\n};\n\nexport type TsConfigTransformer = (\n config: TypescriptConfigMutator,\n context: TsConfigTransformContext\n) => TypescriptConfigMutator;\n\nexport class TypescriptMain {\n constructor(private logger: Logger) {}\n /**\n * create a new compiler.\n */\n createCompiler(\n options: TypeScriptCompilerOptions,\n transformers: TsConfigTransformer[] = [],\n tsModule = ts\n ): Compiler {\n const configMutator = new TypescriptConfigMutator(options);\n const transformerContext: TsConfigTransformContext = {};\n const afterMutation = runTransformersWithContext(configMutator.clone(), transformers, transformerContext);\n return new TypescriptCompiler(TypescriptAspect.id, this.logger, afterMutation.raw, tsModule);\n }\n\n /**\n * create an instance of a typescript semantic schema extractor.\n */\n createSchemaExtractor(tsconfig: TsConfigSourceFile): SchemaExtractor {\n return new TypeScriptExtractor(tsconfig);\n }\n\n /**\n * add the default package json properties to the component\n * :TODO @gilad why do we need this DSL? can't I just get the args here.\n */\n getPackageJsonProps(): PackageJsonProps {\n return {\n main: 'dist/{main}.js',\n types: '{main}.ts',\n };\n }\n\n static runtime = MainRuntime;\n static dependencies = [SchemaAspect, LoggerAspect];\n\n static async provider([schema, loggerExt]: [SchemaMain, LoggerMain]) {\n schema.registerParser(new TypeScriptParser());\n const logger = loggerExt.createLogger(TypescriptAspect.id);\n\n return new TypescriptMain(logger);\n }\n}\n\nTypescriptAspect.addRuntime(TypescriptMain);\n\nexport function runTransformersWithContext(\n config: TypescriptConfigMutator,\n transformers: TsConfigTransformer[] = [],\n context: TsConfigTransformContext\n): TypescriptConfigMutator {\n if (!Array.isArray(transformers)) return config;\n const newConfig = transformers.reduce((acc, transformer) => {\n return transformer(acc, context);\n }, config);\n return newConfig;\n}\n"]}
1
+ {"version":3,"sources":["typescript.main.runtime.ts"],"names":["TypescriptMain","constructor","logger","createCompiler","options","transformers","tsModule","ts","configMutator","TypescriptConfigMutator","transformerContext","afterMutation","runTransformersWithContext","clone","TypescriptCompiler","TypescriptAspect","id","raw","createSchemaExtractor","tsconfig","TypeScriptExtractor","getPackageJsonProps","main","types","provider","schema","loggerExt","createLogger","registerParser","TypeScriptParser","MainRuntime","SchemaAspect","LoggerAspect","addRuntime","config","context","Array","isArray","newConfig","reduce","acc","transformer"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AACA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AAEA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AACA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AAEA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AAEA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AAEA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AACA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AACA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AAaO,MAAMA,cAAN,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C,GAAG,EAFjC,EAGL8B,OAHK,EAIoB;AACzB,MAAI,CAACC,KAAK,CAACC,OAAN,CAAchC,YAAd,CAAL,EAAkC,OAAO6B,MAAP;AAClC,QAAMI,SAAS,GAAGjC,YAAY,CAACkC,MAAb,CAAoB,CAACC,GAAD,EAAMC,WAAN,KAAsB;AAC1D,WAAOA,WAAW,CAACD,GAAD,EAAML,OAAN,CAAlB;AACD,GAFiB,EAEfD,MAFe,CAAlB;AAGA,SAAOI,SAAP;AACD","sourcesContent":["import ts, { TsConfigSourceFile } from 'typescript';\nimport { MainRuntime } from '@teambit/cli';\nimport { Compiler } from '@teambit/compiler';\nimport { Logger, LoggerAspect, LoggerMain } from '@teambit/logger';\nimport { SchemaAspect, SchemaExtractor, SchemaMain } from '@teambit/schema';\nimport { PackageJsonProps } from '@teambit/pkg';\nimport { TypescriptConfigMutator } from '@teambit/typescript.modules.ts-config-mutator';\n\nimport { TypeScriptExtractor } from './typescript.extractor';\nimport { TypeScriptCompilerOptions } from './compiler-options';\nimport { TypescriptAspect } from './typescript.aspect';\nimport { TypescriptCompiler } from './typescript.compiler';\nimport { TypeScriptParser } from './typescript.parser';\n\nexport type TsMode = 'build' | 'dev';\n\nexport type TsConfigTransformContext = {\n // mode: TsMode;\n};\n\nexport type TsConfigTransformer = (\n config: TypescriptConfigMutator,\n context: TsConfigTransformContext\n) => TypescriptConfigMutator;\n\nexport class TypescriptMain {\n constructor(private logger: Logger) {}\n /**\n * create a new compiler.\n */\n createCompiler(\n options: TypeScriptCompilerOptions,\n transformers: TsConfigTransformer[] = [],\n tsModule = ts\n ): Compiler {\n const configMutator = new TypescriptConfigMutator(options);\n const transformerContext: TsConfigTransformContext = {};\n const afterMutation = runTransformersWithContext(configMutator.clone(), transformers, transformerContext);\n return new TypescriptCompiler(TypescriptAspect.id, this.logger, afterMutation.raw, tsModule);\n }\n\n /**\n * create an instance of a typescript semantic schema extractor.\n */\n createSchemaExtractor(tsconfig: TsConfigSourceFile): SchemaExtractor {\n return new TypeScriptExtractor(tsconfig);\n }\n\n /**\n * add the default package json properties to the component\n * :TODO @gilad why do we need this DSL? can't I just get the args here.\n */\n getPackageJsonProps(): PackageJsonProps {\n return {\n main: 'dist/{main}.js',\n types: '{main}.ts',\n };\n }\n\n static runtime = MainRuntime;\n static dependencies = [SchemaAspect, LoggerAspect];\n\n static async provider([schema, loggerExt]: [SchemaMain, LoggerMain]) {\n const logger = loggerExt.createLogger(TypescriptAspect.id);\n schema.registerParser(new TypeScriptParser(logger));\n\n return new TypescriptMain(logger);\n }\n}\n\nTypescriptAspect.addRuntime(TypescriptMain);\n\nexport function runTransformersWithContext(\n config: TypescriptConfigMutator,\n transformers: TsConfigTransformer[] = [],\n context: TsConfigTransformContext\n): TypescriptConfigMutator {\n if (!Array.isArray(transformers)) return config;\n const newConfig = transformers.reduce((acc, transformer) => {\n return transformer(acc, context);\n }, config);\n return newConfig;\n}\n"]}
@@ -1,7 +1,11 @@
1
- import { Export, Module, Parser } from '@teambit/schema';
1
+ import { Export, Module, Parser, StaticProperties } from '@teambit/schema';
2
+ import { Logger } from '@teambit/logger';
2
3
  import { SourceFile } from 'typescript';
3
4
  export declare class TypeScriptParser implements Parser {
5
+ private logger?;
4
6
  extension: RegExp;
5
7
  getExports(sourceFile: SourceFile): Export[];
6
8
  parseModule(modulePath: string): Module;
9
+ parseStaticProperties(sourceFile: SourceFile): Map<string, StaticProperties>;
10
+ constructor(logger?: Logger | undefined);
7
11
  }
@@ -2,6 +2,8 @@
2
2
 
3
3
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
4
 
5
+ require("core-js/modules/es.array.iterator.js");
6
+
5
7
  Object.defineProperty(exports, "__esModule", {
6
8
  value: true
7
9
  });
@@ -52,11 +54,8 @@ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "functio
52
54
  function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
53
55
 
54
56
  class TypeScriptParser {
55
- constructor() {
56
- (0, _defineProperty2().default)(this, "extension", /^.*\.(js|jsx|ts|tsx)$/);
57
- }
58
-
59
57
  getExports(sourceFile) {
58
+ const staticProperties = this.parseStaticProperties(sourceFile);
60
59
  const exports = sourceFile.statements.filter(statement => {
61
60
  if (!statement.modifiers) return false;
62
61
  return statement.modifiers.find(modifier => {
@@ -67,18 +66,20 @@ class TypeScriptParser {
67
66
  // todo refactor to a registry of variable statements.
68
67
  if ((0, _typescript().isVariableStatement)(statement)) {
69
68
  const child = statement.declarationList.declarations[0];
70
- const text = child.name.text;
71
- return new (_schema().Export)(text);
69
+ const name = child.name.text;
70
+ return new (_schema().Export)(name, staticProperties.get(name));
72
71
  }
73
72
 
74
73
  if ((0, _typescript().isFunctionDeclaration)(statement)) {
75
74
  if (!statement.name) return undefined;
76
- return new (_schema().Export)(statement.name.text);
75
+ const name = statement.name.text;
76
+ return new (_schema().Export)(name, staticProperties.get(name));
77
77
  }
78
78
 
79
79
  if ((0, _typescript().isClassDeclaration)(statement)) {
80
80
  if (!statement.name) return undefined;
81
- return new (_schema().Export)(statement.name.text);
81
+ const name = statement.name.text;
82
+ return new (_schema().Export)(name, staticProperties.get(name));
82
83
  }
83
84
 
84
85
  return undefined;
@@ -91,7 +92,50 @@ class TypeScriptParser {
91
92
  parseModule(modulePath) {
92
93
  const ast = _typescript().default.createSourceFile(modulePath, (0, _fsExtra().readFileSync)(modulePath, 'utf8'), _typescript().default.ScriptTarget.Latest);
93
94
 
94
- return new (_schema().Module)(this.getExports(ast));
95
+ const moduleExports = this.getExports(ast);
96
+ return new (_schema().Module)(moduleExports);
97
+ }
98
+
99
+ parseStaticProperties(sourceFile) {
100
+ // TODO - should we also parse staticProperties inside classes / objects?
101
+ const exportStaticProperties = new Map();
102
+ sourceFile.statements.forEach(statement => {
103
+ try {
104
+ if (!_typescript().default.isExpressionStatement(statement)) return;
105
+ if (!_typescript().default.isBinaryExpression(statement.expression)) return;
106
+ if (statement.expression.operatorToken.kind !== _typescript().default.SyntaxKind.EqualsToken) return;
107
+ if (!_typescript().default.isPropertyAccessExpression(statement.expression.left)) return;
108
+ if (!_typescript().default.isIdentifier(statement.expression.left.expression)) return;
109
+ const targetName = statement.expression.left.expression.text;
110
+ const propertyName = statement.expression.left.name.text;
111
+ if (!exportStaticProperties.has(targetName)) exportStaticProperties.set(targetName, new Map());
112
+ const existingProperties = exportStaticProperties.get(targetName);
113
+
114
+ if (_typescript().default.isStringLiteral(statement.expression.right)) {
115
+ existingProperties === null || existingProperties === void 0 ? void 0 : existingProperties.set(propertyName, statement.expression.right.text);
116
+ } else if (_typescript().default.isNumericLiteral(statement.expression.right)) {
117
+ existingProperties === null || existingProperties === void 0 ? void 0 : existingProperties.set(propertyName, +statement.expression.right.text);
118
+ } else if (statement.expression.right.kind === _typescript().default.SyntaxKind.UndefinedKeyword) {
119
+ existingProperties === null || existingProperties === void 0 ? void 0 : existingProperties.set(propertyName, undefined);
120
+ } else if (statement.expression.right.kind === _typescript().default.SyntaxKind.NullKeyword) {
121
+ existingProperties === null || existingProperties === void 0 ? void 0 : existingProperties.set(propertyName, null);
122
+ } else if (statement.expression.right.kind === _typescript().default.SyntaxKind.TrueKeyword) {
123
+ existingProperties === null || existingProperties === void 0 ? void 0 : existingProperties.set(propertyName, true);
124
+ } else if (statement.expression.right.kind === _typescript().default.SyntaxKind.FalseKeyword) {
125
+ existingProperties === null || existingProperties === void 0 ? void 0 : existingProperties.set(propertyName, false);
126
+ }
127
+ } catch (err) {
128
+ var _this$logger;
129
+
130
+ (_this$logger = this.logger) === null || _this$logger === void 0 ? void 0 : _this$logger.error('failed parsing static properties', err);
131
+ }
132
+ });
133
+ return exportStaticProperties;
134
+ }
135
+
136
+ constructor(logger) {
137
+ this.logger = logger;
138
+ (0, _defineProperty2().default)(this, "extension", /^.*\.(js|jsx|ts|tsx)$/);
95
139
  }
96
140
 
97
141
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["typescript.parser.ts"],"names":["TypeScriptParser","getExports","sourceFile","exports","statements","filter","statement","modifiers","find","modifier","kind","ts","SyntaxKind","ExportKeyword","exportModels","map","child","declarationList","declarations","text","name","Export","undefined","withoutEmpty","exportModel","parseModule","modulePath","ast","createSourceFile","ScriptTarget","Latest","Module"],"mappings":";;;;;;;;;;;;;;;;;;;AAAA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AACA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AACA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;;;;;AAQO,MAAMA,gBAAN,CAAyC;AAAA;AAAA,uDAC3B,uBAD2B;AAAA;;AAG9CC,EAAAA,UAAU,CAACC,UAAD,EAAmC;AAC3C,UAAMC,OAAO,GAAGD,UAAU,CAACE,UAAX,CAAsBC,MAAtB,CAA8BC,SAAD,IAAe;AAC1D,UAAI,CAACA,SAAS,CAACC,SAAf,EAA0B,OAAO,KAAP;AAC1B,aAAOD,SAAS,CAACC,SAAV,CAAoBC,IAApB,CAA0BC,QAAD,IAAc;AAC5C,eAAOA,QAAQ,CAACC,IAAT,KAAkBC,sBAAGC,UAAH,CAAcC,aAAvC;AACD,OAFM,CAAP;AAGD,KALe,CAAhB;AAOA,UAAMC,YAAY,GAAGX,OAAO,CAACY,GAAR,CAAaT,SAAD,IAAe;AAC9C;AACA,UAAI,uCAAoBA,SAApB,CAAJ,EAAoC;AAClC,cAAMU,KAAK,GAAIV,SAAD,CAAiCW,eAAjC,CAAiDC,YAAjD,CAA8D,CAA9D,CAAd;AACA,cAAMC,IAAI,GAAIH,KAAD,CAAeI,IAAf,CAAoBD,IAAjC;AACA,eAAO,KAAIE,gBAAJ,EAAWF,IAAX,CAAP;AACD;;AAED,UAAI,yCAAsBb,SAAtB,CAAJ,EAAsC;AACpC,YAAI,CAACA,SAAS,CAACc,IAAf,EAAqB,OAAOE,SAAP;AACrB,eAAO,KAAID,gBAAJ,EAAWf,SAAS,CAACc,IAAV,CAAeD,IAA1B,CAAP;AACD;;AAED,UAAI,sCAAmBb,SAAnB,CAAJ,EAAmC;AACjC,YAAI,CAACA,SAAS,CAACc,IAAf,EAAqB,OAAOE,SAAP;AACrB,eAAO,KAAID,gBAAJ,EAAWf,SAAS,CAACc,IAAV,CAAeD,IAA1B,CAAP;AACD;;AAED,aAAOG,SAAP;AACD,KAnBoB,CAArB;AAoBA,UAAMC,YAAY,GAAGT,YAAY,CAACT,MAAb,CAAqBmB,WAAD,IAAiBA,WAAW,KAAKF,SAArD,CAArB,CA5B2C,CA6B3C;;AACA,WAAOC,YAAP;AACD;;AAEDE,EAAAA,WAAW,CAACC,UAAD,EAAqB;AAC9B,UAAMC,GAAG,GAAGhB,sBAAGiB,gBAAH,CAAoBF,UAApB,EAAgC,6BAAaA,UAAb,EAAyB,MAAzB,CAAhC,EAAkEf,sBAAGkB,YAAH,CAAgBC,MAAlF,CAAZ;;AAEA,WAAO,KAAIC,gBAAJ,EAAW,KAAK9B,UAAL,CAAgB0B,GAAhB,CAAX,CAAP;AACD;;AAxC6C","sourcesContent":["import { Export, Module, Parser } from '@teambit/schema';\nimport { readFileSync } from 'fs-extra';\nimport ts, {\n isClassDeclaration,\n isFunctionDeclaration,\n isVariableStatement,\n SourceFile,\n VariableStatement,\n} from 'typescript';\n\nexport class TypeScriptParser implements Parser {\n public extension = /^.*\\.(js|jsx|ts|tsx)$/;\n\n getExports(sourceFile: SourceFile): Export[] {\n const exports = sourceFile.statements.filter((statement) => {\n if (!statement.modifiers) return false;\n return statement.modifiers.find((modifier) => {\n return modifier.kind === ts.SyntaxKind.ExportKeyword;\n });\n });\n\n const exportModels = exports.map((statement) => {\n // todo refactor to a registry of variable statements.\n if (isVariableStatement(statement)) {\n const child = (statement as VariableStatement).declarationList.declarations[0];\n const text = (child as any).name.text;\n return new Export(text);\n }\n\n if (isFunctionDeclaration(statement)) {\n if (!statement.name) return undefined;\n return new Export(statement.name.text);\n }\n\n if (isClassDeclaration(statement)) {\n if (!statement.name) return undefined;\n return new Export(statement.name.text);\n }\n\n return undefined;\n });\n const withoutEmpty = exportModels.filter((exportModel) => exportModel !== undefined);\n // @ts-ignore\n return withoutEmpty;\n }\n\n parseModule(modulePath: string) {\n const ast = ts.createSourceFile(modulePath, readFileSync(modulePath, 'utf8'), ts.ScriptTarget.Latest);\n\n return new Module(this.getExports(ast));\n }\n}\n"]}
1
+ {"version":3,"sources":["typescript.parser.ts"],"names":["TypeScriptParser","getExports","sourceFile","staticProperties","parseStaticProperties","exports","statements","filter","statement","modifiers","find","modifier","kind","ts","SyntaxKind","ExportKeyword","exportModels","map","child","declarationList","declarations","name","text","Export","get","undefined","withoutEmpty","exportModel","parseModule","modulePath","ast","createSourceFile","ScriptTarget","Latest","moduleExports","Module","exportStaticProperties","Map","forEach","isExpressionStatement","isBinaryExpression","expression","operatorToken","EqualsToken","isPropertyAccessExpression","left","isIdentifier","targetName","propertyName","has","set","existingProperties","isStringLiteral","right","isNumericLiteral","UndefinedKeyword","NullKeyword","TrueKeyword","FalseKeyword","err","logger","error","constructor"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AAEA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AACA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;;;;;AAQO,MAAMA,gBAAN,CAAyC;AAG9CC,EAAAA,UAAU,CAACC,UAAD,EAAmC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AAAA,kBAAkB,WAAlB,YAAAA,kBAAkB,CAAED,GAApB,CAAwBF,YAAxB,EAAsC,IAAtC;AACD,SAFM,MAEA,IAAIxC,SAAS,CAACiC,UAAV,CAAqBY,KAArB,CAA2BzC,IAA3B,KAAoCC,sBAAGC,UAAH,CAAc2C,WAAtD,EAAmE;AACxEN,UAAAA,kBAAkB,SAAlB,IAAAA,kBAAkB,WAAlB,YAAAA,kBAAkB,CAAED,GAApB,CAAwBF,YAAxB,EAAsC,IAAtC;AACD,SAFM,MAEA,IAAIxC,SAAS,CAACiC,UAAV,CAAqBY,KAArB,CAA2BzC,IAA3B,KAAoCC,sBAAGC,UAAH,CAAc4C,YAAtD,EAAoE;AACzEP,UAAAA,kBAAkB,SAAlB,IAAAA,kBAAkB,WAAlB,YAAAA,kBAAkB,CAAED,GAApB,CAAwBF,YAAxB,EAAsC,KAAtC;AACD;AACF,OA3BD,CA2BE,OAAOW,GAAP,EAAY;AAAA;;AACZ,6BAAKC,MAAL,8DAAaC,KAAb,CAAmB,kCAAnB,EAAuDF,GAAvD;AACD;AACF,KA/BD;AAiCA,WAAOvB,sBAAP;AACD;;AAED0B,EAAAA,WAAW,CAASF,MAAT,EAAsC;AAAA,SAA7BA,MAA6B,GAA7BA,MAA6B;AAAA,uDAxF9B,uBAwF8B;AAAE;;AAzFL","sourcesContent":["import { Export, Module, Parser, StaticProperties } from '@teambit/schema';\nimport { Logger } from '@teambit/logger';\nimport { readFileSync } from 'fs-extra';\nimport ts, {\n isClassDeclaration,\n isFunctionDeclaration,\n isVariableStatement,\n SourceFile,\n VariableStatement,\n} from 'typescript';\n\nexport class TypeScriptParser implements Parser {\n public extension = /^.*\\.(js|jsx|ts|tsx)$/;\n\n getExports(sourceFile: SourceFile): Export[] {\n const staticProperties = this.parseStaticProperties(sourceFile);\n\n const exports = sourceFile.statements.filter((statement) => {\n if (!statement.modifiers) return false;\n return statement.modifiers.find((modifier) => {\n return modifier.kind === ts.SyntaxKind.ExportKeyword;\n });\n });\n\n const exportModels = exports.map((statement) => {\n // todo refactor to a registry of variable statements.\n if (isVariableStatement(statement)) {\n const child = (statement as VariableStatement).declarationList.declarations[0];\n const name = (child as any).name.text;\n return new Export(name, staticProperties.get(name));\n }\n\n if (isFunctionDeclaration(statement)) {\n if (!statement.name) return undefined;\n const name = statement.name.text;\n return new Export(name, staticProperties.get(name));\n }\n\n if (isClassDeclaration(statement)) {\n if (!statement.name) return undefined;\n const name = statement.name.text;\n return new Export(name, staticProperties.get(name));\n }\n\n return undefined;\n });\n const withoutEmpty = exportModels.filter((exportModel) => exportModel !== undefined);\n // @ts-ignore\n return withoutEmpty;\n }\n\n parseModule(modulePath: string) {\n const ast = ts.createSourceFile(modulePath, readFileSync(modulePath, 'utf8'), ts.ScriptTarget.Latest);\n\n const moduleExports = this.getExports(ast);\n\n return new Module(moduleExports);\n }\n\n parseStaticProperties(sourceFile: SourceFile) {\n // TODO - should we also parse staticProperties inside classes / objects?\n\n const exportStaticProperties = new Map<string, StaticProperties>();\n\n sourceFile.statements.forEach((statement) => {\n try {\n if (!ts.isExpressionStatement(statement)) return;\n if (!ts.isBinaryExpression(statement.expression)) return;\n if (statement.expression.operatorToken.kind !== ts.SyntaxKind.EqualsToken) return;\n if (!ts.isPropertyAccessExpression(statement.expression.left)) return;\n if (!ts.isIdentifier(statement.expression.left.expression)) return;\n\n const targetName = statement.expression.left.expression.text;\n const propertyName = statement.expression.left.name.text;\n\n if (!exportStaticProperties.has(targetName)) exportStaticProperties.set(targetName, new Map());\n\n const existingProperties = exportStaticProperties.get(targetName);\n\n if (ts.isStringLiteral(statement.expression.right)) {\n existingProperties?.set(propertyName, statement.expression.right.text);\n } else if (ts.isNumericLiteral(statement.expression.right)) {\n existingProperties?.set(propertyName, +statement.expression.right.text);\n } else if (statement.expression.right.kind === ts.SyntaxKind.UndefinedKeyword) {\n existingProperties?.set(propertyName, undefined);\n } else if (statement.expression.right.kind === ts.SyntaxKind.NullKeyword) {\n existingProperties?.set(propertyName, null);\n } else if (statement.expression.right.kind === ts.SyntaxKind.TrueKeyword) {\n existingProperties?.set(propertyName, true);\n } else if (statement.expression.right.kind === ts.SyntaxKind.FalseKeyword) {\n existingProperties?.set(propertyName, false);\n }\n } catch (err) {\n this.logger?.error('failed parsing static properties', err);\n }\n });\n\n return exportStaticProperties;\n }\n\n constructor(private logger?: Logger | undefined) {}\n}\n"]}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,145 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ function _typescript() {
6
+ const data = _interopRequireDefault(require("typescript"));
7
+
8
+ _typescript = function () {
9
+ return data;
10
+ };
11
+
12
+ return data;
13
+ }
14
+
15
+ function _chai() {
16
+ const data = require("chai");
17
+
18
+ _chai = function () {
19
+ return data;
20
+ };
21
+
22
+ return data;
23
+ }
24
+
25
+ function _typescript2() {
26
+ const data = require("./typescript.parser");
27
+
28
+ _typescript2 = function () {
29
+ return data;
30
+ };
31
+
32
+ return data;
33
+ }
34
+
35
+ describe('TypescriptParser', () => {
36
+ describe('getExports', () => {
37
+ const exampleArrowFunction = `
38
+ export const arrow = () => { return 3; }
39
+ arrow.textProperty = "propertyValue";
40
+ `;
41
+ const exampleFunction = `
42
+ export function func() { return 3; }
43
+ func.textProperty = "propertyValue";
44
+ `;
45
+ const exampleClass = `
46
+ export class classy{ render() { return 3; } }
47
+ classy.textProperty = "propertyValue";
48
+ `;
49
+ it('should parse arrowFunctions', () => {
50
+ const ast = _typescript().default.createSourceFile('example.tsx', exampleArrowFunction, _typescript().default.ScriptTarget.Latest);
51
+
52
+ const exports = new (_typescript2().TypeScriptParser)().getExports(ast);
53
+ const exportArrow = exports.find(x => x.identifier === 'arrow');
54
+ (0, _chai().expect)(exportArrow).to.exist;
55
+ });
56
+ it('should parse function exports', () => {
57
+ const ast = _typescript().default.createSourceFile('example.tsx', exampleFunction, _typescript().default.ScriptTarget.Latest);
58
+
59
+ const exports = new (_typescript2().TypeScriptParser)().getExports(ast);
60
+ const exportFunction = exports.find(x => x.identifier === 'func');
61
+ (0, _chai().expect)(exportFunction).to.exist;
62
+ });
63
+ it('should parse classes', () => {
64
+ const ast = _typescript().default.createSourceFile('example.tsx', exampleClass, _typescript().default.ScriptTarget.Latest);
65
+
66
+ const exports = new (_typescript2().TypeScriptParser)().getExports(ast);
67
+ const exportClass = exports.find(x => x.identifier === 'classy');
68
+ (0, _chai().expect)(exportClass).to.exist;
69
+ });
70
+ describe('staticProperties', () => {
71
+ it('should include staticProperties, when on arrowFunctions', () => {
72
+ var _exportArrow$staticPr;
73
+
74
+ const ast = _typescript().default.createSourceFile('example.tsx', exampleArrowFunction, _typescript().default.ScriptTarget.Latest);
75
+
76
+ const exports = new (_typescript2().TypeScriptParser)().getExports(ast);
77
+ const exportArrow = exports.find(x => x.identifier === 'arrow');
78
+ (0, _chai().expect)(exportArrow === null || exportArrow === void 0 ? void 0 : (_exportArrow$staticPr = exportArrow.staticProperties) === null || _exportArrow$staticPr === void 0 ? void 0 : _exportArrow$staticPr.get('textProperty')).to.equal('propertyValue');
79
+ });
80
+ it('should include staticProperties, when on regular functions', () => {
81
+ var _exportClass$staticPr;
82
+
83
+ const ast = _typescript().default.createSourceFile('example.tsx', exampleFunction, _typescript().default.ScriptTarget.Latest);
84
+
85
+ const exports = new (_typescript2().TypeScriptParser)().getExports(ast);
86
+ const exportClass = exports.find(x => x.identifier === 'func');
87
+ (0, _chai().expect)(exportClass === null || exportClass === void 0 ? void 0 : (_exportClass$staticPr = exportClass.staticProperties) === null || _exportClass$staticPr === void 0 ? void 0 : _exportClass$staticPr.get('textProperty')).to.equal('propertyValue');
88
+ });
89
+ it('should include staticProperties, when on classes', () => {
90
+ var _exportClass$staticPr2;
91
+
92
+ const ast = _typescript().default.createSourceFile('example.tsx', exampleClass, _typescript().default.ScriptTarget.Latest);
93
+
94
+ const exports = new (_typescript2().TypeScriptParser)().getExports(ast);
95
+ const exportClass = exports.find(x => x.identifier === 'classy');
96
+ (0, _chai().expect)(exportClass === null || exportClass === void 0 ? void 0 : (_exportClass$staticPr2 = exportClass.staticProperties) === null || _exportClass$staticPr2 === void 0 ? void 0 : _exportClass$staticPr2.get('textProperty')).to.equal('propertyValue');
97
+ });
98
+ });
99
+ });
100
+ describe('collectStaticProperties', () => {
101
+ const exampleFile = `
102
+ export const hello = () => { return 3; }
103
+
104
+ hello.text = "is";
105
+ hello.count = 3;
106
+ hello.nullish = null;
107
+ hello.undef = undefined;
108
+ hello.disable = false;
109
+ hello.enable = true;
110
+ hello.complextLiteral = \`what \${hello.text} it?\`;
111
+ hello.nonAssignedProperty += 'value';
112
+ `;
113
+ it('should parse all primitive values', () => {
114
+ const ast = _typescript().default.createSourceFile('example.tsx', exampleFile, _typescript().default.ScriptTarget.Latest);
115
+
116
+ const staticProperties = new (_typescript2().TypeScriptParser)().parseStaticProperties(ast);
117
+ (0, _chai().expect)(staticProperties).to.exist;
118
+ const exportHello = staticProperties.get('hello');
119
+ (0, _chai().expect)(exportHello).to.exist;
120
+ (0, _chai().expect)(exportHello === null || exportHello === void 0 ? void 0 : exportHello.get('text')).to.equal('is');
121
+ (0, _chai().expect)(exportHello === null || exportHello === void 0 ? void 0 : exportHello.get('count')).to.equal(3);
122
+ (0, _chai().expect)(exportHello === null || exportHello === void 0 ? void 0 : exportHello.get('nullish')).to.equal(null);
123
+ (0, _chai().expect)(exportHello === null || exportHello === void 0 ? void 0 : exportHello.get('undef')).to.equal(undefined);
124
+ (0, _chai().expect)(exportHello === null || exportHello === void 0 ? void 0 : exportHello.get('disable')).to.equal(false);
125
+ (0, _chai().expect)(exportHello === null || exportHello === void 0 ? void 0 : exportHello.get('enable')).to.equal(true);
126
+ (0, _chai().expect)(exportHello === null || exportHello === void 0 ? void 0 : exportHello.has('complextLiteral')).to.be.false;
127
+ });
128
+ it('should skip non primitive values', () => {
129
+ const ast = _typescript().default.createSourceFile('example.tsx', exampleFile, _typescript().default.ScriptTarget.Latest);
130
+
131
+ const staticProperties = new (_typescript2().TypeScriptParser)().parseStaticProperties(ast);
132
+ const exportHello = staticProperties.get('hello');
133
+ (0, _chai().expect)(exportHello === null || exportHello === void 0 ? void 0 : exportHello.has('complextLiteral')).to.be.false;
134
+ });
135
+ it('should skip non assignment statements', () => {
136
+ const ast = _typescript().default.createSourceFile('example.tsx', exampleFile, _typescript().default.ScriptTarget.Latest);
137
+
138
+ const staticProperties = new (_typescript2().TypeScriptParser)().parseStaticProperties(ast);
139
+ const exportHello = staticProperties.get('hello');
140
+ (0, _chai().expect)(exportHello === null || exportHello === void 0 ? void 0 : exportHello.has('nonAssignedProperty')).to.be.false;
141
+ });
142
+ });
143
+ });
144
+
145
+ //# sourceMappingURL=typescript.parser.spec.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["typescript.parser.spec.ts"],"names":["describe","exampleArrowFunction","exampleFunction","exampleClass","it","ast","ts","createSourceFile","ScriptTarget","Latest","exports","TypeScriptParser","getExports","exportArrow","find","x","identifier","to","exist","exportFunction","exportClass","staticProperties","get","equal","exampleFile","parseStaticProperties","exportHello","undefined","has","be","false"],"mappings":";;;;AAAA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AACA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AAEA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AAEAA,QAAQ,CAAC,kBAAD,EAAqB,MAAM;AACjCA,EAAAA,QAAQ,CAAC,YAAD,EAAe,MAAM;AAC3B,UAAMC,oBAAoB,GAAI;AAClC;AACA;AACA,KAHI;AAKA,UAAMC,eAAe,GAAI;AAC7B;AACA;AACA,KAHI;AAKA,UAAMC,YAAY,GAAI;AAC1B;AACA;AACA,KAHI;AAKAC,IAAAA,EAAE,CAAC,6BAAD,EAAgC,MAAM;AACtC,YAAMC,GAAG,GAAGC,sBAAGC,gBAAH,CAAoB,aAApB,EAAmCN,oBAAnC,EAAyDK,sBAAGE,YAAH,CAAgBC,MAAzE,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B;AACA,YAAMqB,WAAW,GAAGL,gBAAgB,CAACC,GAAjB,CAAqB,OAArB,CAApB;AAEA,0BAAOI,WAAP,aAAOA,WAAP,uBAAOA,WAAW,CAAEE,GAAb,CAAiB,qBAAjB,CAAP,EAAgDX,EAAhD,CAAmDY,EAAnD,CAAsDC,KAAtD;AACD,KANC,CAAF;AAOD,GAhDO,CAAR;AAiDD,CA3HO,CAAR","sourcesContent":["import ts from 'typescript';\nimport { expect } from 'chai';\n\nimport { TypeScriptParser } from './typescript.parser';\n\ndescribe('TypescriptParser', () => {\n describe('getExports', () => {\n const exampleArrowFunction = `\n export const arrow = () => { return 3; }\n arrow.textProperty = \"propertyValue\";\n `;\n\n const exampleFunction = `\n export function func() { return 3; }\n func.textProperty = \"propertyValue\";\n `;\n\n const exampleClass = `\n export class classy{ render() { return 3; } }\n classy.textProperty = \"propertyValue\";\n `;\n\n it('should parse arrowFunctions', () => {\n const ast = ts.createSourceFile('example.tsx', exampleArrowFunction, ts.ScriptTarget.Latest);\n const exports = new TypeScriptParser().getExports(ast);\n\n const exportArrow = exports.find((x) => x.identifier === 'arrow');\n\n expect(exportArrow).to.exist;\n });\n\n it('should parse function exports', () => {\n const ast = ts.createSourceFile('example.tsx', exampleFunction, ts.ScriptTarget.Latest);\n const exports = new TypeScriptParser().getExports(ast);\n\n const exportFunction = exports.find((x) => x.identifier === 'func');\n\n expect(exportFunction).to.exist;\n });\n\n it('should parse classes', () => {\n const ast = ts.createSourceFile('example.tsx', exampleClass, ts.ScriptTarget.Latest);\n const exports = new TypeScriptParser().getExports(ast);\n\n const exportClass = exports.find((x) => x.identifier === 'classy');\n\n expect(exportClass).to.exist;\n });\n\n describe('staticProperties', () => {\n it('should include staticProperties, when on arrowFunctions', () => {\n const ast = ts.createSourceFile('example.tsx', exampleArrowFunction, ts.ScriptTarget.Latest);\n const exports = new TypeScriptParser().getExports(ast);\n\n const exportArrow = exports.find((x) => x.identifier === 'arrow');\n\n expect(exportArrow?.staticProperties?.get('textProperty')).to.equal('propertyValue');\n });\n\n it('should include staticProperties, when on regular functions', () => {\n const ast = ts.createSourceFile('example.tsx', exampleFunction, ts.ScriptTarget.Latest);\n const exports = new TypeScriptParser().getExports(ast);\n\n const exportClass = exports.find((x) => x.identifier === 'func');\n\n expect(exportClass?.staticProperties?.get('textProperty')).to.equal('propertyValue');\n });\n\n it('should include staticProperties, when on classes', () => {\n const ast = ts.createSourceFile('example.tsx', exampleClass, ts.ScriptTarget.Latest);\n const exports = new TypeScriptParser().getExports(ast);\n\n const exportClass = exports.find((x) => x.identifier === 'classy');\n\n expect(exportClass?.staticProperties?.get('textProperty')).to.equal('propertyValue');\n });\n });\n });\n\n describe('collectStaticProperties', () => {\n const exampleFile = `\n export const hello = () => { return 3; }\n\n hello.text = \"is\";\n hello.count = 3;\n hello.nullish = null;\n hello.undef = undefined;\n hello.disable = false;\n hello.enable = true;\n hello.complextLiteral = \\`what \\${hello.text} it?\\`;\n hello.nonAssignedProperty += 'value';\n `;\n\n it('should parse all primitive values', () => {\n const ast = ts.createSourceFile('example.tsx', exampleFile, ts.ScriptTarget.Latest);\n const staticProperties = new TypeScriptParser().parseStaticProperties(ast);\n\n expect(staticProperties).to.exist;\n\n const exportHello = staticProperties.get('hello');\n expect(exportHello).to.exist;\n\n expect(exportHello?.get('text')).to.equal('is');\n expect(exportHello?.get('count')).to.equal(3);\n expect(exportHello?.get('nullish')).to.equal(null);\n expect(exportHello?.get('undef')).to.equal(undefined);\n expect(exportHello?.get('disable')).to.equal(false);\n expect(exportHello?.get('enable')).to.equal(true);\n\n expect(exportHello?.has('complextLiteral')).to.be.false;\n });\n\n it('should skip non primitive values', () => {\n const ast = ts.createSourceFile('example.tsx', exampleFile, ts.ScriptTarget.Latest);\n const staticProperties = new TypeScriptParser().parseStaticProperties(ast);\n const exportHello = staticProperties.get('hello');\n\n expect(exportHello?.has('complextLiteral')).to.be.false;\n });\n\n it('should skip non assignment statements', () => {\n const ast = ts.createSourceFile('example.tsx', exampleFile, ts.ScriptTarget.Latest);\n const staticProperties = new TypeScriptParser().parseStaticProperties(ast);\n const exportHello = staticProperties.get('hello');\n\n expect(exportHello?.has('nonAssignedProperty')).to.be.false;\n });\n });\n});\n"]}
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@teambit/typescript",
3
- "version": "0.0.523",
3
+ "version": "0.0.527",
4
4
  "homepage": "https://bit.dev/teambit/typescript/typescript",
5
5
  "main": "dist/index.js",
6
6
  "componentId": {
7
7
  "scope": "teambit.typescript",
8
8
  "name": "typescript",
9
- "version": "0.0.523"
9
+ "version": "0.0.527"
10
10
  },
11
11
  "dependencies": {
12
12
  "@teambit/harmony": "0.2.11",
@@ -15,16 +15,16 @@
15
15
  "typedoc": "0.21.5",
16
16
  "@babel/runtime": "7.12.18",
17
17
  "core-js": "^3.0.0",
18
- "@teambit/compiler": "0.0.523",
19
- "@teambit/typescript.modules.ts-config-mutator": "0.0.25",
20
- "@teambit/bit-error": "0.0.365",
21
- "@teambit/builder": "0.0.523",
22
- "@teambit/isolator": "0.0.523",
23
- "@teambit/logger": "0.0.449",
24
- "@teambit/component": "0.0.523",
25
- "@teambit/schema": "0.0.523",
26
- "@teambit/cli": "0.0.379",
27
- "@teambit/pkg": "0.0.523"
18
+ "@teambit/compiler": "0.0.527",
19
+ "@teambit/typescript.modules.ts-config-mutator": "0.0.28",
20
+ "@teambit/bit-error": "0.0.366",
21
+ "@teambit/builder": "0.0.527",
22
+ "@teambit/isolator": "0.0.527",
23
+ "@teambit/logger": "0.0.453",
24
+ "@teambit/component": "0.0.527",
25
+ "@teambit/schema": "0.0.527",
26
+ "@teambit/cli": "0.0.380",
27
+ "@teambit/pkg": "0.0.527"
28
28
  },
29
29
  "devDependencies": {
30
30
  "chai": "4.3.0",
@@ -36,14 +36,14 @@
36
36
  "@types/react-dom": "^17.0.5",
37
37
  "@types/react": "^17.0.8",
38
38
  "@types/node": "12.20.4",
39
- "@teambit/typescript.aspect-docs.typescript": "0.0.85"
39
+ "@teambit/typescript.aspect-docs.typescript": "0.0.89"
40
40
  },
41
41
  "peerDependencies": {
42
42
  "@teambit/legacy": "1.0.76",
43
43
  "react-dom": "^16.8.0 || ^17.0.0",
44
44
  "react": "^16.8.0 || ^17.0.0"
45
45
  },
46
- "license": "SEE LICENSE IN LICENSE",
46
+ "license": "Apache-2.0",
47
47
  "bit": {
48
48
  "bindingPrefix": "@teambit",
49
49
  "env": {},
@@ -61,8 +61,8 @@ export class TypescriptMain {
61
61
  static dependencies = [SchemaAspect, LoggerAspect];
62
62
 
63
63
  static async provider([schema, loggerExt]: [SchemaMain, LoggerMain]) {
64
- schema.registerParser(new TypeScriptParser());
65
64
  const logger = loggerExt.createLogger(TypescriptAspect.id);
65
+ schema.registerParser(new TypeScriptParser(logger));
66
66
 
67
67
  return new TypescriptMain(logger);
68
68
  }
@@ -0,0 +1,129 @@
1
+ import ts from 'typescript';
2
+ import { expect } from 'chai';
3
+
4
+ import { TypeScriptParser } from './typescript.parser';
5
+
6
+ describe('TypescriptParser', () => {
7
+ describe('getExports', () => {
8
+ const exampleArrowFunction = `
9
+ export const arrow = () => { return 3; }
10
+ arrow.textProperty = "propertyValue";
11
+ `;
12
+
13
+ const exampleFunction = `
14
+ export function func() { return 3; }
15
+ func.textProperty = "propertyValue";
16
+ `;
17
+
18
+ const exampleClass = `
19
+ export class classy{ render() { return 3; } }
20
+ classy.textProperty = "propertyValue";
21
+ `;
22
+
23
+ it('should parse arrowFunctions', () => {
24
+ const ast = ts.createSourceFile('example.tsx', exampleArrowFunction, ts.ScriptTarget.Latest);
25
+ const exports = new TypeScriptParser().getExports(ast);
26
+
27
+ const exportArrow = exports.find((x) => x.identifier === 'arrow');
28
+
29
+ expect(exportArrow).to.exist;
30
+ });
31
+
32
+ it('should parse function exports', () => {
33
+ const ast = ts.createSourceFile('example.tsx', exampleFunction, ts.ScriptTarget.Latest);
34
+ const exports = new TypeScriptParser().getExports(ast);
35
+
36
+ const exportFunction = exports.find((x) => x.identifier === 'func');
37
+
38
+ expect(exportFunction).to.exist;
39
+ });
40
+
41
+ it('should parse classes', () => {
42
+ const ast = ts.createSourceFile('example.tsx', exampleClass, ts.ScriptTarget.Latest);
43
+ const exports = new TypeScriptParser().getExports(ast);
44
+
45
+ const exportClass = exports.find((x) => x.identifier === 'classy');
46
+
47
+ expect(exportClass).to.exist;
48
+ });
49
+
50
+ describe('staticProperties', () => {
51
+ it('should include staticProperties, when on arrowFunctions', () => {
52
+ const ast = ts.createSourceFile('example.tsx', exampleArrowFunction, ts.ScriptTarget.Latest);
53
+ const exports = new TypeScriptParser().getExports(ast);
54
+
55
+ const exportArrow = exports.find((x) => x.identifier === 'arrow');
56
+
57
+ expect(exportArrow?.staticProperties?.get('textProperty')).to.equal('propertyValue');
58
+ });
59
+
60
+ it('should include staticProperties, when on regular functions', () => {
61
+ const ast = ts.createSourceFile('example.tsx', exampleFunction, ts.ScriptTarget.Latest);
62
+ const exports = new TypeScriptParser().getExports(ast);
63
+
64
+ const exportClass = exports.find((x) => x.identifier === 'func');
65
+
66
+ expect(exportClass?.staticProperties?.get('textProperty')).to.equal('propertyValue');
67
+ });
68
+
69
+ it('should include staticProperties, when on classes', () => {
70
+ const ast = ts.createSourceFile('example.tsx', exampleClass, ts.ScriptTarget.Latest);
71
+ const exports = new TypeScriptParser().getExports(ast);
72
+
73
+ const exportClass = exports.find((x) => x.identifier === 'classy');
74
+
75
+ expect(exportClass?.staticProperties?.get('textProperty')).to.equal('propertyValue');
76
+ });
77
+ });
78
+ });
79
+
80
+ describe('collectStaticProperties', () => {
81
+ const exampleFile = `
82
+ export const hello = () => { return 3; }
83
+
84
+ hello.text = "is";
85
+ hello.count = 3;
86
+ hello.nullish = null;
87
+ hello.undef = undefined;
88
+ hello.disable = false;
89
+ hello.enable = true;
90
+ hello.complextLiteral = \`what \${hello.text} it?\`;
91
+ hello.nonAssignedProperty += 'value';
92
+ `;
93
+
94
+ it('should parse all primitive values', () => {
95
+ const ast = ts.createSourceFile('example.tsx', exampleFile, ts.ScriptTarget.Latest);
96
+ const staticProperties = new TypeScriptParser().parseStaticProperties(ast);
97
+
98
+ expect(staticProperties).to.exist;
99
+
100
+ const exportHello = staticProperties.get('hello');
101
+ expect(exportHello).to.exist;
102
+
103
+ expect(exportHello?.get('text')).to.equal('is');
104
+ expect(exportHello?.get('count')).to.equal(3);
105
+ expect(exportHello?.get('nullish')).to.equal(null);
106
+ expect(exportHello?.get('undef')).to.equal(undefined);
107
+ expect(exportHello?.get('disable')).to.equal(false);
108
+ expect(exportHello?.get('enable')).to.equal(true);
109
+
110
+ expect(exportHello?.has('complextLiteral')).to.be.false;
111
+ });
112
+
113
+ it('should skip non primitive values', () => {
114
+ const ast = ts.createSourceFile('example.tsx', exampleFile, ts.ScriptTarget.Latest);
115
+ const staticProperties = new TypeScriptParser().parseStaticProperties(ast);
116
+ const exportHello = staticProperties.get('hello');
117
+
118
+ expect(exportHello?.has('complextLiteral')).to.be.false;
119
+ });
120
+
121
+ it('should skip non assignment statements', () => {
122
+ const ast = ts.createSourceFile('example.tsx', exampleFile, ts.ScriptTarget.Latest);
123
+ const staticProperties = new TypeScriptParser().parseStaticProperties(ast);
124
+ const exportHello = staticProperties.get('hello');
125
+
126
+ expect(exportHello?.has('nonAssignedProperty')).to.be.false;
127
+ });
128
+ });
129
+ });
@@ -1,4 +1,5 @@
1
- import { Export, Module, Parser } from '@teambit/schema';
1
+ import { Export, Module, Parser, StaticProperties } from '@teambit/schema';
2
+ import { Logger } from '@teambit/logger';
2
3
  import { readFileSync } from 'fs-extra';
3
4
  import ts, {
4
5
  isClassDeclaration,
@@ -12,6 +13,8 @@ export class TypeScriptParser implements Parser {
12
13
  public extension = /^.*\.(js|jsx|ts|tsx)$/;
13
14
 
14
15
  getExports(sourceFile: SourceFile): Export[] {
16
+ const staticProperties = this.parseStaticProperties(sourceFile);
17
+
15
18
  const exports = sourceFile.statements.filter((statement) => {
16
19
  if (!statement.modifiers) return false;
17
20
  return statement.modifiers.find((modifier) => {
@@ -23,18 +26,20 @@ export class TypeScriptParser implements Parser {
23
26
  // todo refactor to a registry of variable statements.
24
27
  if (isVariableStatement(statement)) {
25
28
  const child = (statement as VariableStatement).declarationList.declarations[0];
26
- const text = (child as any).name.text;
27
- return new Export(text);
29
+ const name = (child as any).name.text;
30
+ return new Export(name, staticProperties.get(name));
28
31
  }
29
32
 
30
33
  if (isFunctionDeclaration(statement)) {
31
34
  if (!statement.name) return undefined;
32
- return new Export(statement.name.text);
35
+ const name = statement.name.text;
36
+ return new Export(name, staticProperties.get(name));
33
37
  }
34
38
 
35
39
  if (isClassDeclaration(statement)) {
36
40
  if (!statement.name) return undefined;
37
- return new Export(statement.name.text);
41
+ const name = statement.name.text;
42
+ return new Export(name, staticProperties.get(name));
38
43
  }
39
44
 
40
45
  return undefined;
@@ -47,6 +52,51 @@ export class TypeScriptParser implements Parser {
47
52
  parseModule(modulePath: string) {
48
53
  const ast = ts.createSourceFile(modulePath, readFileSync(modulePath, 'utf8'), ts.ScriptTarget.Latest);
49
54
 
50
- return new Module(this.getExports(ast));
55
+ const moduleExports = this.getExports(ast);
56
+
57
+ return new Module(moduleExports);
58
+ }
59
+
60
+ parseStaticProperties(sourceFile: SourceFile) {
61
+ // TODO - should we also parse staticProperties inside classes / objects?
62
+
63
+ const exportStaticProperties = new Map<string, StaticProperties>();
64
+
65
+ sourceFile.statements.forEach((statement) => {
66
+ try {
67
+ if (!ts.isExpressionStatement(statement)) return;
68
+ if (!ts.isBinaryExpression(statement.expression)) return;
69
+ if (statement.expression.operatorToken.kind !== ts.SyntaxKind.EqualsToken) return;
70
+ if (!ts.isPropertyAccessExpression(statement.expression.left)) return;
71
+ if (!ts.isIdentifier(statement.expression.left.expression)) return;
72
+
73
+ const targetName = statement.expression.left.expression.text;
74
+ const propertyName = statement.expression.left.name.text;
75
+
76
+ if (!exportStaticProperties.has(targetName)) exportStaticProperties.set(targetName, new Map());
77
+
78
+ const existingProperties = exportStaticProperties.get(targetName);
79
+
80
+ if (ts.isStringLiteral(statement.expression.right)) {
81
+ existingProperties?.set(propertyName, statement.expression.right.text);
82
+ } else if (ts.isNumericLiteral(statement.expression.right)) {
83
+ existingProperties?.set(propertyName, +statement.expression.right.text);
84
+ } else if (statement.expression.right.kind === ts.SyntaxKind.UndefinedKeyword) {
85
+ existingProperties?.set(propertyName, undefined);
86
+ } else if (statement.expression.right.kind === ts.SyntaxKind.NullKeyword) {
87
+ existingProperties?.set(propertyName, null);
88
+ } else if (statement.expression.right.kind === ts.SyntaxKind.TrueKeyword) {
89
+ existingProperties?.set(propertyName, true);
90
+ } else if (statement.expression.right.kind === ts.SyntaxKind.FalseKeyword) {
91
+ existingProperties?.set(propertyName, false);
92
+ }
93
+ } catch (err) {
94
+ this.logger?.error('failed parsing static properties', err);
95
+ }
96
+ });
97
+
98
+ return exportStaticProperties;
51
99
  }
100
+
101
+ constructor(private logger?: Logger | undefined) {}
52
102
  }