@teambit/typescript 0.0.1101 → 0.0.1103
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/dist/index.d.ts +1 -0
- package/dist/index.js +20 -0
- package/dist/index.js.map +1 -1
- package/dist/{preview-1688959062567.js → preview-1689131730552.js} +2 -2
- package/dist/sourceFileTransformers/class.d.ts +2 -0
- package/dist/sourceFileTransformers/class.js +38 -0
- package/dist/sourceFileTransformers/class.js.map +1 -0
- package/dist/sourceFileTransformers/empty-line-encoder.d.ts +14 -0
- package/dist/sourceFileTransformers/empty-line-encoder.js +68 -0
- package/dist/sourceFileTransformers/empty-line-encoder.js.map +1 -0
- package/dist/sourceFileTransformers/export.d.ts +2 -0
- package/dist/sourceFileTransformers/export.js +61 -0
- package/dist/sourceFileTransformers/export.js.map +1 -0
- package/dist/sourceFileTransformers/function.d.ts +2 -0
- package/dist/sourceFileTransformers/function.js +110 -0
- package/dist/sourceFileTransformers/function.js.map +1 -0
- package/dist/sourceFileTransformers/identifier.d.ts +2 -0
- package/dist/sourceFileTransformers/identifier.js +38 -0
- package/dist/sourceFileTransformers/identifier.js.map +1 -0
- package/dist/sourceFileTransformers/import.d.ts +2 -0
- package/dist/sourceFileTransformers/import.js +74 -0
- package/dist/sourceFileTransformers/import.js.map +1 -0
- package/dist/sourceFileTransformers/index.d.ts +11 -0
- package/dist/sourceFileTransformers/index.js +124 -0
- package/dist/sourceFileTransformers/index.js.map +1 -0
- package/dist/sourceFileTransformers/interface.d.ts +2 -0
- package/dist/sourceFileTransformers/interface.js +38 -0
- package/dist/sourceFileTransformers/interface.js.map +1 -0
- package/dist/sourceFileTransformers/transform.d.ts +12 -0
- package/dist/sourceFileTransformers/transform.js +113 -0
- package/dist/sourceFileTransformers/transform.js.map +1 -0
- package/dist/sourceFileTransformers/typeAlias.d.ts +2 -0
- package/dist/sourceFileTransformers/typeAlias.js +38 -0
- package/dist/sourceFileTransformers/typeAlias.js.map +1 -0
- package/dist/sourceFileTransformers/variable.d.ts +2 -0
- package/dist/sourceFileTransformers/variable.js +38 -0
- package/dist/sourceFileTransformers/variable.js.map +1 -0
- package/dist/transform-source-file.spec.d.ts +1 -0
- package/dist/transform-source-file.spec.js +109 -0
- package/dist/transform-source-file.spec.js.map +1 -0
- package/package-tar/teambit-typescript-0.0.1103.tgz +0 -0
- package/package.json +19 -19
- package/sourceFileTransformers/class.ts +29 -0
- package/sourceFileTransformers/empty-line-encoder.ts +56 -0
- package/sourceFileTransformers/export.ts +65 -0
- package/sourceFileTransformers/function.ts +164 -0
- package/sourceFileTransformers/identifier.ts +24 -0
- package/sourceFileTransformers/import.ts +103 -0
- package/sourceFileTransformers/index.ts +13 -0
- package/sourceFileTransformers/interface.ts +29 -0
- package/sourceFileTransformers/transform.ts +100 -0
- package/sourceFileTransformers/typeAlias.ts +28 -0
- package/sourceFileTransformers/variable.ts +28 -0
- package/package-tar/teambit-typescript-0.0.1101.tgz +0 -0
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
Object.defineProperty(exports, "classNamesTransformer", {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: function () {
|
|
9
|
+
return _class().classNamesTransformer;
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
Object.defineProperty(exports, "exportTransformer", {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function () {
|
|
15
|
+
return _export().exportTransformer;
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
Object.defineProperty(exports, "functionNamesTransformer", {
|
|
19
|
+
enumerable: true,
|
|
20
|
+
get: function () {
|
|
21
|
+
return _function().functionNamesTransformer;
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
Object.defineProperty(exports, "identifierTransformer", {
|
|
25
|
+
enumerable: true,
|
|
26
|
+
get: function () {
|
|
27
|
+
return _identifier().identifierTransformer;
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
Object.defineProperty(exports, "importTransformer", {
|
|
31
|
+
enumerable: true,
|
|
32
|
+
get: function () {
|
|
33
|
+
return _import().importTransformer;
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
Object.defineProperty(exports, "interfaceNamesTransformer", {
|
|
37
|
+
enumerable: true,
|
|
38
|
+
get: function () {
|
|
39
|
+
return _interface().interfaceNamesTransformer;
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
Object.defineProperty(exports, "transformSourceFile", {
|
|
43
|
+
enumerable: true,
|
|
44
|
+
get: function () {
|
|
45
|
+
return _transform().transformSourceFile;
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
Object.defineProperty(exports, "typeAliasNamesTransformer", {
|
|
49
|
+
enumerable: true,
|
|
50
|
+
get: function () {
|
|
51
|
+
return _typeAlias().typeAliasNamesTransformer;
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
Object.defineProperty(exports, "variableNamesTransformer", {
|
|
55
|
+
enumerable: true,
|
|
56
|
+
get: function () {
|
|
57
|
+
return _variable().variableNamesTransformer;
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
function _class() {
|
|
61
|
+
const data = require("./class");
|
|
62
|
+
_class = function () {
|
|
63
|
+
return data;
|
|
64
|
+
};
|
|
65
|
+
return data;
|
|
66
|
+
}
|
|
67
|
+
function _interface() {
|
|
68
|
+
const data = require("./interface");
|
|
69
|
+
_interface = function () {
|
|
70
|
+
return data;
|
|
71
|
+
};
|
|
72
|
+
return data;
|
|
73
|
+
}
|
|
74
|
+
function _variable() {
|
|
75
|
+
const data = require("./variable");
|
|
76
|
+
_variable = function () {
|
|
77
|
+
return data;
|
|
78
|
+
};
|
|
79
|
+
return data;
|
|
80
|
+
}
|
|
81
|
+
function _function() {
|
|
82
|
+
const data = require("./function");
|
|
83
|
+
_function = function () {
|
|
84
|
+
return data;
|
|
85
|
+
};
|
|
86
|
+
return data;
|
|
87
|
+
}
|
|
88
|
+
function _typeAlias() {
|
|
89
|
+
const data = require("./typeAlias");
|
|
90
|
+
_typeAlias = function () {
|
|
91
|
+
return data;
|
|
92
|
+
};
|
|
93
|
+
return data;
|
|
94
|
+
}
|
|
95
|
+
function _import() {
|
|
96
|
+
const data = require("./import");
|
|
97
|
+
_import = function () {
|
|
98
|
+
return data;
|
|
99
|
+
};
|
|
100
|
+
return data;
|
|
101
|
+
}
|
|
102
|
+
function _identifier() {
|
|
103
|
+
const data = require("./identifier");
|
|
104
|
+
_identifier = function () {
|
|
105
|
+
return data;
|
|
106
|
+
};
|
|
107
|
+
return data;
|
|
108
|
+
}
|
|
109
|
+
function _export() {
|
|
110
|
+
const data = require("./export");
|
|
111
|
+
_export = function () {
|
|
112
|
+
return data;
|
|
113
|
+
};
|
|
114
|
+
return data;
|
|
115
|
+
}
|
|
116
|
+
function _transform() {
|
|
117
|
+
const data = require("./transform");
|
|
118
|
+
_transform = function () {
|
|
119
|
+
return data;
|
|
120
|
+
};
|
|
121
|
+
return data;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":[],"sources":["index.ts"],"sourcesContent":["import ts from 'typescript';\n\nexport type SourceFileTransformer = (mapping: Record<string, string>) => ts.TransformerFactory<ts.SourceFile>;\n\nexport { classNamesTransformer } from './class';\nexport { interfaceNamesTransformer } from './interface';\nexport { variableNamesTransformer } from './variable';\nexport { functionNamesTransformer } from './function';\nexport { typeAliasNamesTransformer } from './typeAlias';\nexport { importTransformer } from './import';\nexport { identifierTransformer } from './identifier';\nexport { exportTransformer } from './export';\nexport { transformSourceFile } from './transform';\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAIA;EAAA;EAAA;IAAA;EAAA;EAAA;AAAA;AACA;EAAA;EAAA;IAAA;EAAA;EAAA;AAAA;AACA;EAAA;EAAA;IAAA;EAAA;EAAA;AAAA;AACA;EAAA;EAAA;IAAA;EAAA;EAAA;AAAA;AACA;EAAA;EAAA;IAAA;EAAA;EAAA;AAAA;AACA;EAAA;EAAA;IAAA;EAAA;EAAA;AAAA;AACA;EAAA;EAAA;IAAA;EAAA;EAAA;AAAA;AACA;EAAA;EAAA;IAAA;EAAA;EAAA;AAAA;AACA;EAAA;EAAA;IAAA;EAAA;EAAA;AAAA"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
require("core-js/modules/es.regexp.constructor.js");
|
|
5
|
+
require("core-js/modules/es.regexp.exec.js");
|
|
6
|
+
require("core-js/modules/es.string.replace.js");
|
|
7
|
+
Object.defineProperty(exports, "__esModule", {
|
|
8
|
+
value: true
|
|
9
|
+
});
|
|
10
|
+
exports.interfaceNamesTransformer = interfaceNamesTransformer;
|
|
11
|
+
function _typescript() {
|
|
12
|
+
const data = _interopRequireDefault(require("typescript"));
|
|
13
|
+
_typescript = function () {
|
|
14
|
+
return data;
|
|
15
|
+
};
|
|
16
|
+
return data;
|
|
17
|
+
}
|
|
18
|
+
function interfaceNamesTransformer(nameMapping) {
|
|
19
|
+
return context => {
|
|
20
|
+
const {
|
|
21
|
+
factory
|
|
22
|
+
} = context;
|
|
23
|
+
const visit = node => {
|
|
24
|
+
if (_typescript().default.isInterfaceDeclaration(node)) {
|
|
25
|
+
const oldName = node.name.text;
|
|
26
|
+
const newName = Object.keys(nameMapping).find(key => oldName.startsWith(key) || oldName.endsWith(key));
|
|
27
|
+
if (newName) {
|
|
28
|
+
const replacedName = oldName.startsWith(newName) ? oldName.replace(newName, nameMapping[newName]) : oldName.replace(new RegExp(`${newName}$`), nameMapping[newName]);
|
|
29
|
+
return factory.updateInterfaceDeclaration(node, node.decorators, node.modifiers, factory.createIdentifier(replacedName), node.typeParameters, node.heritageClauses, node.members);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return _typescript().default.visitEachChild(node, child => visit(child), context);
|
|
33
|
+
};
|
|
34
|
+
return node => _typescript().default.visitNode(node, visit);
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
//# sourceMappingURL=interface.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["interfaceNamesTransformer","nameMapping","context","factory","visit","node","ts","isInterfaceDeclaration","oldName","name","text","newName","Object","keys","find","key","startsWith","endsWith","replacedName","replace","RegExp","updateInterfaceDeclaration","decorators","modifiers","createIdentifier","typeParameters","heritageClauses","members","visitEachChild","child","visitNode"],"sources":["interface.ts"],"sourcesContent":["import ts from 'typescript';\n\nexport function interfaceNamesTransformer(nameMapping: Record<string, string>): ts.TransformerFactory<ts.SourceFile> {\n return (context) => {\n const { factory } = context;\n const visit: ts.Visitor = (node) => {\n if (ts.isInterfaceDeclaration(node)) {\n const oldName = node.name.text;\n const newName = Object.keys(nameMapping).find((key) => oldName.startsWith(key) || oldName.endsWith(key));\n if (newName) {\n const replacedName = oldName.startsWith(newName)\n ? oldName.replace(newName, nameMapping[newName])\n : oldName.replace(new RegExp(`${newName}$`), nameMapping[newName]);\n return factory.updateInterfaceDeclaration(\n node,\n node.decorators,\n node.modifiers,\n factory.createIdentifier(replacedName),\n node.typeParameters,\n node.heritageClauses,\n node.members\n );\n }\n }\n return ts.visitEachChild(node, (child) => visit(child), context);\n };\n return (node) => ts.visitNode(node, visit);\n };\n}\n"],"mappings":";;;;;;;;;;AAAA;EAAA;EAAA;IAAA;EAAA;EAAA;AAAA;AAEO,SAASA,yBAAyB,CAACC,WAAmC,EAAwC;EACnH,OAAQC,OAAO,IAAK;IAClB,MAAM;MAAEC;IAAQ,CAAC,GAAGD,OAAO;IAC3B,MAAME,KAAiB,GAAIC,IAAI,IAAK;MAClC,IAAIC,qBAAE,CAACC,sBAAsB,CAACF,IAAI,CAAC,EAAE;QACnC,MAAMG,OAAO,GAAGH,IAAI,CAACI,IAAI,CAACC,IAAI;QAC9B,MAAMC,OAAO,GAAGC,MAAM,CAACC,IAAI,CAACZ,WAAW,CAAC,CAACa,IAAI,CAAEC,GAAG,IAAKP,OAAO,CAACQ,UAAU,CAACD,GAAG,CAAC,IAAIP,OAAO,CAACS,QAAQ,CAACF,GAAG,CAAC,CAAC;QACxG,IAAIJ,OAAO,EAAE;UACX,MAAMO,YAAY,GAAGV,OAAO,CAACQ,UAAU,CAACL,OAAO,CAAC,GAC5CH,OAAO,CAACW,OAAO,CAACR,OAAO,EAAEV,WAAW,CAACU,OAAO,CAAC,CAAC,GAC9CH,OAAO,CAACW,OAAO,CAAC,IAAIC,MAAM,CAAE,GAAET,OAAQ,GAAE,CAAC,EAAEV,WAAW,CAACU,OAAO,CAAC,CAAC;UACpE,OAAOR,OAAO,CAACkB,0BAA0B,CACvChB,IAAI,EACJA,IAAI,CAACiB,UAAU,EACfjB,IAAI,CAACkB,SAAS,EACdpB,OAAO,CAACqB,gBAAgB,CAACN,YAAY,CAAC,EACtCb,IAAI,CAACoB,cAAc,EACnBpB,IAAI,CAACqB,eAAe,EACpBrB,IAAI,CAACsB,OAAO,CACb;QACH;MACF;MACA,OAAOrB,qBAAE,CAACsB,cAAc,CAACvB,IAAI,EAAGwB,KAAK,IAAKzB,KAAK,CAACyB,KAAK,CAAC,EAAE3B,OAAO,CAAC;IAClE,CAAC;IACD,OAAQG,IAAI,IAAKC,qBAAE,CAACwB,SAAS,CAACzB,IAAI,EAAED,KAAK,CAAC;EAC5C,CAAC;AACH"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Formatter } from '@teambit/formatter';
|
|
2
|
+
import ts from 'typescript';
|
|
3
|
+
/**
|
|
4
|
+
* Transforms a TypeScript source file using the provided transformer.
|
|
5
|
+
*
|
|
6
|
+
* @param sourceFilePath Path to the TypeScript source file.
|
|
7
|
+
* @param sourceFileContent The content of the source file.
|
|
8
|
+
* @param transformers The transformers to be applied on the source file.
|
|
9
|
+
* @param formatter (Optional) An optional formatter to format the transformed code. If no formatter is provided, the function returns the transformed source file as a string without any formatting.
|
|
10
|
+
* @returns A promise that resolves to the transformed source file as a string.
|
|
11
|
+
*/
|
|
12
|
+
export declare function transformSourceFile(sourceFilePath: string, sourceFileContent: string, transformers: ts.TransformerFactory<ts.SourceFile>[], formatter?: Formatter, updates?: Record<string, string>): Promise<string>;
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
require("core-js/modules/es.array.iterator.js");
|
|
5
|
+
require("core-js/modules/es.promise.js");
|
|
6
|
+
require("core-js/modules/es.regexp.constructor.js");
|
|
7
|
+
require("core-js/modules/es.regexp.exec.js");
|
|
8
|
+
require("core-js/modules/es.string.replace.js");
|
|
9
|
+
Object.defineProperty(exports, "__esModule", {
|
|
10
|
+
value: true
|
|
11
|
+
});
|
|
12
|
+
exports.transformSourceFile = transformSourceFile;
|
|
13
|
+
function _typescript() {
|
|
14
|
+
const data = _interopRequireDefault(require("typescript"));
|
|
15
|
+
_typescript = function () {
|
|
16
|
+
return data;
|
|
17
|
+
};
|
|
18
|
+
return data;
|
|
19
|
+
}
|
|
20
|
+
function path() {
|
|
21
|
+
const data = _interopRequireWildcard(require("path"));
|
|
22
|
+
path = function () {
|
|
23
|
+
return data;
|
|
24
|
+
};
|
|
25
|
+
return data;
|
|
26
|
+
}
|
|
27
|
+
function _emptyLineEncoder() {
|
|
28
|
+
const data = require("./empty-line-encoder");
|
|
29
|
+
_emptyLineEncoder = function () {
|
|
30
|
+
return data;
|
|
31
|
+
};
|
|
32
|
+
return data;
|
|
33
|
+
}
|
|
34
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
35
|
+
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; }
|
|
36
|
+
/**
|
|
37
|
+
* Transforms a TypeScript source file using the provided transformer.
|
|
38
|
+
*
|
|
39
|
+
* @param sourceFilePath Path to the TypeScript source file.
|
|
40
|
+
* @param sourceFileContent The content of the source file.
|
|
41
|
+
* @param transformers The transformers to be applied on the source file.
|
|
42
|
+
* @param formatter (Optional) An optional formatter to format the transformed code. If no formatter is provided, the function returns the transformed source file as a string without any formatting.
|
|
43
|
+
* @returns A promise that resolves to the transformed source file as a string.
|
|
44
|
+
*/
|
|
45
|
+
async function transformSourceFile(sourceFilePath, sourceFileContent, transformers, formatter, updates) {
|
|
46
|
+
const ext = path().extname(sourceFilePath);
|
|
47
|
+
const compatibleExts = ['.ts', '.tsx', '.js', '.jsx'];
|
|
48
|
+
if (!compatibleExts.includes(ext)) {
|
|
49
|
+
if (!updates) return sourceFileContent;
|
|
50
|
+
let transformed = sourceFileContent;
|
|
51
|
+
Object.entries(updates).forEach(([oldStr, newStr]) => {
|
|
52
|
+
const oldStringRegex = new RegExp(oldStr, 'g');
|
|
53
|
+
transformed = transformed.replace(oldStringRegex, newStr);
|
|
54
|
+
});
|
|
55
|
+
return transformed;
|
|
56
|
+
}
|
|
57
|
+
const encoder = new (_emptyLineEncoder().EmptyLineEncoder)();
|
|
58
|
+
sourceFileContent = encoder.encode(sourceFileContent);
|
|
59
|
+
const sourceFile = _typescript().default.createSourceFile(sourceFilePath, sourceFileContent, _typescript().default.ScriptTarget.Latest, true, _typescript().default.ScriptKind.TSX);
|
|
60
|
+
const transformedResult = _typescript().default.transform(sourceFile, transformers);
|
|
61
|
+
const transformedSourceFile = transformedResult.transformed[0];
|
|
62
|
+
|
|
63
|
+
// const _identifierTransformer = identifierTransformer(updates || {});
|
|
64
|
+
|
|
65
|
+
// const transformedResultWithIdentifiers: ts.TransformationResult<ts.SourceFile> =
|
|
66
|
+
// ts.transform<ts.SourceFile>(transformedSourceFile, []);
|
|
67
|
+
// const transformedSourceFileWithIdentifiers: ts.SourceFile
|
|
68
|
+
// = transformedResultWithIdentifiers.transformed[0] as ts.SourceFile;
|
|
69
|
+
|
|
70
|
+
const printer = _typescript().default.createPrinter({
|
|
71
|
+
removeComments: false
|
|
72
|
+
});
|
|
73
|
+
let transformedSourceFileStr = printer.printFile(transformedSourceFile);
|
|
74
|
+
transformedSourceFileStr = encoder.decode(transformedSourceFileStr);
|
|
75
|
+
// Remove trailing empty line markers
|
|
76
|
+
const emptyLineComment = `\\s*\\/\\*${encoder.emptyLineMarker}\\*\\/\\s*$`;
|
|
77
|
+
const regex = new RegExp(emptyLineComment, 'g');
|
|
78
|
+
transformedSourceFileStr = transformedSourceFileStr.replace(regex, '');
|
|
79
|
+
const formattedSourceFileStr = await (formatter === null || formatter === void 0 ? void 0 : formatter.formatSnippet(transformedSourceFileStr));
|
|
80
|
+
return formattedSourceFileStr || transformedSourceFileStr;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// function createMarkingTransformer<T extends ts.Node>(innerTransformer: ts.TransformerFactory<T>)
|
|
84
|
+
// : ts.TransformerFactory<T> {
|
|
85
|
+
// return context => {
|
|
86
|
+
// const innerTransform = innerTransformer(context);
|
|
87
|
+
|
|
88
|
+
// return node => {
|
|
89
|
+
// const result = innerTransform(node);
|
|
90
|
+
|
|
91
|
+
// // Add a custom property to the node to mark it as transformed
|
|
92
|
+
// // Note: this relies on TypeScript's internal behavior and may not be stable across versions
|
|
93
|
+
// (result as any).__transformed = true;
|
|
94
|
+
|
|
95
|
+
// return result;
|
|
96
|
+
// };
|
|
97
|
+
// };
|
|
98
|
+
// }
|
|
99
|
+
|
|
100
|
+
// const defaultTransformer: ts.TransformerFactory<ts.SourceFile> = () => {
|
|
101
|
+
// const visit: ts.Visitor = node => {
|
|
102
|
+
// // If the node has been marked as transformed by a specific transformer, don't transform it
|
|
103
|
+
// if ((node as any).__transformed) {
|
|
104
|
+
// return node;
|
|
105
|
+
// }
|
|
106
|
+
|
|
107
|
+
// // Your transformation logic goes here...
|
|
108
|
+
// };
|
|
109
|
+
|
|
110
|
+
// return node => ts.visitNode(node, visit);
|
|
111
|
+
// };
|
|
112
|
+
|
|
113
|
+
//# sourceMappingURL=transform.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["transformSourceFile","sourceFilePath","sourceFileContent","transformers","formatter","updates","ext","path","extname","compatibleExts","includes","transformed","Object","entries","forEach","oldStr","newStr","oldStringRegex","RegExp","replace","encoder","EmptyLineEncoder","encode","sourceFile","ts","createSourceFile","ScriptTarget","Latest","ScriptKind","TSX","transformedResult","transform","transformedSourceFile","printer","createPrinter","removeComments","transformedSourceFileStr","printFile","decode","emptyLineComment","emptyLineMarker","regex","formattedSourceFileStr","formatSnippet"],"sources":["transform.ts"],"sourcesContent":["import { Formatter } from '@teambit/formatter';\nimport ts from 'typescript';\nimport * as path from 'path';\nimport { EmptyLineEncoder } from './empty-line-encoder';\n/**\n * Transforms a TypeScript source file using the provided transformer.\n *\n * @param sourceFilePath Path to the TypeScript source file.\n * @param sourceFileContent The content of the source file.\n * @param transformers The transformers to be applied on the source file.\n * @param formatter (Optional) An optional formatter to format the transformed code. If no formatter is provided, the function returns the transformed source file as a string without any formatting.\n * @returns A promise that resolves to the transformed source file as a string.\n */\nexport async function transformSourceFile(\n sourceFilePath: string,\n sourceFileContent: string,\n transformers: ts.TransformerFactory<ts.SourceFile>[],\n formatter?: Formatter,\n updates?: Record<string, string>\n): Promise<string> {\n const ext = path.extname(sourceFilePath);\n const compatibleExts = ['.ts', '.tsx', '.js', '.jsx'];\n if (!compatibleExts.includes(ext)) {\n if (!updates) return sourceFileContent;\n let transformed = sourceFileContent;\n Object.entries(updates).forEach(([oldStr, newStr]) => {\n const oldStringRegex = new RegExp(oldStr, 'g');\n transformed = transformed.replace(oldStringRegex, newStr);\n });\n return transformed;\n }\n\n const encoder = new EmptyLineEncoder();\n sourceFileContent = encoder.encode(sourceFileContent);\n\n const sourceFile = ts.createSourceFile(\n sourceFilePath,\n sourceFileContent,\n ts.ScriptTarget.Latest,\n true,\n ts.ScriptKind.TSX\n );\n\n const transformedResult: ts.TransformationResult<ts.SourceFile> = ts.transform<ts.SourceFile>(\n sourceFile,\n transformers\n );\n const transformedSourceFile: ts.SourceFile = transformedResult.transformed[0] as ts.SourceFile;\n\n // const _identifierTransformer = identifierTransformer(updates || {});\n\n // const transformedResultWithIdentifiers: ts.TransformationResult<ts.SourceFile> =\n // ts.transform<ts.SourceFile>(transformedSourceFile, []);\n // const transformedSourceFileWithIdentifiers: ts.SourceFile\n // = transformedResultWithIdentifiers.transformed[0] as ts.SourceFile;\n\n const printer: ts.Printer = ts.createPrinter({\n removeComments: false,\n });\n\n let transformedSourceFileStr = printer.printFile(transformedSourceFile);\n transformedSourceFileStr = encoder.decode(transformedSourceFileStr);\n // Remove trailing empty line markers\n const emptyLineComment = `\\\\s*\\\\/\\\\*${encoder.emptyLineMarker}\\\\*\\\\/\\\\s*$`;\n const regex = new RegExp(emptyLineComment, 'g');\n transformedSourceFileStr = transformedSourceFileStr.replace(regex, '');\n\n const formattedSourceFileStr = await formatter?.formatSnippet(transformedSourceFileStr);\n return formattedSourceFileStr || transformedSourceFileStr;\n}\n\n// function createMarkingTransformer<T extends ts.Node>(innerTransformer: ts.TransformerFactory<T>)\n// : ts.TransformerFactory<T> {\n// return context => {\n// const innerTransform = innerTransformer(context);\n\n// return node => {\n// const result = innerTransform(node);\n\n// // Add a custom property to the node to mark it as transformed\n// // Note: this relies on TypeScript's internal behavior and may not be stable across versions\n// (result as any).__transformed = true;\n\n// return result;\n// };\n// };\n// }\n\n// const defaultTransformer: ts.TransformerFactory<ts.SourceFile> = () => {\n// const visit: ts.Visitor = node => {\n// // If the node has been marked as transformed by a specific transformer, don't transform it\n// if ((node as any).__transformed) {\n// return node;\n// }\n\n// // Your transformation logic goes here...\n// };\n\n// return node => ts.visitNode(node, visit);\n// };\n"],"mappings":";;;;;;;;;;;;AACA;EAAA;EAAA;IAAA;EAAA;EAAA;AAAA;AACA;EAAA;EAAA;IAAA;EAAA;EAAA;AAAA;AACA;EAAA;EAAA;IAAA;EAAA;EAAA;AAAA;AAAwD;AAAA;AACxD;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,eAAeA,mBAAmB,CACvCC,cAAsB,EACtBC,iBAAyB,EACzBC,YAAoD,EACpDC,SAAqB,EACrBC,OAAgC,EACf;EACjB,MAAMC,GAAG,GAAGC,IAAI,GAACC,OAAO,CAACP,cAAc,CAAC;EACxC,MAAMQ,cAAc,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,CAAC;EACrD,IAAI,CAACA,cAAc,CAACC,QAAQ,CAACJ,GAAG,CAAC,EAAE;IACjC,IAAI,CAACD,OAAO,EAAE,OAAOH,iBAAiB;IACtC,IAAIS,WAAW,GAAGT,iBAAiB;IACnCU,MAAM,CAACC,OAAO,CAACR,OAAO,CAAC,CAACS,OAAO,CAAC,CAAC,CAACC,MAAM,EAAEC,MAAM,CAAC,KAAK;MACpD,MAAMC,cAAc,GAAG,IAAIC,MAAM,CAACH,MAAM,EAAE,GAAG,CAAC;MAC9CJ,WAAW,GAAGA,WAAW,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AACA;AACA;AACA;;AAEA;AACA;;AAEA;AACA"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
require("core-js/modules/es.regexp.constructor.js");
|
|
5
|
+
require("core-js/modules/es.regexp.exec.js");
|
|
6
|
+
require("core-js/modules/es.string.replace.js");
|
|
7
|
+
Object.defineProperty(exports, "__esModule", {
|
|
8
|
+
value: true
|
|
9
|
+
});
|
|
10
|
+
exports.typeAliasNamesTransformer = typeAliasNamesTransformer;
|
|
11
|
+
function _typescript() {
|
|
12
|
+
const data = _interopRequireDefault(require("typescript"));
|
|
13
|
+
_typescript = function () {
|
|
14
|
+
return data;
|
|
15
|
+
};
|
|
16
|
+
return data;
|
|
17
|
+
}
|
|
18
|
+
function typeAliasNamesTransformer(nameMapping) {
|
|
19
|
+
return context => {
|
|
20
|
+
const {
|
|
21
|
+
factory
|
|
22
|
+
} = context;
|
|
23
|
+
const visit = node => {
|
|
24
|
+
if (_typescript().default.isTypeAliasDeclaration(node)) {
|
|
25
|
+
const oldName = node.name.text;
|
|
26
|
+
const newName = Object.keys(nameMapping).find(key => oldName.startsWith(key) || oldName.endsWith(key));
|
|
27
|
+
if (newName) {
|
|
28
|
+
const replacedName = oldName.startsWith(newName) ? oldName.replace(newName, nameMapping[newName]) : oldName.replace(new RegExp(`${newName}$`), nameMapping[newName]);
|
|
29
|
+
return factory.updateTypeAliasDeclaration(node, node.decorators, node.modifiers, factory.createIdentifier(replacedName), node.typeParameters, node.type);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return _typescript().default.visitEachChild(node, child => visit(child), context);
|
|
33
|
+
};
|
|
34
|
+
return node => _typescript().default.visitNode(node, visit);
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
//# sourceMappingURL=typeAlias.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["typeAliasNamesTransformer","nameMapping","context","factory","visit","node","ts","isTypeAliasDeclaration","oldName","name","text","newName","Object","keys","find","key","startsWith","endsWith","replacedName","replace","RegExp","updateTypeAliasDeclaration","decorators","modifiers","createIdentifier","typeParameters","type","visitEachChild","child","visitNode"],"sources":["typeAlias.ts"],"sourcesContent":["import ts from 'typescript';\n\nexport function typeAliasNamesTransformer(nameMapping: Record<string, string>): ts.TransformerFactory<ts.SourceFile> {\n return (context) => {\n const { factory } = context;\n const visit: ts.Visitor = (node) => {\n if (ts.isTypeAliasDeclaration(node)) {\n const oldName = node.name.text;\n const newName = Object.keys(nameMapping).find((key) => oldName.startsWith(key) || oldName.endsWith(key));\n if (newName) {\n const replacedName = oldName.startsWith(newName)\n ? oldName.replace(newName, nameMapping[newName])\n : oldName.replace(new RegExp(`${newName}$`), nameMapping[newName]);\n return factory.updateTypeAliasDeclaration(\n node,\n node.decorators,\n node.modifiers,\n factory.createIdentifier(replacedName),\n node.typeParameters,\n node.type\n );\n }\n }\n return ts.visitEachChild(node, (child) => visit(child), context);\n };\n return (node) => ts.visitNode(node, visit);\n };\n}\n"],"mappings":";;;;;;;;;;AAAA;EAAA;EAAA;IAAA;EAAA;EAAA;AAAA;AAEO,SAASA,yBAAyB,CAACC,WAAmC,EAAwC;EACnH,OAAQC,OAAO,IAAK;IAClB,MAAM;MAAEC;IAAQ,CAAC,GAAGD,OAAO;IAC3B,MAAME,KAAiB,GAAIC,IAAI,IAAK;MAClC,IAAIC,qBAAE,CAACC,sBAAsB,CAACF,IAAI,CAAC,EAAE;QACnC,MAAMG,OAAO,GAAGH,IAAI,CAACI,IAAI,CAACC,IAAI;QAC9B,MAAMC,OAAO,GAAGC,MAAM,CAACC,IAAI,CAACZ,WAAW,CAAC,CAACa,IAAI,CAAEC,GAAG,IAAKP,OAAO,CAACQ,UAAU,CAACD,GAAG,CAAC,IAAIP,OAAO,CAACS,QAAQ,CAACF,GAAG,CAAC,CAAC;QACxG,IAAIJ,OAAO,EAAE;UACX,MAAMO,YAAY,GAAGV,OAAO,CAACQ,UAAU,CAACL,OAAO,CAAC,GAC5CH,OAAO,CAACW,OAAO,CAACR,OAAO,EAAEV,WAAW,CAACU,OAAO,CAAC,CAAC,GAC9CH,OAAO,CAACW,OAAO,CAAC,IAAIC,MAAM,CAAE,GAAET,OAAQ,GAAE,CAAC,EAAEV,WAAW,CAACU,OAAO,CAAC,CAAC;UACpE,OAAOR,OAAO,CAACkB,0BAA0B,CACvChB,IAAI,EACJA,IAAI,CAACiB,UAAU,EACfjB,IAAI,CAACkB,SAAS,EACdpB,OAAO,CAACqB,gBAAgB,CAACN,YAAY,CAAC,EACtCb,IAAI,CAACoB,cAAc,EACnBpB,IAAI,CAACqB,IAAI,CACV;QACH;MACF;MACA,OAAOpB,qBAAE,CAACqB,cAAc,CAACtB,IAAI,EAAGuB,KAAK,IAAKxB,KAAK,CAACwB,KAAK,CAAC,EAAE1B,OAAO,CAAC;IAClE,CAAC;IACD,OAAQG,IAAI,IAAKC,qBAAE,CAACuB,SAAS,CAACxB,IAAI,EAAED,KAAK,CAAC;EAC5C,CAAC;AACH"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
require("core-js/modules/es.regexp.constructor.js");
|
|
5
|
+
require("core-js/modules/es.regexp.exec.js");
|
|
6
|
+
require("core-js/modules/es.string.replace.js");
|
|
7
|
+
Object.defineProperty(exports, "__esModule", {
|
|
8
|
+
value: true
|
|
9
|
+
});
|
|
10
|
+
exports.variableNamesTransformer = variableNamesTransformer;
|
|
11
|
+
function _typescript() {
|
|
12
|
+
const data = _interopRequireDefault(require("typescript"));
|
|
13
|
+
_typescript = function () {
|
|
14
|
+
return data;
|
|
15
|
+
};
|
|
16
|
+
return data;
|
|
17
|
+
}
|
|
18
|
+
function variableNamesTransformer(nameMapping) {
|
|
19
|
+
return context => {
|
|
20
|
+
const {
|
|
21
|
+
factory
|
|
22
|
+
} = context;
|
|
23
|
+
const visit = node => {
|
|
24
|
+
if (_typescript().default.isVariableDeclaration(node) && node.name.kind === _typescript().default.SyntaxKind.Identifier) {
|
|
25
|
+
const oldName = node.name.text;
|
|
26
|
+
const newName = Object.keys(nameMapping).find(key => oldName.startsWith(key) || oldName.endsWith(key));
|
|
27
|
+
if (newName) {
|
|
28
|
+
const replacedName = oldName.startsWith(newName) ? oldName.replace(newName, nameMapping[newName]) : oldName.replace(new RegExp(`${newName}$`), nameMapping[newName]);
|
|
29
|
+
return factory.updateVariableDeclaration(node, factory.createIdentifier(replacedName), node.exclamationToken, node.type, node.initializer);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return _typescript().default.visitEachChild(node, child => visit(child), context);
|
|
33
|
+
};
|
|
34
|
+
return node => _typescript().default.visitNode(node, visit);
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
//# sourceMappingURL=variable.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["variableNamesTransformer","nameMapping","context","factory","visit","node","ts","isVariableDeclaration","name","kind","SyntaxKind","Identifier","oldName","text","newName","Object","keys","find","key","startsWith","endsWith","replacedName","replace","RegExp","updateVariableDeclaration","createIdentifier","exclamationToken","type","initializer","visitEachChild","child","visitNode"],"sources":["variable.ts"],"sourcesContent":["import ts from 'typescript';\n\nexport function variableNamesTransformer(nameMapping: Record<string, string>): ts.TransformerFactory<ts.SourceFile> {\n return (context) => {\n const { factory } = context;\n const visit: ts.Visitor = (node) => {\n if (ts.isVariableDeclaration(node) && node.name.kind === ts.SyntaxKind.Identifier) {\n const oldName = node.name.text;\n const newName = Object.keys(nameMapping).find((key) => oldName.startsWith(key) || oldName.endsWith(key));\n if (newName) {\n const replacedName = oldName.startsWith(newName)\n ? oldName.replace(newName, nameMapping[newName])\n : oldName.replace(new RegExp(`${newName}$`), nameMapping[newName]);\n\n return factory.updateVariableDeclaration(\n node,\n factory.createIdentifier(replacedName),\n node.exclamationToken,\n node.type,\n node.initializer\n );\n }\n }\n return ts.visitEachChild(node, (child) => visit(child), context);\n };\n return (node) => ts.visitNode(node, visit);\n };\n}\n"],"mappings":";;;;;;;;;;AAAA;EAAA;EAAA;IAAA;EAAA;EAAA;AAAA;AAEO,SAASA,wBAAwB,CAACC,WAAmC,EAAwC;EAClH,OAAQC,OAAO,IAAK;IAClB,MAAM;MAAEC;IAAQ,CAAC,GAAGD,OAAO;IAC3B,MAAME,KAAiB,GAAIC,IAAI,IAAK;MAClC,IAAIC,qBAAE,CAACC,qBAAqB,CAACF,IAAI,CAAC,IAAIA,IAAI,CAACG,IAAI,CAACC,IAAI,KAAKH,qBAAE,CAACI,UAAU,CAACC,UAAU,EAAE;QACjF,MAAMC,OAAO,GAAGP,IAAI,CAACG,IAAI,CAACK,IAAI;QAC9B,MAAMC,OAAO,GAAGC,MAAM,CAACC,IAAI,CAACf,WAAW,CAAC,CAACgB,IAAI,CAAEC,GAAG,IAAKN,OAAO,CAACO,UAAU,CAACD,GAAG,CAAC,IAAIN,OAAO,CAACQ,QAAQ,CAACF,GAAG,CAAC,CAAC;QACxG,IAAIJ,OAAO,EAAE;UACX,MAAMO,YAAY,GAAGT,OAAO,CAACO,UAAU,CAACL,OAAO,CAAC,GAC5CF,OAAO,CAACU,OAAO,CAACR,OAAO,EAAEb,WAAW,CAACa,OAAO,CAAC,CAAC,GAC9CF,OAAO,CAACU,OAAO,CAAC,IAAIC,MAAM,CAAE,GAAET,OAAQ,GAAE,CAAC,EAAEb,WAAW,CAACa,OAAO,CAAC,CAAC;UAEpE,OAAOX,OAAO,CAACqB,yBAAyB,CACtCnB,IAAI,EACJF,OAAO,CAACsB,gBAAgB,CAACJ,YAAY,CAAC,EACtChB,IAAI,CAACqB,gBAAgB,EACrBrB,IAAI,CAACsB,IAAI,EACTtB,IAAI,CAACuB,WAAW,CACjB;QACH;MACF;MACA,OAAOtB,qBAAE,CAACuB,cAAc,CAACxB,IAAI,EAAGyB,KAAK,IAAK1B,KAAK,CAAC0B,KAAK,CAAC,EAAE5B,OAAO,CAAC;IAClE,CAAC;IACD,OAAQG,IAAI,IAAKC,qBAAE,CAACyB,SAAS,CAAC1B,IAAI,EAAED,KAAK,CAAC;EAC5C,CAAC;AACH"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
require("core-js/modules/es.array.iterator.js");
|
|
5
|
+
require("core-js/modules/es.promise.js");
|
|
6
|
+
function _typescript() {
|
|
7
|
+
const data = _interopRequireDefault(require("typescript"));
|
|
8
|
+
_typescript = function () {
|
|
9
|
+
return data;
|
|
10
|
+
};
|
|
11
|
+
return data;
|
|
12
|
+
}
|
|
13
|
+
function _sourceFileTransformers() {
|
|
14
|
+
const data = require("./sourceFileTransformers");
|
|
15
|
+
_sourceFileTransformers = function () {
|
|
16
|
+
return data;
|
|
17
|
+
};
|
|
18
|
+
return data;
|
|
19
|
+
}
|
|
20
|
+
function normalizeFormatting(code) {
|
|
21
|
+
const sourceFile = _typescript().default.createSourceFile('temp.ts', code, _typescript().default.ScriptTarget.Latest, true);
|
|
22
|
+
const printer = _typescript().default.createPrinter({
|
|
23
|
+
newLine: _typescript().default.NewLineKind.LineFeed,
|
|
24
|
+
removeComments: false
|
|
25
|
+
});
|
|
26
|
+
return printer.printFile(sourceFile);
|
|
27
|
+
}
|
|
28
|
+
describe('transformSourceFile', () => {
|
|
29
|
+
const createTransformerTest = (sourceCode, nameMapping, transformer, expectedCode) => {
|
|
30
|
+
it(`should correctly transform source code with ${transformer.name}`, async () => {
|
|
31
|
+
const result = await (0, _sourceFileTransformers().transformSourceFile)('test.ts', sourceCode, [transformer(nameMapping)]);
|
|
32
|
+
const normalizedResult = normalizeFormatting(result);
|
|
33
|
+
const normalizedExpectedCode = normalizeFormatting(expectedCode);
|
|
34
|
+
expect(normalizedResult).toBe(normalizedExpectedCode);
|
|
35
|
+
});
|
|
36
|
+
};
|
|
37
|
+
const testCases = [{
|
|
38
|
+
transformer: _sourceFileTransformers().classNamesTransformer,
|
|
39
|
+
sourceCode: 'class TestClass {}',
|
|
40
|
+
nameMapping: {
|
|
41
|
+
TestClass: 'NewClassName'
|
|
42
|
+
},
|
|
43
|
+
expectedCode: 'class NewClassName {}'
|
|
44
|
+
}, {
|
|
45
|
+
transformer: _sourceFileTransformers().functionNamesTransformer,
|
|
46
|
+
sourceCode: 'function testFunction() {}',
|
|
47
|
+
nameMapping: {
|
|
48
|
+
testFunction: 'newFunctionName'
|
|
49
|
+
},
|
|
50
|
+
expectedCode: 'function newFunctionName() {}'
|
|
51
|
+
}, {
|
|
52
|
+
transformer: _sourceFileTransformers().importTransformer,
|
|
53
|
+
sourceCode: 'import { Test } from "./test";',
|
|
54
|
+
nameMapping: {
|
|
55
|
+
'./test': './newTest'
|
|
56
|
+
},
|
|
57
|
+
expectedCode: 'import { Test } from "./newTest";'
|
|
58
|
+
}, {
|
|
59
|
+
transformer: _sourceFileTransformers().interfaceNamesTransformer,
|
|
60
|
+
sourceCode: 'interface TestInterface {}',
|
|
61
|
+
nameMapping: {
|
|
62
|
+
TestInterface: 'NewInterfaceName'
|
|
63
|
+
},
|
|
64
|
+
expectedCode: 'interface NewInterfaceName {}'
|
|
65
|
+
}, {
|
|
66
|
+
transformer: _sourceFileTransformers().typeAliasNamesTransformer,
|
|
67
|
+
sourceCode: 'type TestType = string;',
|
|
68
|
+
nameMapping: {
|
|
69
|
+
TestType: 'NewTypeName'
|
|
70
|
+
},
|
|
71
|
+
expectedCode: 'type NewTypeName = string;'
|
|
72
|
+
}, {
|
|
73
|
+
transformer: _sourceFileTransformers().variableNamesTransformer,
|
|
74
|
+
sourceCode: 'let testVariable = "test";',
|
|
75
|
+
nameMapping: {
|
|
76
|
+
testVariable: 'newVariableName'
|
|
77
|
+
},
|
|
78
|
+
expectedCode: 'let newVariableName = "test";'
|
|
79
|
+
}];
|
|
80
|
+
for (const {
|
|
81
|
+
transformer,
|
|
82
|
+
sourceCode,
|
|
83
|
+
nameMapping,
|
|
84
|
+
expectedCode
|
|
85
|
+
} of testCases) {
|
|
86
|
+
createTransformerTest(sourceCode, nameMapping, transformer, expectedCode);
|
|
87
|
+
}
|
|
88
|
+
it('should return original source code if no transformations are applied', async () => {
|
|
89
|
+
const sourceCode = 'class TestClass {} function testFunction() {}';
|
|
90
|
+
const result = await (0, _sourceFileTransformers().transformSourceFile)('test.ts', sourceCode, []);
|
|
91
|
+
const normalizedResult = normalizeFormatting(result);
|
|
92
|
+
const normalizedSourceCode = normalizeFormatting(sourceCode);
|
|
93
|
+
expect(normalizedResult).toBe(normalizedSourceCode);
|
|
94
|
+
});
|
|
95
|
+
it('should handle multiple transformers', async () => {
|
|
96
|
+
const sourceCode = 'class TestClass {} function testFunction() {}';
|
|
97
|
+
const expectedCode = 'class NewClassName {} function newFunctionName() {}';
|
|
98
|
+
const result = await (0, _sourceFileTransformers().transformSourceFile)('test.ts', sourceCode, [(0, _sourceFileTransformers().classNamesTransformer)({
|
|
99
|
+
TestClass: 'NewClassName'
|
|
100
|
+
}), (0, _sourceFileTransformers().functionNamesTransformer)({
|
|
101
|
+
testFunction: 'newFunctionName'
|
|
102
|
+
})]);
|
|
103
|
+
const normalizedResult = normalizeFormatting(result);
|
|
104
|
+
const normalizedExpectedCode = normalizeFormatting(expectedCode);
|
|
105
|
+
expect(normalizedResult).toBe(normalizedExpectedCode);
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
//# sourceMappingURL=transform-source-file.spec.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["normalizeFormatting","code","sourceFile","ts","createSourceFile","ScriptTarget","Latest","printer","createPrinter","newLine","NewLineKind","LineFeed","removeComments","printFile","describe","createTransformerTest","sourceCode","nameMapping","transformer","expectedCode","it","name","result","transformSourceFile","normalizedResult","normalizedExpectedCode","expect","toBe","testCases","classNamesTransformer","TestClass","functionNamesTransformer","testFunction","importTransformer","interfaceNamesTransformer","TestInterface","typeAliasNamesTransformer","TestType","variableNamesTransformer","testVariable","normalizedSourceCode"],"sources":["transform-source-file.spec.ts"],"sourcesContent":["import ts from 'typescript';\nimport {\n transformSourceFile,\n classNamesTransformer,\n functionNamesTransformer,\n importTransformer,\n interfaceNamesTransformer,\n typeAliasNamesTransformer,\n variableNamesTransformer,\n} from './sourceFileTransformers';\n\nfunction normalizeFormatting(code: string): string {\n const sourceFile = ts.createSourceFile('temp.ts', code, ts.ScriptTarget.Latest, true);\n const printer = ts.createPrinter({\n newLine: ts.NewLineKind.LineFeed,\n removeComments: false,\n });\n return printer.printFile(sourceFile);\n}\n\ndescribe('transformSourceFile', () => {\n const createTransformerTest = (\n sourceCode: string,\n nameMapping: any,\n transformer: (nameMapping: any) => ts.TransformerFactory<ts.SourceFile>,\n expectedCode: string\n ) => {\n it(`should correctly transform source code with ${transformer.name}`, async () => {\n const result = await transformSourceFile('test.ts', sourceCode, [transformer(nameMapping)]);\n const normalizedResult = normalizeFormatting(result);\n const normalizedExpectedCode = normalizeFormatting(expectedCode);\n\n expect(normalizedResult).toBe(normalizedExpectedCode);\n });\n };\n\n const testCases = [\n {\n transformer: classNamesTransformer,\n sourceCode: 'class TestClass {}',\n nameMapping: { TestClass: 'NewClassName' },\n expectedCode: 'class NewClassName {}',\n },\n {\n transformer: functionNamesTransformer,\n sourceCode: 'function testFunction() {}',\n nameMapping: { testFunction: 'newFunctionName' },\n expectedCode: 'function newFunctionName() {}',\n },\n {\n transformer: importTransformer,\n sourceCode: 'import { Test } from \"./test\";',\n nameMapping: { './test': './newTest' },\n expectedCode: 'import { Test } from \"./newTest\";',\n },\n {\n transformer: interfaceNamesTransformer,\n sourceCode: 'interface TestInterface {}',\n nameMapping: { TestInterface: 'NewInterfaceName' },\n expectedCode: 'interface NewInterfaceName {}',\n },\n {\n transformer: typeAliasNamesTransformer,\n sourceCode: 'type TestType = string;',\n nameMapping: { TestType: 'NewTypeName' },\n expectedCode: 'type NewTypeName = string;',\n },\n {\n transformer: variableNamesTransformer,\n sourceCode: 'let testVariable = \"test\";',\n nameMapping: { testVariable: 'newVariableName' },\n expectedCode: 'let newVariableName = \"test\";',\n },\n ];\n\n for (const { transformer, sourceCode, nameMapping, expectedCode } of testCases) {\n createTransformerTest(sourceCode, nameMapping, transformer, expectedCode);\n }\n\n it('should return original source code if no transformations are applied', async () => {\n const sourceCode = 'class TestClass {} function testFunction() {}';\n const result = await transformSourceFile('test.ts', sourceCode, []);\n const normalizedResult = normalizeFormatting(result);\n const normalizedSourceCode = normalizeFormatting(sourceCode);\n expect(normalizedResult).toBe(normalizedSourceCode);\n });\n\n it('should handle multiple transformers', async () => {\n const sourceCode = 'class TestClass {} function testFunction() {}';\n const expectedCode = 'class NewClassName {} function newFunctionName() {}';\n const result = await transformSourceFile('test.ts', sourceCode, [\n classNamesTransformer({ TestClass: 'NewClassName' }),\n functionNamesTransformer({ testFunction: 'newFunctionName' }),\n ]);\n const normalizedResult = normalizeFormatting(result);\n const normalizedExpectedCode = normalizeFormatting(expectedCode);\n expect(normalizedResult).toBe(normalizedExpectedCode);\n });\n});\n"],"mappings":";;;;;AAAA;EAAA;EAAA;IAAA;EAAA;EAAA;AAAA;AACA;EAAA;EAAA;IAAA;EAAA;EAAA;AAAA;AAUA,SAASA,mBAAmB,CAACC,IAAY,EAAU;EACjD,MAAMC,UAAU,GAAGC,qBAAE,CAACC,gBAAgB,CAAC,SAAS,EAAEH,IAAI,EAAEE,qBAAE,CAACE,YAAY,CAACC,MAAM,EAAE,IAAI,CAAC;EACrF,MAAMC,OAAO,GAAGJ,qBAAE,CAACK,aAAa,CAAC;IAC/BC,OAAO,EAAEN,qBAAE,CAACO,WAAW,CAACC,QAAQ;IAChCC,cAAc,EAAE;EAClB,CAAC,CAAC;EACF,OAA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AAC;IAChEO,MAAM,CAACF,gBAAgB,CAAC,CAACG,IAAI,CAACF,sBAAsB,CAAC;EACvD,CAAC,CAAC;AACJ,CAAC,CAAC"}
|
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@teambit/typescript",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.1103",
|
|
4
4
|
"homepage": "https://bit.cloud/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.
|
|
9
|
+
"version": "0.0.1103"
|
|
10
10
|
},
|
|
11
11
|
"dependencies": {
|
|
12
12
|
"get-tsconfig": "4.2.0",
|
|
@@ -21,26 +21,26 @@
|
|
|
21
21
|
"core-js": "^3.0.0",
|
|
22
22
|
"@babel/runtime": "7.20.0",
|
|
23
23
|
"@teambit/harmony": "0.4.6",
|
|
24
|
-
"@teambit/compiler": "0.0.
|
|
24
|
+
"@teambit/compiler": "0.0.1103",
|
|
25
25
|
"@teambit/typescript.modules.ts-config-mutator": "0.0.76",
|
|
26
|
-
"@teambit/builder": "0.0.
|
|
27
|
-
"@teambit/isolator": "0.0.
|
|
28
|
-
"@teambit/component": "0.0.
|
|
29
|
-
"@teambit/dependency-resolver": "0.0.
|
|
30
|
-
"@teambit/formatter": "0.0.
|
|
26
|
+
"@teambit/builder": "0.0.1103",
|
|
27
|
+
"@teambit/isolator": "0.0.1103",
|
|
28
|
+
"@teambit/component": "0.0.1103",
|
|
29
|
+
"@teambit/dependency-resolver": "0.0.1103",
|
|
30
|
+
"@teambit/formatter": "0.0.654",
|
|
31
31
|
"@teambit/semantics.entities.semantic-schema": "0.0.53",
|
|
32
32
|
"@teambit/ts-server": "0.0.48",
|
|
33
|
-
"@teambit/aspect-loader": "0.0.
|
|
34
|
-
"@teambit/envs": "0.0.
|
|
35
|
-
"@teambit/logger": "0.0.
|
|
36
|
-
"@teambit/workspace-config-files": "0.0.
|
|
37
|
-
"@teambit/workspace": "0.0.
|
|
33
|
+
"@teambit/aspect-loader": "0.0.1103",
|
|
34
|
+
"@teambit/envs": "0.0.1103",
|
|
35
|
+
"@teambit/logger": "0.0.832",
|
|
36
|
+
"@teambit/workspace-config-files": "0.0.83",
|
|
37
|
+
"@teambit/workspace": "0.0.1103",
|
|
38
38
|
"@teambit/bit-error": "0.0.402",
|
|
39
|
-
"@teambit/schema": "0.0.
|
|
40
|
-
"@teambit/scope": "0.0.
|
|
41
|
-
"@teambit/cli": "0.0.
|
|
42
|
-
"@teambit/pkg": "0.0.
|
|
43
|
-
"@teambit/watcher": "0.0.
|
|
39
|
+
"@teambit/schema": "0.0.1103",
|
|
40
|
+
"@teambit/scope": "0.0.1103",
|
|
41
|
+
"@teambit/cli": "0.0.739",
|
|
42
|
+
"@teambit/pkg": "0.0.1103",
|
|
43
|
+
"@teambit/watcher": "0.0.115"
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
46
46
|
"@types/lodash": "4.14.165",
|
|
@@ -56,7 +56,7 @@
|
|
|
56
56
|
"@teambit/typescript.aspect-docs.typescript": "0.0.153"
|
|
57
57
|
},
|
|
58
58
|
"peerDependencies": {
|
|
59
|
-
"@teambit/legacy": "1.0.
|
|
59
|
+
"@teambit/legacy": "1.0.520",
|
|
60
60
|
"react": "^16.8.0 || ^17.0.0",
|
|
61
61
|
"react-dom": "^16.8.0 || ^17.0.0"
|
|
62
62
|
},
|