@jay-framework/compiler-analyze-exported-types 0.6.7 → 0.6.8
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.js +63 -41
- package/package.json +7 -6
- /package/dist/{index.d.ts → index.d.cts} +0 -0
package/dist/index.js
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const compilerShared = require("@jay-framework/compiler-shared");
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import { createRequire } from "module";
|
|
3
|
+
import path from "path";
|
|
4
|
+
import ts from "typescript";
|
|
5
|
+
import { JayUnknown, JayElementType, JayObjectType, JayElementConstructorType, JayComponentApiMember, JayComponentType, resolvePrimitiveType, JayArrayType, JAY_COMPONENT, MAKE_JAY_COMPONENT } from "@jay-framework/compiler-shared";
|
|
7
6
|
function resolveTsConfig(options) {
|
|
8
7
|
const tsConfigPath = path.resolve(process.cwd(), options.relativePath || "tsconfig.json");
|
|
9
8
|
if (!ts.sys.fileExists(tsConfigPath)) {
|
|
@@ -15,27 +14,48 @@ function resolveTsConfig(options) {
|
|
|
15
14
|
}
|
|
16
15
|
return tsConfigPath;
|
|
17
16
|
}
|
|
17
|
+
const require2 = createRequire(import.meta.url);
|
|
18
|
+
const tsModule = require2("typescript");
|
|
19
|
+
const {
|
|
20
|
+
convertCompilerOptionsFromJson,
|
|
21
|
+
createProgram,
|
|
22
|
+
isCallExpression,
|
|
23
|
+
isFunctionDeclaration,
|
|
24
|
+
isIdentifier,
|
|
25
|
+
isImportDeclaration,
|
|
26
|
+
isInterfaceDeclaration,
|
|
27
|
+
isNamedImports,
|
|
28
|
+
isPropertySignature,
|
|
29
|
+
isQualifiedName,
|
|
30
|
+
isStringLiteral,
|
|
31
|
+
isTypeAliasDeclaration,
|
|
32
|
+
isTypeReferenceNode,
|
|
33
|
+
isVariableStatement,
|
|
34
|
+
parseConfigFileTextToJson,
|
|
35
|
+
SyntaxKind
|
|
36
|
+
} = tsModule;
|
|
18
37
|
function getTypeName(typeName) {
|
|
19
|
-
if (
|
|
38
|
+
if (isIdentifier(typeName))
|
|
20
39
|
return typeName.text;
|
|
21
|
-
else if (
|
|
40
|
+
else if (isQualifiedName(typeName))
|
|
22
41
|
return `${getTypeName(typeName.left)}.${typeName.right.text}`;
|
|
42
|
+
return "";
|
|
23
43
|
}
|
|
24
44
|
function getJayType(typeNode, types) {
|
|
25
|
-
let propType =
|
|
26
|
-
if (propType ===
|
|
45
|
+
let propType = resolvePrimitiveType(typeNode.getText());
|
|
46
|
+
if (propType === JayUnknown && isTypeReferenceNode(typeNode)) {
|
|
27
47
|
const typeName = getTypeName(typeNode.typeName);
|
|
28
48
|
if (typeName === "Array" && typeNode.typeArguments.length > 0)
|
|
29
|
-
propType = new
|
|
49
|
+
propType = new JayArrayType(getJayType(typeNode.typeArguments[0], types));
|
|
30
50
|
else
|
|
31
|
-
propType = types.find((_) => _.name === getTypeName(typeNode.typeName)) ??
|
|
51
|
+
propType = types.find((_) => _.name === getTypeName(typeNode.typeName)) ?? JayUnknown;
|
|
32
52
|
}
|
|
33
53
|
return propType;
|
|
34
54
|
}
|
|
35
55
|
function getInterfaceJayType(interfaceDeclaration, types) {
|
|
36
56
|
let props = {};
|
|
37
|
-
const jayObjectType = new
|
|
38
|
-
interfaceDeclaration.members.filter((member) =>
|
|
57
|
+
const jayObjectType = new JayObjectType(interfaceDeclaration.name.text, props);
|
|
58
|
+
interfaceDeclaration.members.filter((member) => isPropertySignature(member) && isIdentifier(member.name)).forEach((member) => {
|
|
39
59
|
let property = member;
|
|
40
60
|
let name = member.name;
|
|
41
61
|
let propKey = name.text;
|
|
@@ -44,7 +64,7 @@ function getInterfaceJayType(interfaceDeclaration, types) {
|
|
|
44
64
|
return jayObjectType;
|
|
45
65
|
}
|
|
46
66
|
function getElementConstructorType(name, typeName) {
|
|
47
|
-
return new
|
|
67
|
+
return new JayElementConstructorType(name, typeName);
|
|
48
68
|
}
|
|
49
69
|
function getComponentType(tsTypeChecker, name, componentType) {
|
|
50
70
|
let properties = componentType.getProperties();
|
|
@@ -55,12 +75,12 @@ function getComponentType(tsTypeChecker, name, componentType) {
|
|
|
55
75
|
;
|
|
56
76
|
else {
|
|
57
77
|
if (type.getSymbol().getName() === "EventEmitter")
|
|
58
|
-
componentAPIs.push(new
|
|
78
|
+
componentAPIs.push(new JayComponentApiMember(property.getName(), true));
|
|
59
79
|
else
|
|
60
|
-
componentAPIs.push(new
|
|
80
|
+
componentAPIs.push(new JayComponentApiMember(property.getName(), false));
|
|
61
81
|
}
|
|
62
82
|
}
|
|
63
|
-
return new
|
|
83
|
+
return new JayComponentType(name, componentAPIs);
|
|
64
84
|
}
|
|
65
85
|
function autoAddExtension(filename) {
|
|
66
86
|
if (fs.existsSync(filename + ".ts"))
|
|
@@ -93,32 +113,32 @@ const JayComponentProperties = {
|
|
|
93
113
|
function loadTSCompilerOptions(options = {}) {
|
|
94
114
|
let tsConfigPath = resolveTsConfig(options);
|
|
95
115
|
let loadedTSConfig = fs.readFileSync(tsConfigPath, "utf8");
|
|
96
|
-
let tsConfig =
|
|
97
|
-
return
|
|
116
|
+
let tsConfig = parseConfigFileTextToJson("tsconfig.json", loadedTSConfig);
|
|
117
|
+
return convertCompilerOptionsFromJson(tsConfig.config.compilerOptions, tsConfigPath).options;
|
|
98
118
|
}
|
|
99
119
|
function isExportedStatement(statement) {
|
|
100
120
|
return Boolean(
|
|
101
121
|
statement.modifiers && statement.modifiers.find(
|
|
102
|
-
(_) => _.kind ===
|
|
122
|
+
(_) => _.kind === SyntaxKind.ExportKeyword
|
|
103
123
|
)
|
|
104
124
|
);
|
|
105
125
|
}
|
|
106
126
|
const SYMBOLS = {
|
|
107
|
-
MAKE_JAY_COMPONENT: { module:
|
|
127
|
+
MAKE_JAY_COMPONENT: { module: JAY_COMPONENT, namedImport: MAKE_JAY_COMPONENT }
|
|
108
128
|
};
|
|
109
|
-
function findImportedSymbol(
|
|
129
|
+
function findImportedSymbol(module, namedImport) {
|
|
110
130
|
return Object.values(SYMBOLS).find(
|
|
111
|
-
(importedSymbol) => importedSymbol.module ===
|
|
131
|
+
(importedSymbol) => importedSymbol.module === module && importedSymbol.namedImport == namedImport
|
|
112
132
|
);
|
|
113
133
|
}
|
|
114
134
|
function mapImportedSymbols(statements, tsTypeChecker) {
|
|
115
|
-
statements.filter(
|
|
116
|
-
if (
|
|
117
|
-
const
|
|
118
|
-
if (
|
|
135
|
+
statements.filter(isImportDeclaration).forEach((importDeclaration) => {
|
|
136
|
+
if (isStringLiteral(importDeclaration.moduleSpecifier) && importDeclaration.importClause?.namedBindings) {
|
|
137
|
+
const module = importDeclaration.moduleSpecifier.text;
|
|
138
|
+
if (isNamedImports(importDeclaration.importClause.namedBindings)) {
|
|
119
139
|
importDeclaration.importClause.namedBindings.elements.forEach((namedImport) => {
|
|
120
140
|
const name = namedImport.name.text;
|
|
121
|
-
const importedSymbol = findImportedSymbol(
|
|
141
|
+
const importedSymbol = findImportedSymbol(module, name);
|
|
122
142
|
if (importedSymbol)
|
|
123
143
|
importedSymbol.symbol = tsTypeChecker.getTypeAtLocation(namedImport).symbol;
|
|
124
144
|
});
|
|
@@ -130,15 +150,15 @@ function mapImportedSymbols(statements, tsTypeChecker) {
|
|
|
130
150
|
function analyzeExportedTypes(filename, options = {}) {
|
|
131
151
|
let compilerOptions = loadTSCompilerOptions(options);
|
|
132
152
|
filename = autoAddExtension(filename);
|
|
133
|
-
const program =
|
|
153
|
+
const program = createProgram([filename], compilerOptions);
|
|
134
154
|
const sourceFile = program.getSourceFile(filename);
|
|
135
155
|
const tsTypeChecker = program.getTypeChecker();
|
|
136
156
|
const types = [];
|
|
137
157
|
const { MAKE_JAY_COMPONENT: MAKE_JAY_COMPONENT2 } = mapImportedSymbols(sourceFile.statements, tsTypeChecker);
|
|
138
158
|
for (const statement of sourceFile.statements.filter(isExportedStatement)) {
|
|
139
|
-
if (
|
|
159
|
+
if (isInterfaceDeclaration(statement)) {
|
|
140
160
|
types.push(getInterfaceJayType(statement, types));
|
|
141
|
-
} else if (
|
|
161
|
+
} else if (isFunctionDeclaration(statement)) {
|
|
142
162
|
const functionType = tsTypeChecker.getTypeAtLocation(statement.type);
|
|
143
163
|
if (functionType.symbol || functionType.aliasSymbol) {
|
|
144
164
|
const typeName = functionType.symbol?.name;
|
|
@@ -149,14 +169,14 @@ function analyzeExportedTypes(filename, options = {}) {
|
|
|
149
169
|
} else if (isOrSubclassOf(functionType, "JayComponent")) {
|
|
150
170
|
types.push(getComponentType(tsTypeChecker, functionName, functionType));
|
|
151
171
|
} else
|
|
152
|
-
types.push(
|
|
172
|
+
types.push(JayUnknown);
|
|
153
173
|
}
|
|
154
|
-
} else if (
|
|
155
|
-
if (
|
|
156
|
-
types.push(new
|
|
157
|
-
} else if (
|
|
174
|
+
} else if (isTypeAliasDeclaration(statement)) {
|
|
175
|
+
if (isTypeReferenceNode(statement.type) && isIdentifier(statement.type.typeName) && statement.type.typeName.text === "JayElement")
|
|
176
|
+
types.push(new JayElementType(statement.name.text));
|
|
177
|
+
} else if (isVariableStatement(statement)) {
|
|
158
178
|
statement.declarationList.declarations.forEach((declaration) => {
|
|
159
|
-
if (
|
|
179
|
+
if (isCallExpression(declaration.initializer) && isIdentifier(declaration.name)) {
|
|
160
180
|
const functionType = tsTypeChecker.getTypeAtLocation(
|
|
161
181
|
declaration.initializer.expression
|
|
162
182
|
);
|
|
@@ -172,9 +192,11 @@ function analyzeExportedTypes(filename, options = {}) {
|
|
|
172
192
|
}
|
|
173
193
|
});
|
|
174
194
|
} else
|
|
175
|
-
types.push(
|
|
195
|
+
types.push(JayUnknown);
|
|
176
196
|
}
|
|
177
197
|
return types;
|
|
178
198
|
}
|
|
179
|
-
|
|
180
|
-
|
|
199
|
+
export {
|
|
200
|
+
analyzeExportedTypes,
|
|
201
|
+
resolveTsConfig
|
|
202
|
+
};
|
package/package.json
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jay-framework/compiler-analyze-exported-types",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.8",
|
|
4
4
|
"description": "",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
|
+
"type": "module",
|
|
7
8
|
"directories": {
|
|
8
9
|
"lib": "lib"
|
|
9
10
|
},
|
|
@@ -24,10 +25,10 @@
|
|
|
24
25
|
},
|
|
25
26
|
"author": "",
|
|
26
27
|
"dependencies": {
|
|
27
|
-
"@jay-framework/compiler-shared": "^0.6.
|
|
28
|
-
"@jay-framework/component": "^0.6.
|
|
29
|
-
"@jay-framework/runtime": "^0.6.
|
|
30
|
-
"@jay-framework/secure": "^0.6.
|
|
28
|
+
"@jay-framework/compiler-shared": "^0.6.8",
|
|
29
|
+
"@jay-framework/component": "^0.6.8",
|
|
30
|
+
"@jay-framework/runtime": "^0.6.8",
|
|
31
|
+
"@jay-framework/secure": "^0.6.8",
|
|
31
32
|
"@types/js-yaml": "^4.0.9",
|
|
32
33
|
"change-case": "^4.1.2",
|
|
33
34
|
"js-yaml": "^4.1.0",
|
|
@@ -37,7 +38,7 @@
|
|
|
37
38
|
},
|
|
38
39
|
"devDependencies": {
|
|
39
40
|
"@caiogondim/strip-margin": "^1.0.0",
|
|
40
|
-
"@jay-framework/dev-environment": "^0.6.
|
|
41
|
+
"@jay-framework/dev-environment": "^0.6.8",
|
|
41
42
|
"@testing-library/jest-dom": "^6.2.0",
|
|
42
43
|
"@types/js-beautify": "^1",
|
|
43
44
|
"@types/node": "^20.11.5",
|
|
File without changes
|