mirror of
https://gitee.com/openharmony/third_party_jsframework
synced 2024-11-23 06:40:59 +00:00
根据 interface_sdk-js 仓库api中的.d.ts文件自动生成mock接口 part1
Signed-off-by: buzhifeng <buzhifeng2@huawei.com>
This commit is contained in:
parent
f0e1f3c9a2
commit
887939a5b5
2
BUILD.gn
2
BUILD.gn
@ -33,6 +33,7 @@ action("gen_snapshot") {
|
||||
script = "//third_party/jsframework/js_framework_build.sh"
|
||||
|
||||
js_framework = "//third_party/jsframework/runtime"
|
||||
js_framework_mock_generate = "//third_party/jsframework/mock-generate"
|
||||
is_mac = "false"
|
||||
use_mac = host_os == "mac"
|
||||
|
||||
@ -83,6 +84,7 @@ action("gen_snapshot") {
|
||||
rebase_path("//prebuilts", root_build_dir),
|
||||
rebase_path(buildfile_native_min, root_build_dir),
|
||||
rebase_path(css_what, root_build_dir),
|
||||
rebase_path(js_framework_mock_generate, root_build_dir),
|
||||
]
|
||||
|
||||
inputs = [
|
||||
|
@ -21,8 +21,10 @@ prebuilts_path=${12}
|
||||
# copy runtime to target out, and runtime/css-what is solt link, copy it always follow symbolic links in SOURCE
|
||||
if [ "${11}" == 'true' ];then
|
||||
cp -R -L $3 $9
|
||||
cp -R ${15} $9
|
||||
else
|
||||
cp -r -L $3 $9
|
||||
cp -r ${15} $9
|
||||
fi
|
||||
|
||||
# $2 => node $4 => node_modules
|
||||
@ -55,12 +57,14 @@ if [ -d "$prebuilts_path" ]; then
|
||||
cp -r $2 $9
|
||||
cd $9
|
||||
if [ "${11}" == 'true' ];then
|
||||
./node-v12.18.4-darwin-x64/bin/node ./mock-generate/build.js &&
|
||||
./node-v12.18.4-darwin-x64/bin/node build_jsmock_system_plugin.js || exit 1 &
|
||||
./node-v12.18.4-darwin-x64/bin/node build_strip_native_min.js || exit 1 &
|
||||
# run unit test
|
||||
./node-v12.18.4-darwin-x64/bin/node node_modules/.bin/mocha -r ts-node/register test/lib.ts test/ut/**/*.ts test/ut/*.ts || exit 1 &
|
||||
wait
|
||||
else
|
||||
./node-v12.18.4-linux-x64/bin/node ./mock-generate/build.js &&
|
||||
./node-v12.18.4-linux-x64/bin/node build_jsmock_system_plugin.js || exit 1 &
|
||||
./node-v12.18.4-linux-x64/bin/node build_strip_native_min.js || exit 1 &
|
||||
# run unit test
|
||||
@ -88,3 +92,4 @@ rm -rf ./test
|
||||
rm -rf ./.eslintrc
|
||||
rm -rf ./.babelrc
|
||||
rm -rf ./package.json
|
||||
rm -rf ./mock-generate
|
||||
|
102
mock-generate/.eslintrc
Normal file
102
mock-generate/.eslintrc
Normal file
@ -0,0 +1,102 @@
|
||||
{
|
||||
"parserOptions": {
|
||||
"ecmaVersion": 6,
|
||||
"sourceType": "module"
|
||||
},
|
||||
|
||||
"env": {
|
||||
"es6": true,
|
||||
"node": true,
|
||||
"mocha": true
|
||||
},
|
||||
|
||||
"globals": {
|
||||
"ace": false,
|
||||
"aceConsole": false,
|
||||
"aceapp": false,
|
||||
"markupState": false,
|
||||
"notifyTrimMemory": false,
|
||||
"i18nPluralRules": false,
|
||||
"compileAndRunBundle": false,
|
||||
"language": false
|
||||
},
|
||||
|
||||
"extends": "eslint:recommended",
|
||||
|
||||
"parser": "@typescript-eslint/parser",
|
||||
|
||||
"rules": {
|
||||
"camelcase": "off",
|
||||
"accessor-pairs": 2,
|
||||
"arrow-spacing": 2,
|
||||
"block-spacing": 2,
|
||||
"brace-style": 2,
|
||||
"comma-dangle": 2,
|
||||
"comma-spacing": 2,
|
||||
"comma-style": 2,
|
||||
"curly": 2,
|
||||
"dot-location": [2, "property"],
|
||||
"eol-last": 2,
|
||||
"eqeqeq": 2,
|
||||
"indent": [2, 2, { "SwitchCase": 1 }],
|
||||
"key-spacing": 2,
|
||||
"keyword-spacing": 2,
|
||||
"new-cap": 2,
|
||||
"new-parens": 2,
|
||||
"no-array-constructor": 2,
|
||||
"no-caller": 2,
|
||||
"no-eval": 2,
|
||||
"no-extend-native": 2,
|
||||
"no-extra-bind": 2,
|
||||
"no-extra-parens": 2,
|
||||
"no-floating-decimal": 2,
|
||||
"no-implied-eval": 2,
|
||||
"no-iterator": 2,
|
||||
"no-label-var": 2,
|
||||
"no-labels": 2,
|
||||
"no-lone-blocks": 2,
|
||||
"no-multi-spaces": 2,
|
||||
"no-multi-str": 2,
|
||||
"no-multiple-empty-lines": [2, { "max": 1 }],
|
||||
"no-new-object": 2,
|
||||
"no-new-wrappers": 2,
|
||||
"no-octal-escape": 2,
|
||||
"no-proto": 2,
|
||||
"no-return-assign": 2,
|
||||
"no-self-compare": 2,
|
||||
"no-sequences": 2,
|
||||
"func-call-spacing": 2,
|
||||
"no-throw-literal": 2,
|
||||
"no-trailing-spaces": 2,
|
||||
"no-undef-init": "off",
|
||||
"no-unmodified-loop-condition": 2,
|
||||
"no-unneeded-ternary": [2, { "defaultAssignment": false }],
|
||||
"no-unused-vars": "off",
|
||||
"no-useless-computed-key": 2,
|
||||
"no-useless-constructor": "off",
|
||||
"no-whitespace-before-property": 2,
|
||||
"one-var": [2, { "initialized": "never" }],
|
||||
"padded-blocks": [2, "never"],
|
||||
"quotes": [2, "single", {"avoidEscape": true, "allowTemplateLiterals": true}],
|
||||
"semi": 2,
|
||||
"semi-spacing": 2,
|
||||
"space-before-blocks": 2,
|
||||
"space-before-function-paren": [2, "never"],
|
||||
"space-in-parens": 2,
|
||||
"space-infix-ops": 2,
|
||||
"space-unary-ops": [2, { "words": true, "nonwords": false }],
|
||||
"spaced-comment": 2,
|
||||
"template-curly-spacing": 2,
|
||||
"wrap-iife": [2, "any"],
|
||||
"no-var": 2,
|
||||
"prefer-const": 2,
|
||||
"array-bracket-spacing": 2,
|
||||
"no-useless-escape": 0
|
||||
},
|
||||
|
||||
"settings": {
|
||||
"flowtype": {
|
||||
"onlyFilesWithFlowAnnotation": true
|
||||
}
|
||||
}
|
||||
}
|
47
mock-generate/build.js
Normal file
47
mock-generate/build.js
Normal file
@ -0,0 +1,47 @@
|
||||
const fs = require('fs');
|
||||
const { spawn } = require('child_process');
|
||||
const path = require('path');
|
||||
const os = require('os');
|
||||
function compileMock() {
|
||||
const mockJsPath = path.join(__dirname, '..', './runtime/main/extend/systemplugin');
|
||||
let nodeDir = '';
|
||||
if (os.platform() === 'linux') {
|
||||
nodeDir = './node-v12.18.4-linux-x64/bin/node';
|
||||
} else {
|
||||
nodeDir = './node-v12.18.4-darwin-x64/bin/node';
|
||||
}
|
||||
|
||||
const bat = spawn(`${path.join(__dirname, '..', nodeDir)} ${path.join(__dirname, '..','./node_modules/typescript/bin/tsc')} &&
|
||||
${path.join(__dirname, '..', nodeDir)} ${path.join(__dirname, 'dist')}/main.js &&
|
||||
${path.join(__dirname, '..', nodeDir)} ${path.join(__dirname, '..', './node_modules/eslint/bin/eslint.js')} -c .eslintrc --fix ${mockJsPath}/**/*.js`, {
|
||||
cwd: __dirname,
|
||||
shell: true
|
||||
});
|
||||
shellOutput(bat);
|
||||
}
|
||||
|
||||
function shellOutput(bat) {
|
||||
bat.stdout.on('data', (data) => {
|
||||
const lines = `${data}`.split(/\r{0,1}\n/);
|
||||
if (lines) {
|
||||
lines.forEach(line => {
|
||||
if (line.length > 0) {
|
||||
console.log(line.toString());
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
bat.stderr.on('data', (data) => {
|
||||
const lines = `${data}`.split(/\r{0,1}\n/);
|
||||
if (lines) {
|
||||
lines.forEach(line => {
|
||||
if (line.length > 0) {
|
||||
console.log(line.toString());
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
compileMock();
|
19
mock-generate/package.json
Normal file
19
mock-generate/package.json
Normal file
@ -0,0 +1,19 @@
|
||||
{
|
||||
"name": "mock-generate",
|
||||
"version": "1.0.0",
|
||||
"description": "generate mock",
|
||||
"main": "main.js",
|
||||
"scripts": {
|
||||
"build": "npm install && tsc && cd dist && node main.js && cd .. && eslint -c .eslintrc --fix ../runtime/**/systemplugin/**/*.js"
|
||||
},
|
||||
"keywords": [
|
||||
"mock"
|
||||
],
|
||||
"license": "Apache 2.0",
|
||||
"devDependencies": {
|
||||
"@types/node": "^14.18.21",
|
||||
"typescript": "4.1.3",
|
||||
"eslint": "^7.14.0",
|
||||
"@typescript-eslint/parser": "^4.8.2"
|
||||
}
|
||||
}
|
200
mock-generate/src/common/commonUtils.ts
Normal file
200
mock-generate/src/common/commonUtils.ts
Normal file
@ -0,0 +1,200 @@
|
||||
import path from 'path';
|
||||
import {
|
||||
CallSignatureDeclaration, ComputedPropertyName, FunctionDeclaration, Identifier, isClassDeclaration,
|
||||
isComputedPropertyName, isIdentifier, isModuleBlock, isModuleDeclaration, isPrivateIdentifier, MethodDeclaration,
|
||||
MethodSignature, ModifiersArray, ModuleDeclaration, NodeArray, ParameterDeclaration, PropertyName, SourceFile
|
||||
} from 'typescript';
|
||||
|
||||
const allLegalImports = new Set<string>();
|
||||
const fileNameList = new Set<string>();
|
||||
const allClassSet = new Set<string>();
|
||||
|
||||
/**
|
||||
* get all legal imports
|
||||
* @returns
|
||||
*/
|
||||
export function getAllLegalImports(): Set<string> {
|
||||
return allLegalImports;
|
||||
}
|
||||
|
||||
/**
|
||||
* get all legal imports
|
||||
* @param element
|
||||
*/
|
||||
export function collectAllLegalImports(element: string) {
|
||||
allLegalImports.add(element);
|
||||
}
|
||||
|
||||
/**
|
||||
* collect all mock js file path
|
||||
* @returns
|
||||
*/
|
||||
export function getAllFileNameList(): Set<string> {
|
||||
return fileNameList;
|
||||
}
|
||||
|
||||
/**
|
||||
* collect all file name
|
||||
*/
|
||||
export function collectAllFileName(filePath: string) {
|
||||
const fileName = path.basename(filePath).split('.d.ts')[0];
|
||||
let outputFileName = '';
|
||||
if (fileName.includes('@')) {
|
||||
outputFileName = fileName.split('@')[1].replace(/\./g, '_');
|
||||
} else {
|
||||
outputFileName = fileName;
|
||||
}
|
||||
fileNameList.add(outputFileName);
|
||||
}
|
||||
|
||||
/**
|
||||
* get all class name set
|
||||
* @returns
|
||||
*/
|
||||
export function getClassNameSet(): Set<string> {
|
||||
return allClassSet;
|
||||
}
|
||||
|
||||
/**
|
||||
* get all class declaration
|
||||
* @param sourceFile
|
||||
* @returns
|
||||
*/
|
||||
export function getAllClassDeclaration(sourceFile: SourceFile): Set<string> {
|
||||
sourceFile.forEachChild(node => {
|
||||
if (isClassDeclaration(node)) {
|
||||
if (node.name !== undefined) {
|
||||
allClassSet.add(node.name.escapedText.toString());
|
||||
}
|
||||
} else if (isModuleDeclaration(node)) {
|
||||
const moduleDeclaration = node as ModuleDeclaration;
|
||||
const moduleBody = moduleDeclaration.body;
|
||||
if (moduleBody !== undefined && isModuleBlock(moduleBody)) {
|
||||
moduleBody.statements.forEach(value => {
|
||||
if (isClassDeclaration(value)) {
|
||||
if (value.name !== undefined) {
|
||||
allClassSet.add(firstCharacterToUppercase(value.name?.escapedText.toString()));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
return allClassSet;
|
||||
}
|
||||
|
||||
/**
|
||||
* get keywords
|
||||
* @param modifiers
|
||||
* @returns
|
||||
*/
|
||||
export function getModifiers(modifiers: ModifiersArray): Array<number> {
|
||||
const modifiersArray: Array<number> = [];
|
||||
modifiers.forEach(value => modifiersArray.push(value.kind));
|
||||
return modifiersArray;
|
||||
}
|
||||
|
||||
/**
|
||||
* get property name
|
||||
* @param node property node
|
||||
* @param sourceFile
|
||||
* @returns
|
||||
*/
|
||||
export function getPropertyName(node: PropertyName, sourceFile: SourceFile): string {
|
||||
let propertyName = '';
|
||||
if (isIdentifier(node) || isPrivateIdentifier(node)) {
|
||||
const newNameNode = node as Identifier;
|
||||
propertyName = newNameNode.escapedText.toString();
|
||||
} else if (isComputedPropertyName(node)) {
|
||||
const newNameNode = node as ComputedPropertyName;
|
||||
propertyName = sourceFile.text.substring(newNameNode.expression.pos, newNameNode.expression.end).trimStart().trimEnd();
|
||||
} else {
|
||||
propertyName = sourceFile.text.substring(node.pos, node.end).trimStart().trimEnd();
|
||||
}
|
||||
return propertyName;
|
||||
}
|
||||
|
||||
/**
|
||||
* get parameter declaration
|
||||
* @param parameter
|
||||
* @param sourceFile
|
||||
* @returns
|
||||
*/
|
||||
export function getParameter(parameter: ParameterDeclaration, sourceFile: SourceFile): ParameterEntity {
|
||||
let paramName = '';
|
||||
let paramTypeString = '';
|
||||
const paramTypeKind = parameter.type?.kind === undefined ? -1 : parameter.type.kind;
|
||||
if (isIdentifier(parameter.name)) {
|
||||
paramName = parameter.name.escapedText === undefined ? '' : parameter.name.escapedText.toString();
|
||||
} else {
|
||||
const start = parameter.name.pos === undefined ? 0 : parameter.name.pos;
|
||||
const end = parameter.name.end === undefined ? 0 : parameter.name.end;
|
||||
paramName = sourceFile.text.substring(start, end).trimStart().trimEnd();
|
||||
}
|
||||
|
||||
const start = parameter.type?.pos === undefined ? 0 : parameter.type.pos;
|
||||
const end = parameter.type?.end === undefined ? 0 : parameter.type.end;
|
||||
paramTypeString = sourceFile.text.substring(start, end).trimStart().trimEnd();
|
||||
return {
|
||||
paramName: paramName,
|
||||
paramTypeString: paramTypeString,
|
||||
paramTypeKind: paramTypeKind
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* get method or function return info
|
||||
* @param node
|
||||
* @param sourceFile
|
||||
* @returns
|
||||
*/
|
||||
export function getFunctionAndMethodReturnInfo(node: FunctionDeclaration | MethodDeclaration |
|
||||
MethodSignature | CallSignatureDeclaration, sourceFile: SourceFile): ReturnTypeEntity {
|
||||
const returnInfo = { returnKindName: '', returnKind: -1 };
|
||||
if (node.type !== undefined) {
|
||||
const start = node.type.pos === undefined ? 0 : node.type.pos;
|
||||
const end = node.type.end === undefined ? 0 : node.type.end;
|
||||
returnInfo.returnKindName = sourceFile.text.substring(start, end).trimStart().trimEnd();
|
||||
returnInfo.returnKind = node.type.kind;
|
||||
}
|
||||
return returnInfo;
|
||||
}
|
||||
|
||||
/**
|
||||
* get export modifiers
|
||||
* @param modifiers
|
||||
* @returns
|
||||
*/
|
||||
export function getExportKeyword(modifiers: ModifiersArray): Array<number> {
|
||||
const modifiersArray: Array<number> = [];
|
||||
modifiers.forEach(value => {
|
||||
modifiersArray.push(value.kind);
|
||||
});
|
||||
return modifiersArray;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param str first letter capitalization
|
||||
* @returns
|
||||
*/
|
||||
export function firstCharacterToUppercase(str: string): string {
|
||||
return str.slice(0, 1).toUpperCase() + str.slice(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* parameters entity
|
||||
*/
|
||||
export interface ParameterEntity {
|
||||
paramName: string,
|
||||
paramTypeString: string,
|
||||
paramTypeKind: number
|
||||
}
|
||||
|
||||
/**
|
||||
* return type entity
|
||||
*/
|
||||
export interface ReturnTypeEntity {
|
||||
returnKindName: string,
|
||||
returnKind: number
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
import { CallSignatureDeclaration, Node, SourceFile } from 'typescript';
|
||||
import {
|
||||
getFunctionAndMethodReturnInfo, getParameter, getPropertyName,
|
||||
ParameterEntity, ReturnTypeEntity
|
||||
} from '../common/commonUtils';
|
||||
|
||||
/**
|
||||
* get nameless function info
|
||||
* @param node
|
||||
* @param sourceFile
|
||||
* @returns
|
||||
*/
|
||||
export function getCallSignatureDeclaration(node: Node, sourceFile: SourceFile): CallSignatureEntity {
|
||||
const callSignatureNode = node as CallSignatureDeclaration;
|
||||
let functionName = '';
|
||||
const args: Array<ParameterEntity> = [];
|
||||
const returnType = getFunctionAndMethodReturnInfo(callSignatureNode, sourceFile);
|
||||
if (callSignatureNode.name !== undefined) {
|
||||
functionName = getPropertyName(callSignatureNode.name, sourceFile);
|
||||
}
|
||||
|
||||
callSignatureNode.parameters.forEach(value => {
|
||||
args.push(getParameter(value, sourceFile));
|
||||
});
|
||||
|
||||
return {
|
||||
functionName: functionName,
|
||||
returnType: returnType,
|
||||
args: args
|
||||
};
|
||||
}
|
||||
|
||||
export interface CallSignatureEntity {
|
||||
functionName: string,
|
||||
returnType: ReturnTypeEntity,
|
||||
args: Array<ParameterEntity>
|
||||
}
|
88
mock-generate/src/declaration-node/classDeclaration.ts
Normal file
88
mock-generate/src/declaration-node/classDeclaration.ts
Normal file
@ -0,0 +1,88 @@
|
||||
import {
|
||||
ClassDeclaration, isConstructorDeclaration,
|
||||
isMethodDeclaration, isPropertyDeclaration,
|
||||
isTypeParameterDeclaration, SourceFile, SyntaxKind
|
||||
} from 'typescript';
|
||||
import { getExportKeyword } from '../common/commonUtils';
|
||||
import { ConstructorEntity, getConstructorDeclaration } from './constructorDeclaration';
|
||||
import { getHeritageClauseDeclaration, HeritageClauseEntity } from './heritageClauseDeclaration';
|
||||
import { getMethodDeclaration, MethodEntity, StaticMethodEntity } from './methodDeclaration';
|
||||
import { getPropertyDeclaration, PropertyEntity } from './propertyDeclaration';
|
||||
import { getTypeParameterDeclaration, TypeParameterEntity } from './typeParameterDeclaration';
|
||||
|
||||
/**
|
||||
* get class info
|
||||
* @param classNode
|
||||
* @param sourceFile
|
||||
* @returns
|
||||
*/
|
||||
export function getClassDeclaration(classNode: ClassDeclaration, sourceFile: SourceFile): ClassEntity {
|
||||
let exportModifiers: Array<number> = [];
|
||||
if (classNode.modifiers !== undefined) {
|
||||
exportModifiers = getExportKeyword(classNode.modifiers);
|
||||
}
|
||||
|
||||
const className = classNode.name === undefined ? '' : classNode.name.escapedText.toString();
|
||||
const heritageClauses: Array<HeritageClauseEntity> = [];
|
||||
const classConstructor: Array<Array<ConstructorEntity>> = [];
|
||||
const classMethod: Map<string, Array<MethodEntity>> = new Map<string, Array<MethodEntity>>();
|
||||
const classProperty: Array<PropertyEntity> = [];
|
||||
const typeParameters: Array<TypeParameterEntity> = [];
|
||||
const staticMethods: Array<StaticMethodEntity> = [];
|
||||
|
||||
if (classNode.heritageClauses !== undefined) {
|
||||
classNode.heritageClauses.forEach(value => {
|
||||
heritageClauses.push(getHeritageClauseDeclaration(value, sourceFile));
|
||||
});
|
||||
}
|
||||
|
||||
classNode.members.forEach(value => {
|
||||
if (isMethodDeclaration(value)) {
|
||||
const methodEntity = getMethodDeclaration(value, sourceFile);
|
||||
if (methodEntity.modifiers.includes(SyntaxKind.StaticKeyword)) {
|
||||
staticMethods.push({ className: className, methodEntity: methodEntity });
|
||||
} else {
|
||||
if (classMethod.get(methodEntity.functionName.name) !== undefined) {
|
||||
classMethod.get(methodEntity.functionName.name)?.push(methodEntity);
|
||||
} else {
|
||||
const methodArray: Array<MethodEntity> = [];
|
||||
methodArray.push(methodEntity);
|
||||
classMethod.set(methodEntity.functionName.name, methodArray);
|
||||
}
|
||||
}
|
||||
} else if (isPropertyDeclaration(value)) {
|
||||
classProperty.push(getPropertyDeclaration(value, sourceFile));
|
||||
} else if (isConstructorDeclaration(value)) {
|
||||
classConstructor.push(getConstructorDeclaration(value, sourceFile));
|
||||
} else if (isTypeParameterDeclaration(value)) {
|
||||
typeParameters.push(getTypeParameterDeclaration(value, sourceFile));
|
||||
} else {
|
||||
console.log('--------------------------- uncaught class type start -----------------------');
|
||||
console.log('className: ' + className);
|
||||
console.log(value);
|
||||
console.log('--------------------------- uncaught class type end -----------------------');
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
className: className,
|
||||
typeParameters: typeParameters,
|
||||
heritageClauses: heritageClauses,
|
||||
classConstructor: classConstructor,
|
||||
classMethod: classMethod,
|
||||
classProperty: classProperty,
|
||||
exportModifiers: exportModifiers,
|
||||
staticMethods: staticMethods
|
||||
};
|
||||
}
|
||||
|
||||
export interface ClassEntity {
|
||||
className: string,
|
||||
typeParameters: Array<TypeParameterEntity>,
|
||||
heritageClauses: Array<HeritageClauseEntity>,
|
||||
classConstructor: Array<Array<ConstructorEntity>>,
|
||||
classMethod: Map<string, Array<MethodEntity>>,
|
||||
classProperty: Array<PropertyEntity>,
|
||||
exportModifiers: Array<number>,
|
||||
staticMethods: Array<StaticMethodEntity>
|
||||
}
|
44
mock-generate/src/declaration-node/constructorDeclaration.ts
Normal file
44
mock-generate/src/declaration-node/constructorDeclaration.ts
Normal file
@ -0,0 +1,44 @@
|
||||
import { ConstructorDeclaration, isIdentifier, Node, SourceFile } from 'typescript';
|
||||
|
||||
/**
|
||||
* get constructors info
|
||||
* @param node
|
||||
* @param sourceFile
|
||||
* @returns
|
||||
*/
|
||||
export function getConstructorDeclaration(node: Node, sourceFile: SourceFile): Array<ConstructorEntity> {
|
||||
const constructorNode = node as ConstructorDeclaration;
|
||||
const constructors: Array<ConstructorEntity> = [];
|
||||
constructorNode.parameters.forEach(value => {
|
||||
const paramElement = value.name;
|
||||
let name = '';
|
||||
let typeName = '';
|
||||
let typeKind = -1;
|
||||
if (isIdentifier(paramElement)) {
|
||||
name = paramElement.escapedText.toString();
|
||||
} else {
|
||||
name = sourceFile.text.substring(paramElement.pos, paramElement.end).trimStart().trimEnd();
|
||||
}
|
||||
|
||||
const paramTypeElement = value.type;
|
||||
if (paramTypeElement !== undefined) {
|
||||
typeName = sourceFile.text.substring(paramTypeElement.pos, paramTypeElement.end).trimStart().trimEnd();
|
||||
typeKind = paramTypeElement.kind;
|
||||
}
|
||||
|
||||
constructors.push(
|
||||
{
|
||||
name: name,
|
||||
typeName: typeName,
|
||||
typeKind: typeKind
|
||||
}
|
||||
);
|
||||
});
|
||||
return constructors;
|
||||
}
|
||||
|
||||
export interface ConstructorEntity {
|
||||
name: string,
|
||||
typeName: string,
|
||||
typeKind: number
|
||||
}
|
46
mock-generate/src/declaration-node/enumDeclaration.ts
Normal file
46
mock-generate/src/declaration-node/enumDeclaration.ts
Normal file
@ -0,0 +1,46 @@
|
||||
import { EnumDeclaration, SourceFile } from 'typescript';
|
||||
import { getExportKeyword, getPropertyName } from '../common/commonUtils';
|
||||
|
||||
/**
|
||||
* get enum info
|
||||
* @param node
|
||||
* @param sourceFile
|
||||
* @returns
|
||||
*/
|
||||
export function getEnumDeclaration(node: EnumDeclaration, sourceFile: SourceFile): EnumEntity {
|
||||
const enumName = node.name.escapedText.toString();
|
||||
const enumMembers: Array<MemberEntity> = [];
|
||||
let exportModifiers: Array<number> = [];
|
||||
|
||||
if (node.modifiers !== undefined) {
|
||||
exportModifiers = getExportKeyword(node.modifiers);
|
||||
}
|
||||
|
||||
node.members.forEach(value => {
|
||||
const enumValueName = getPropertyName(value.name, sourceFile);
|
||||
let enumValue = '';
|
||||
if (value.initializer !== undefined) {
|
||||
enumValue = sourceFile.text.substring(value.initializer.pos, value.initializer.end).trimEnd().trimStart();
|
||||
}
|
||||
const enumKind = value.initializer?.kind === undefined ? -1 : value.initializer?.kind;
|
||||
enumMembers.push({ enumValueName: enumValueName, enumValue: enumValue, enumKind: enumKind });
|
||||
});
|
||||
|
||||
return {
|
||||
enumName: enumName,
|
||||
enumMembers: enumMembers,
|
||||
exportModifiers: exportModifiers
|
||||
};
|
||||
}
|
||||
|
||||
export interface EnumEntity {
|
||||
enumName: string,
|
||||
enumMembers: Array<MemberEntity>,
|
||||
exportModifiers: Array<number>
|
||||
}
|
||||
|
||||
export interface MemberEntity {
|
||||
enumValueName: string,
|
||||
enumValue: string,
|
||||
enumKind: number,
|
||||
}
|
31
mock-generate/src/declaration-node/functionDeclaration.ts
Normal file
31
mock-generate/src/declaration-node/functionDeclaration.ts
Normal file
@ -0,0 +1,31 @@
|
||||
import { FunctionDeclaration, Node, SourceFile } from 'typescript';
|
||||
import { getFunctionAndMethodReturnInfo, getParameter, ParameterEntity, ReturnTypeEntity } from '../common/commonUtils';
|
||||
|
||||
/**
|
||||
* get function info
|
||||
* @param node
|
||||
* @param sourceFile
|
||||
* @returns
|
||||
*/
|
||||
export function getFunctionDeclaration(node: Node, sourceFile: SourceFile): FunctionEntity {
|
||||
const funcNode = node as FunctionDeclaration;
|
||||
let functionName = '';
|
||||
const args: Array<ParameterEntity> = [];
|
||||
const returnType = getFunctionAndMethodReturnInfo(funcNode, sourceFile);
|
||||
functionName = funcNode.name?.escapedText === undefined ? 'undefind' : funcNode.name.escapedText.toString();
|
||||
funcNode.parameters.forEach(value => {
|
||||
args.push(getParameter(value, sourceFile));
|
||||
});
|
||||
|
||||
return {
|
||||
functionName: functionName,
|
||||
returnType: returnType,
|
||||
args: args
|
||||
};
|
||||
}
|
||||
|
||||
export interface FunctionEntity {
|
||||
functionName: string,
|
||||
returnType: ReturnTypeEntity,
|
||||
args: Array<ParameterEntity>
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
import { HeritageClause, Node, SourceFile, SyntaxKind } from 'typescript';
|
||||
|
||||
/**
|
||||
* get heritage info
|
||||
* @param node
|
||||
* @param sourceFile
|
||||
* @returns
|
||||
*/
|
||||
export function getHeritageClauseDeclaration(node: Node, sourceFile: SourceFile): HeritageClauseEntity {
|
||||
const HeritageClauseNode = node as HeritageClause;
|
||||
const clauseToken = HeritageClauseNode.token === SyntaxKind.ExtendsKeyword ? 'extends' : 'implements';
|
||||
const types: Array<string> = [];
|
||||
|
||||
HeritageClauseNode.types.forEach(value => {
|
||||
types.push(sourceFile.text.substring(value.pos, value.end).trimStart().trimEnd());
|
||||
});
|
||||
|
||||
return {
|
||||
clauseToken: clauseToken,
|
||||
types: types
|
||||
};
|
||||
}
|
||||
|
||||
export interface HeritageClauseEntity {
|
||||
clauseToken: string,
|
||||
types: Array<string>
|
||||
}
|
@ -0,0 +1,91 @@
|
||||
import { ExportAssignment, ExportDeclaration, ImportDeclaration,
|
||||
ImportEqualsDeclaration, isImportDeclaration, Node, SourceFile } from 'typescript';
|
||||
|
||||
/**
|
||||
* get current sourceFile all imports
|
||||
* @param sourceFile
|
||||
* @returns
|
||||
*/
|
||||
export function getImportDeclarationArray(sourceFile: SourceFile): Array<ImportElementEntity> {
|
||||
const importDeclarations: Array<ImportElementEntity> = [];
|
||||
sourceFile.forEachChild(node => {
|
||||
if (isImportDeclaration(node)) {
|
||||
importDeclarations.push(getImportDeclaration(node, sourceFile));
|
||||
}
|
||||
});
|
||||
return importDeclarations;
|
||||
}
|
||||
|
||||
/**
|
||||
* get module inner import info
|
||||
* @param importEqualNode
|
||||
* @param sourceFile
|
||||
* @returns
|
||||
*/
|
||||
export function getModuleImportEqual(importEqualNode: ImportEqualsDeclaration, sourceFile: SourceFile): ImportEuqalEntity {
|
||||
return {
|
||||
importEqualName: importEqualNode.name.escapedText.toString(),
|
||||
importEqualTypeName: sourceFile.text.substring(importEqualNode.moduleReference.pos, importEqualNode.moduleReference.end).trimStart().trimEnd(),
|
||||
importEqualTypeKind: importEqualNode.moduleReference.kind
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* get export info
|
||||
* @param exportNode
|
||||
* @param sourceFile
|
||||
* @returns
|
||||
*/
|
||||
export function getExportDeclaration(exportNode: ExportDeclaration, sourceFile: SourceFile): string {
|
||||
return sourceFile.text.substring(exportNode.pos, exportNode.end).trimStart().trimEnd();
|
||||
}
|
||||
|
||||
/**
|
||||
* get import info
|
||||
* @param node
|
||||
* @param sourceFile
|
||||
* @returns
|
||||
*/
|
||||
export function getImportDeclaration(node: Node, sourceFile: SourceFile): ImportElementEntity {
|
||||
let importElements = '';
|
||||
const importNode = node as ImportDeclaration;
|
||||
const importPath = sourceFile.text.substring(importNode.moduleSpecifier.pos, importNode.moduleSpecifier.end).trimStart().trimEnd();
|
||||
const importClause = importNode.importClause;
|
||||
if (importClause !== undefined) {
|
||||
importElements = sourceFile.text.substring(importClause.pos, importClause.end).trimStart().trimEnd();
|
||||
}
|
||||
|
||||
return {
|
||||
importPath: importPath,
|
||||
importElements: importElements
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* get export info
|
||||
* @param exportAssigment
|
||||
* @param sourceFile
|
||||
* @returns
|
||||
*/
|
||||
export function getExportAssignment(exportAssigment: ExportAssignment, sourceFile: SourceFile): Array<string> {
|
||||
const exportAssignments: Array<string> = [];
|
||||
if (exportAssigment.expression !== undefined) {
|
||||
exportAssignments.push(sourceFile.text.substring(exportAssigment.expression.pos, exportAssigment.expression.end).trimStart().trimEnd());
|
||||
}
|
||||
return exportAssignments;
|
||||
}
|
||||
|
||||
export interface ImportElementEntity {
|
||||
importPath: string,
|
||||
importElements: string
|
||||
}
|
||||
|
||||
export interface ExportElementEntity {
|
||||
exportName: string
|
||||
}
|
||||
|
||||
export interface ImportEuqalEntity {
|
||||
importEqualName: string,
|
||||
importEqualTypeName: string,
|
||||
importEqualTypeKind: number
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
import { IndexSignatureDeclaration, SourceFile } from 'typescript';
|
||||
|
||||
/**
|
||||
* get index signature info
|
||||
* @param indexSignature
|
||||
* @param sourceFile
|
||||
* @returns
|
||||
*/
|
||||
export function getIndexSignatureDeclaration(indexSignature: IndexSignatureDeclaration, sourceFile: SourceFile): IndexSignatureEntity {
|
||||
return {
|
||||
indexSignatureKey: 'key',
|
||||
indexSignatureKind: indexSignature.type.kind,
|
||||
indexSignatureTypeName: sourceFile.text.substring(indexSignature.type.pos, indexSignature.type.end).trimStart().trimEnd()
|
||||
};
|
||||
}
|
||||
|
||||
export interface IndexSignatureEntity {
|
||||
indexSignatureKey: string,
|
||||
indexSignatureKind: number,
|
||||
indexSignatureTypeName: string
|
||||
}
|
89
mock-generate/src/declaration-node/interfaceDeclaration.ts
Normal file
89
mock-generate/src/declaration-node/interfaceDeclaration.ts
Normal file
@ -0,0 +1,89 @@
|
||||
import {
|
||||
InterfaceDeclaration, isCallSignatureDeclaration, isConstructSignatureDeclaration,
|
||||
isHeritageClause, isIndexSignatureDeclaration, isMethodSignature, isPropertySignature,
|
||||
isTypeParameterDeclaration, SourceFile
|
||||
} from 'typescript';
|
||||
import { getExportKeyword } from '../common/commonUtils';
|
||||
import { CallSignatureEntity, getCallSignatureDeclaration } from './callSignatureDeclaration';
|
||||
import { ConstructorEntity, getConstructorDeclaration } from './constructorDeclaration';
|
||||
import { getHeritageClauseDeclaration, HeritageClauseEntity } from './heritageClauseDeclaration';
|
||||
import { getIndexSignatureDeclaration, IndexSignatureEntity } from './indexSignatureDeclaration';
|
||||
import { getMethodSignatureDeclaration, MethodSignatureEntity } from './methodSignatureDeclaration';
|
||||
import { getPropertySignatureDeclaration, PropertySignatureEntity } from './propertySignatureDeclaration';
|
||||
import { getTypeParameterDeclaration, TypeParameterEntity } from './typeParameterDeclaration';
|
||||
|
||||
/**
|
||||
* get interface info
|
||||
* @param interfaceNode
|
||||
* @param sourceFile
|
||||
* @returns
|
||||
*/
|
||||
export function getInterfaceDeclaration(interfaceNode: InterfaceDeclaration, sourceFile: SourceFile): InterfaceEntity {
|
||||
let exportModifiers: Array<number> = [];
|
||||
if (interfaceNode.modifiers !== undefined) {
|
||||
exportModifiers = getExportKeyword(interfaceNode.modifiers);
|
||||
}
|
||||
|
||||
const interfaceName = interfaceNode.name.escapedText.toString();
|
||||
const heritageClauses: Array<HeritageClauseEntity> = [];
|
||||
const interfaceConstructors: Array<Array<ConstructorEntity>> = [];
|
||||
const interfaceMethodSignature: Map<string, Array<MethodSignatureEntity>> = new Map<string, Array<MethodSignatureEntity>>();
|
||||
const interfacePropertySignatures: Array<PropertySignatureEntity> = [];
|
||||
const callSignature: Array<CallSignatureEntity> = [];
|
||||
const typeParameters: Array<TypeParameterEntity> = [];
|
||||
const indexSignature: Array<IndexSignatureEntity> = [];
|
||||
|
||||
interfaceNode.members.forEach(value => {
|
||||
if (isPropertySignature(value)) {
|
||||
interfacePropertySignatures.push(getPropertySignatureDeclaration(value, sourceFile));
|
||||
} else if (isMethodSignature(value)) {
|
||||
const methodSignature = getMethodSignatureDeclaration(value, sourceFile);
|
||||
if (interfaceMethodSignature.get(methodSignature.functionName) !== undefined) {
|
||||
interfaceMethodSignature.get(methodSignature.functionName)?.push(methodSignature);
|
||||
} else {
|
||||
const methodSignatureArray: Array<MethodSignatureEntity> = [];
|
||||
methodSignatureArray.push(methodSignature);
|
||||
interfaceMethodSignature.set(methodSignature.functionName, methodSignatureArray);
|
||||
}
|
||||
} else if (isHeritageClause(value)) {
|
||||
heritageClauses.push(getHeritageClauseDeclaration(value, sourceFile));
|
||||
} else if (isConstructSignatureDeclaration(value)) {
|
||||
interfaceConstructors.push(getConstructorDeclaration(value, sourceFile));
|
||||
} else if (isCallSignatureDeclaration(value)) {
|
||||
callSignature.push(getCallSignatureDeclaration(value, sourceFile));
|
||||
} else if (isTypeParameterDeclaration(value)) {
|
||||
typeParameters.push(getTypeParameterDeclaration(value, sourceFile));
|
||||
} else if (isIndexSignatureDeclaration(value)) {
|
||||
indexSignature.push(getIndexSignatureDeclaration(value, sourceFile));
|
||||
} else {
|
||||
console.log('--------------------------- uncaught interface type start -----------------------');
|
||||
console.log('interfaceName: ' + interfaceName);
|
||||
console.log(value);
|
||||
console.log('--------------------------- uncaught interface type end -----------------------');
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
interfaceName: interfaceName,
|
||||
typeParameters: typeParameters,
|
||||
heritageClauses: heritageClauses,
|
||||
interfaceConstructors: interfaceConstructors,
|
||||
interfaceMethodSignature: interfaceMethodSignature,
|
||||
interfacePropertySignatures: interfacePropertySignatures,
|
||||
callSignatures: callSignature,
|
||||
exportModifiers: exportModifiers,
|
||||
indexSignature: indexSignature
|
||||
};
|
||||
}
|
||||
|
||||
export interface InterfaceEntity {
|
||||
interfaceName: string,
|
||||
typeParameters: Array<TypeParameterEntity>,
|
||||
heritageClauses: Array<HeritageClauseEntity>,
|
||||
interfaceConstructors: Array<Array<ConstructorEntity>>,
|
||||
interfaceMethodSignature: Map<string, Array<MethodSignatureEntity>>,
|
||||
interfacePropertySignatures: Array<PropertySignatureEntity>,
|
||||
callSignatures: Array<CallSignatureEntity>,
|
||||
exportModifiers: Array<number>,
|
||||
indexSignature: Array<IndexSignatureEntity>
|
||||
}
|
64
mock-generate/src/declaration-node/methodDeclaration.ts
Normal file
64
mock-generate/src/declaration-node/methodDeclaration.ts
Normal file
@ -0,0 +1,64 @@
|
||||
import { isComputedPropertyName, MethodDeclaration, Node, SourceFile } from 'typescript';
|
||||
import {
|
||||
getFunctionAndMethodReturnInfo, getModifiers, getParameter,
|
||||
getPropertyName, ParameterEntity, ReturnTypeEntity
|
||||
} from '../common/commonUtils';
|
||||
|
||||
/**
|
||||
* get method info
|
||||
* @param node
|
||||
* @param sourceFile
|
||||
* @returns
|
||||
*/
|
||||
export function getMethodDeclaration(node: Node, sourceFile: SourceFile): MethodEntity {
|
||||
const methodNode = node as MethodDeclaration;
|
||||
const functionName = {
|
||||
name: '',
|
||||
expressionKind: -1,
|
||||
kind: -1
|
||||
};
|
||||
|
||||
const args: Array<ParameterEntity> = [];
|
||||
let modifiers: Array<number> = [];
|
||||
|
||||
if (methodNode.modifiers !== undefined) {
|
||||
modifiers = getModifiers(methodNode.modifiers);
|
||||
}
|
||||
|
||||
const returnType = getFunctionAndMethodReturnInfo(methodNode, sourceFile);
|
||||
functionName.name = getPropertyName(methodNode.name, sourceFile);
|
||||
if (isComputedPropertyName(methodNode.name)) {
|
||||
functionName.expressionKind = methodNode.name.expression.kind;
|
||||
}
|
||||
|
||||
functionName.kind = methodNode.name.kind;
|
||||
|
||||
methodNode.parameters.forEach(value => {
|
||||
args.push(getParameter(value, sourceFile));
|
||||
});
|
||||
|
||||
return {
|
||||
modifiers: modifiers,
|
||||
functionName: functionName,
|
||||
returnType: returnType,
|
||||
args: args
|
||||
};
|
||||
}
|
||||
|
||||
export interface StaticMethodEntity {
|
||||
className: string,
|
||||
methodEntity: MethodEntity
|
||||
}
|
||||
|
||||
export interface MethodEntity {
|
||||
modifiers: Array<number>,
|
||||
functionName: FunctionNameEntity,
|
||||
returnType: ReturnTypeEntity,
|
||||
args: Array<ParameterEntity>
|
||||
}
|
||||
|
||||
export interface FunctionNameEntity {
|
||||
name: string,
|
||||
expressionKind: number,
|
||||
kind: number
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
import { MethodSignature, Node, SourceFile } from 'typescript';
|
||||
import {
|
||||
getFunctionAndMethodReturnInfo, getParameter, getPropertyName,
|
||||
ParameterEntity, ReturnTypeEntity
|
||||
} from '../common/commonUtils';
|
||||
|
||||
/**
|
||||
* get interface signature info
|
||||
* @param node
|
||||
* @param sourceFile
|
||||
* @returns
|
||||
*/
|
||||
export function getMethodSignatureDeclaration(node: Node, sourceFile: SourceFile): MethodSignatureEntity {
|
||||
const methodSignatureNode = node as MethodSignature;
|
||||
let functionName = '';
|
||||
const args: Array<ParameterEntity> = [];
|
||||
const returnType = getFunctionAndMethodReturnInfo(methodSignatureNode, sourceFile);
|
||||
functionName = getPropertyName(methodSignatureNode.name, sourceFile);
|
||||
methodSignatureNode.parameters.forEach(value => {
|
||||
args.push(getParameter(value, sourceFile));
|
||||
});
|
||||
|
||||
return {
|
||||
functionName: functionName,
|
||||
returnType: returnType,
|
||||
args: args
|
||||
};
|
||||
}
|
||||
|
||||
export interface MethodSignatureEntity {
|
||||
functionName: string,
|
||||
returnType: ReturnTypeEntity,
|
||||
args: Array<ParameterEntity>
|
||||
}
|
111
mock-generate/src/declaration-node/moduleDeclaration.ts
Normal file
111
mock-generate/src/declaration-node/moduleDeclaration.ts
Normal file
@ -0,0 +1,111 @@
|
||||
import {
|
||||
isClassDeclaration, isEnumDeclaration, isExportDeclaration, isFunctionDeclaration, isIdentifier,
|
||||
isImportEqualsDeclaration, isInterfaceDeclaration, isModuleBlock, isModuleDeclaration, isTypeAliasDeclaration,
|
||||
isVariableStatement, ModuleDeclaration, Node, SourceFile
|
||||
} from 'typescript';
|
||||
import { getExportKeyword } from '../common/commonUtils';
|
||||
import { ClassEntity, getClassDeclaration } from './classDeclaration';
|
||||
import { EnumEntity, getEnumDeclaration } from './enumDeclaration';
|
||||
import { FunctionEntity, getFunctionDeclaration } from './functionDeclaration';
|
||||
import { getExportDeclaration, getModuleImportEqual, ImportEuqalEntity } from './importAndExportDeclaration';
|
||||
import { getInterfaceDeclaration, InterfaceEntity } from './interfaceDeclaration';
|
||||
import { getTypeAliasDeclaration, TypeAliasEntity } from './typeAliasDeclaration';
|
||||
import { getVariableStatementDeclaration, StatementEntity } from './variableStatementResolve';
|
||||
|
||||
/**
|
||||
* get module info
|
||||
* @param node
|
||||
* @param sourceFile
|
||||
* @param fileName
|
||||
* @returns
|
||||
*/
|
||||
export function getModuleDeclaration(node: Node, sourceFile: SourceFile, fileName: string): ModuleBlockEntity {
|
||||
const moduleNode = node as ModuleDeclaration;
|
||||
let moduleName = '';
|
||||
if (isIdentifier(moduleNode.name)) {
|
||||
moduleName = moduleNode.name.escapedText.toString();
|
||||
} else {
|
||||
moduleName = sourceFile.text.substring(moduleNode.name.pos, moduleNode.name.end).trimStart().trimEnd();
|
||||
}
|
||||
|
||||
let exportModifiers: Array<number> = [];
|
||||
const modifiers = moduleNode.modifiers;
|
||||
if (modifiers !== undefined) {
|
||||
exportModifiers = getExportKeyword(modifiers);
|
||||
}
|
||||
|
||||
const typeAliasDeclarations: Array<TypeAliasEntity> = [];
|
||||
const classDeclarations: Array<ClassEntity> = [];
|
||||
const interfaceDeclarations: Array<InterfaceEntity> = [];
|
||||
const functionDeclarations: Map<string, Array<FunctionEntity>> = new Map<string, Array<FunctionEntity>>();
|
||||
const enumDeclarations: Array<EnumEntity> = [];
|
||||
const moduleDeclarations: Array<ModuleBlockEntity> = [];
|
||||
const variableStatements: Array<Array<StatementEntity>> = [];
|
||||
const moduleImportEquaqls: Array<ImportEuqalEntity> = [];
|
||||
const exportDeclarations: Array<string> = [];
|
||||
const moduleBody = moduleNode.body;
|
||||
|
||||
if (moduleBody !== undefined && isModuleBlock(moduleBody)) {
|
||||
moduleBody.statements.forEach(value => {
|
||||
if (isFunctionDeclaration(value)) {
|
||||
const FunctionEntity = getFunctionDeclaration(value, sourceFile);
|
||||
if (functionDeclarations.get(FunctionEntity.functionName) !== undefined) {
|
||||
functionDeclarations.get(FunctionEntity.functionName)?.push(FunctionEntity);
|
||||
} else {
|
||||
const functionArray: Array<FunctionEntity> = [];
|
||||
functionArray.push(FunctionEntity);
|
||||
functionDeclarations.set(FunctionEntity.functionName, functionArray);
|
||||
}
|
||||
} else if (isTypeAliasDeclaration(value)) {
|
||||
typeAliasDeclarations.push(getTypeAliasDeclaration(value, sourceFile));
|
||||
} else if (isEnumDeclaration(value)) {
|
||||
enumDeclarations.push(getEnumDeclaration(value, sourceFile));
|
||||
} else if (isClassDeclaration(value)) {
|
||||
classDeclarations.push(getClassDeclaration(value, sourceFile));
|
||||
} else if (isInterfaceDeclaration(value)) {
|
||||
interfaceDeclarations.push(getInterfaceDeclaration(value, sourceFile));
|
||||
} else if (isModuleDeclaration(value)) {
|
||||
moduleDeclarations.push(getModuleDeclaration(value, sourceFile, fileName));
|
||||
} else if (isVariableStatement(value)) {
|
||||
variableStatements.push(getVariableStatementDeclaration(value, sourceFile));
|
||||
} else if (isImportEqualsDeclaration(value)) {
|
||||
moduleImportEquaqls.push(getModuleImportEqual(value, sourceFile));
|
||||
} else if (isExportDeclaration(value)) {
|
||||
exportDeclarations.push(getExportDeclaration(value, sourceFile));
|
||||
} else {
|
||||
console.log('--------------------------- uncaught module type start -----------------------');
|
||||
console.log('fileName: ' + fileName);
|
||||
console.log(value);
|
||||
console.log('--------------------------- uncaught module type end -----------------------');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
moduleName: moduleName,
|
||||
exportModifiers: exportModifiers,
|
||||
typeAliasDeclarations: typeAliasDeclarations,
|
||||
classDeclarations: classDeclarations,
|
||||
interfaceDeclarations: interfaceDeclarations,
|
||||
functionDeclarations: functionDeclarations,
|
||||
enumDeclarations: enumDeclarations,
|
||||
moduleDeclarations: moduleDeclarations,
|
||||
variableStatements: variableStatements,
|
||||
moduleImportEquaqls: moduleImportEquaqls,
|
||||
exportDeclarations: exportDeclarations
|
||||
};
|
||||
}
|
||||
|
||||
export interface ModuleBlockEntity {
|
||||
moduleName: string,
|
||||
exportModifiers: Array<number>,
|
||||
typeAliasDeclarations: Array<TypeAliasEntity>,
|
||||
classDeclarations: Array<ClassEntity>,
|
||||
interfaceDeclarations: Array<InterfaceEntity>,
|
||||
functionDeclarations: Map<string, Array<FunctionEntity>>,
|
||||
enumDeclarations: Array<EnumEntity>,
|
||||
moduleDeclarations: Array<ModuleBlockEntity>,
|
||||
variableStatements: Array<Array<StatementEntity>>,
|
||||
moduleImportEquaqls: Array<ImportEuqalEntity>,
|
||||
exportDeclarations: Array<string>
|
||||
}
|
54
mock-generate/src/declaration-node/propertyDeclaration.ts
Normal file
54
mock-generate/src/declaration-node/propertyDeclaration.ts
Normal file
@ -0,0 +1,54 @@
|
||||
import { PropertyDeclaration, SourceFile } from 'typescript';
|
||||
import { getPropertyName } from '../common/commonUtils';
|
||||
|
||||
/**
|
||||
* get property node info
|
||||
* @param node
|
||||
* @param sourceFile
|
||||
* @returns
|
||||
*/
|
||||
export function getPropertyDeclaration(node: PropertyDeclaration, sourceFile: SourceFile): PropertyEntity {
|
||||
let propertyName = '';
|
||||
let propertyTypeName = '';
|
||||
let kind = -1;
|
||||
let isInitializer = false;
|
||||
let initializer = '';
|
||||
const modifiers: Array<string> = [];
|
||||
|
||||
if (node.modifiers !== undefined) {
|
||||
node.modifiers.forEach(value => {
|
||||
modifiers.push(sourceFile.text.substring(value.pos, value.end));
|
||||
});
|
||||
}
|
||||
|
||||
if (node.initializer !== undefined) {
|
||||
isInitializer = true;
|
||||
initializer = sourceFile.text.substring(node.initializer.pos, node.initializer.end).trimStart().trimEnd();
|
||||
}
|
||||
|
||||
propertyName = getPropertyName(node.name, sourceFile);
|
||||
|
||||
const propertyType = node.type;
|
||||
if (propertyType !== undefined) {
|
||||
propertyTypeName = sourceFile.text.substring(propertyType.pos, propertyType.end).trimStart().trimEnd();
|
||||
kind = propertyType.kind;
|
||||
}
|
||||
|
||||
return {
|
||||
modifiers: modifiers,
|
||||
propertyName: propertyName,
|
||||
propertyTypeName: propertyTypeName,
|
||||
kind: kind,
|
||||
isInitializer: isInitializer,
|
||||
initializer: initializer
|
||||
};
|
||||
}
|
||||
|
||||
export interface PropertyEntity {
|
||||
modifiers: Array<string>,
|
||||
propertyName: string,
|
||||
propertyTypeName: string,
|
||||
kind: number,
|
||||
isInitializer: boolean,
|
||||
initializer: string
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
import { PropertySignature, SourceFile } from 'typescript';
|
||||
import { getPropertyName } from '../common/commonUtils';
|
||||
|
||||
/**
|
||||
* get interface property signature info
|
||||
* @param node
|
||||
* @param sourceFile
|
||||
* @returns
|
||||
*/
|
||||
export function getPropertySignatureDeclaration(node: PropertySignature, sourceFile: SourceFile): PropertySignatureEntity {
|
||||
let propertyName = '';
|
||||
let propertyTypeName = '';
|
||||
let kind = -1;
|
||||
|
||||
propertyName = getPropertyName(node.name, sourceFile);
|
||||
const propertyType = node.type;
|
||||
const modifiers: Array<string> = [];
|
||||
if (node.modifiers !== undefined) {
|
||||
node.modifiers.forEach(value => {
|
||||
modifiers.push(sourceFile.text.substring(value.pos, value.end));
|
||||
});
|
||||
}
|
||||
|
||||
if (propertyType !== undefined) {
|
||||
propertyTypeName = sourceFile.text.substring(propertyType.pos, propertyType.end).trimStart().trimEnd();
|
||||
kind = propertyType.kind;
|
||||
}
|
||||
|
||||
return {
|
||||
modifiers: modifiers,
|
||||
propertyName: propertyName,
|
||||
propertyTypeName: propertyTypeName,
|
||||
kind: kind
|
||||
};
|
||||
}
|
||||
|
||||
export interface PropertySignatureEntity {
|
||||
modifiers: Array<string>,
|
||||
propertyName: string,
|
||||
propertyTypeName: string,
|
||||
kind: number
|
||||
}
|
149
mock-generate/src/declaration-node/sourceFileElementsAssemply.ts
Normal file
149
mock-generate/src/declaration-node/sourceFileElementsAssemply.ts
Normal file
@ -0,0 +1,149 @@
|
||||
import {
|
||||
isClassDeclaration, isEnumDeclaration, isExportAssignment, isExportDeclaration, isFunctionDeclaration,
|
||||
isImportDeclaration, isInterfaceDeclaration, isModuleDeclaration, isTypeAliasDeclaration, isVariableStatement,
|
||||
SourceFile, SyntaxKind
|
||||
} from 'typescript';
|
||||
import { ClassEntity, getClassDeclaration } from './classDeclaration';
|
||||
import { EnumEntity, getEnumDeclaration } from './enumDeclaration';
|
||||
import { FunctionEntity, getFunctionDeclaration } from './functionDeclaration';
|
||||
import { getExportAssignment, getImportDeclaration, ImportElementEntity } from './importAndExportDeclaration';
|
||||
import { getInterfaceDeclaration, InterfaceEntity } from './interfaceDeclaration';
|
||||
import { StaticMethodEntity } from './methodDeclaration';
|
||||
import { getModuleDeclaration, ModuleBlockEntity } from './moduleDeclaration';
|
||||
import { getTypeAliasDeclaration, TypeAliasEntity } from './typeAliasDeclaration';
|
||||
import { getVariableStatementDeclaration, StatementEntity } from './variableStatementResolve';
|
||||
|
||||
/**
|
||||
* assembly all sourceFile node info
|
||||
* @param sourceFile
|
||||
* @param fileName
|
||||
* @returns
|
||||
*/
|
||||
export function getSourceFileAssembly(sourceFile: SourceFile, fileName: string): SourceFileEntity {
|
||||
const importDeclarations: Array<ImportElementEntity> = [];
|
||||
const moduleDeclarations: Array<ModuleBlockEntity> = [];
|
||||
const typeAliasDeclarations: Array<TypeAliasEntity> = [];
|
||||
const classDeclarations: Array<ClassEntity> = [];
|
||||
const interfaceDeclarations: Array<InterfaceEntity> = [];
|
||||
const enumDeclarations: Array<EnumEntity> = [];
|
||||
let exportAssignment: Array<string> = [];
|
||||
const staticMethods: Array<Array<StaticMethodEntity>> = [];
|
||||
const exportDeclarations: Array<string> = [];
|
||||
|
||||
sourceFile.forEachChild(node => {
|
||||
if (isImportDeclaration(node)) {
|
||||
importDeclarations.push(getImportDeclaration(node, sourceFile));
|
||||
} else if (isModuleDeclaration(node)) {
|
||||
moduleDeclarations.push(getModuleDeclaration(node, sourceFile, fileName));
|
||||
} else if (isTypeAliasDeclaration(node)) {
|
||||
typeAliasDeclarations.push(getTypeAliasDeclaration(node, sourceFile));
|
||||
} else if (isClassDeclaration(node)) {
|
||||
let isDefaultExportClass = false;
|
||||
if (node.modifiers !== undefined) {
|
||||
node.modifiers.forEach(value => {
|
||||
if (value.kind === SyntaxKind.DefaultKeyword) {
|
||||
isDefaultExportClass = true;
|
||||
}
|
||||
});
|
||||
}
|
||||
if (isDefaultExportClass) {
|
||||
const classDeclarationEntity = getClassDeclaration(node, sourceFile);
|
||||
classDeclarations.push(classDeclarationEntity);
|
||||
if (classDeclarationEntity.staticMethods.length > 0) {
|
||||
staticMethods.push(classDeclarationEntity.staticMethods);
|
||||
}
|
||||
}
|
||||
} else if (isInterfaceDeclaration(node)) {
|
||||
interfaceDeclarations.push(getInterfaceDeclaration(node, sourceFile));
|
||||
} else if (isExportAssignment(node)) {
|
||||
exportAssignment = getExportAssignment(node, sourceFile);
|
||||
} else if (isEnumDeclaration(node)) {
|
||||
enumDeclarations.push(getEnumDeclaration(node, sourceFile));
|
||||
} else if (isExportDeclaration(node)) {
|
||||
exportDeclarations.push(sourceFile.text.substring(node.pos, node.end).trimStart().trimEnd());
|
||||
} else {
|
||||
if (node.kind !== SyntaxKind.EndOfFileToken && !isFunctionDeclaration(node) && !isVariableStatement(node)) {
|
||||
console.log('--------------------------- uncaught sourceFile type start -----------------------');
|
||||
console.log('fileName: ' + fileName);
|
||||
console.log(node);
|
||||
console.log('--------------------------- uncaught sourceFile type end -----------------------');
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
importDeclarations: importDeclarations,
|
||||
moduleDeclarations: moduleDeclarations,
|
||||
typeAliasDeclarations: typeAliasDeclarations,
|
||||
classDeclarations: classDeclarations,
|
||||
interfaceDeclarations: interfaceDeclarations,
|
||||
enumDeclarations: enumDeclarations,
|
||||
exportAssignment: exportAssignment,
|
||||
staticMethods: staticMethods,
|
||||
exportDeclarations: exportDeclarations
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* get default export class
|
||||
* @param sourceFile
|
||||
* @returns
|
||||
*/
|
||||
export function getDefaultExportClassDeclaration(sourceFile: SourceFile): Array<ClassEntity> {
|
||||
const defaultExportClass: Array<ClassEntity> = [];
|
||||
sourceFile.forEachChild(node => {
|
||||
if (isClassDeclaration(node)) {
|
||||
defaultExportClass.push(getClassDeclaration(node, sourceFile));
|
||||
}
|
||||
});
|
||||
return defaultExportClass;
|
||||
}
|
||||
|
||||
/**
|
||||
* get sourceFile const variable statement
|
||||
* @param sourceFile
|
||||
* @returns
|
||||
*/
|
||||
export function getSourceFileVariableStatements(sourceFile: SourceFile): Array<Array<StatementEntity>> {
|
||||
const variableStatements: Array<Array<StatementEntity>> = [];
|
||||
sourceFile.forEachChild(node => {
|
||||
if (isVariableStatement(node)) {
|
||||
variableStatements.push(getVariableStatementDeclaration(node, sourceFile));
|
||||
}
|
||||
});
|
||||
return variableStatements;
|
||||
}
|
||||
|
||||
/**
|
||||
* get sourcefile functions
|
||||
* @param sourceFile
|
||||
* @returns
|
||||
*/
|
||||
export function getSourceFileFunctions(sourceFile: SourceFile): Map<string, Array<FunctionEntity>> {
|
||||
const functionDeclarations: Map<string, Array<FunctionEntity>> = new Map<string, Array<FunctionEntity>>();
|
||||
sourceFile.forEachChild(node => {
|
||||
if (isFunctionDeclaration(node)) {
|
||||
const functionEntity = getFunctionDeclaration(node, sourceFile);
|
||||
if (functionDeclarations.get(functionEntity.functionName) !== undefined) {
|
||||
functionDeclarations.get(functionEntity.functionName)?.push(functionEntity);
|
||||
} else {
|
||||
const functionArray: Array<FunctionEntity> = [];
|
||||
functionArray.push(functionEntity);
|
||||
functionDeclarations.set(functionEntity.functionName, functionArray);
|
||||
}
|
||||
}
|
||||
});
|
||||
return functionDeclarations;
|
||||
}
|
||||
|
||||
export interface SourceFileEntity {
|
||||
importDeclarations: Array<ImportElementEntity>,
|
||||
moduleDeclarations: Array<ModuleBlockEntity>,
|
||||
typeAliasDeclarations: Array<TypeAliasEntity>,
|
||||
classDeclarations: Array<ClassEntity>,
|
||||
interfaceDeclarations: Array<InterfaceEntity>,
|
||||
enumDeclarations: Array<EnumEntity>,
|
||||
exportAssignment: Array<string>,
|
||||
staticMethods: Array<Array<StaticMethodEntity>>,
|
||||
exportDeclarations: Array<string>
|
||||
}
|
71
mock-generate/src/declaration-node/typeAliasDeclaration.ts
Normal file
71
mock-generate/src/declaration-node/typeAliasDeclaration.ts
Normal file
@ -0,0 +1,71 @@
|
||||
import {
|
||||
isTypeLiteralNode, isTypeReferenceNode,
|
||||
isUnionTypeNode, Node, SourceFile, TypeAliasDeclaration
|
||||
} from 'typescript';
|
||||
|
||||
/**
|
||||
* get type alias info
|
||||
* @param node
|
||||
* @param sourceFile
|
||||
* @returns
|
||||
*/
|
||||
export function getTypeAliasDeclaration(node: Node, sourceFile: SourceFile): TypeAliasEntity {
|
||||
const typeAliasNode = node as TypeAliasDeclaration;
|
||||
const typeAliasName = typeAliasNode.name.escapedText.toString();
|
||||
const typeAliasTypeKind = typeAliasNode.type.kind;
|
||||
const typeAliasTypeElements: Array<TypeAliasTypeEntity> = [];
|
||||
const modifiers: Array<number> = [];
|
||||
|
||||
const modifiersNode = typeAliasNode.modifiers;
|
||||
if (modifiersNode !== undefined) {
|
||||
modifiersNode.forEach(value => {
|
||||
modifiers.push(value.kind);
|
||||
});
|
||||
}
|
||||
|
||||
const typeAliasTypeElementsNode = typeAliasNode.type;
|
||||
if (typeAliasTypeElementsNode !== undefined) {
|
||||
if (isUnionTypeNode(typeAliasTypeElementsNode)) {
|
||||
typeAliasTypeElementsNode.types.forEach(value => {
|
||||
const typeName = sourceFile.text.substring(value.pos, value.end).trimStart().trimEnd();
|
||||
const typeKind = value.kind;
|
||||
typeAliasTypeElements.push({ typeName: typeName, typeKind: typeKind });
|
||||
});
|
||||
} else if (isTypeReferenceNode(typeAliasTypeElementsNode)) {
|
||||
const typeName = sourceFile.text.substring(typeAliasTypeElementsNode.typeName.pos, typeAliasTypeElementsNode.typeName.end).trimStart().trimEnd();
|
||||
typeAliasTypeElements.push({ typeName: typeName, typeKind: typeAliasTypeElementsNode.typeName.kind });
|
||||
} else if (isTypeLiteralNode(typeAliasTypeElementsNode)) {
|
||||
typeAliasTypeElementsNode.members.forEach(value => {
|
||||
const typeName = sourceFile.text.substring(value.pos, value.end).trimStart().trimEnd();
|
||||
const typeKind = value.kind;
|
||||
typeAliasTypeElements.push({ typeName: typeName, typeKind: typeKind });
|
||||
});
|
||||
} else {
|
||||
typeAliasTypeElements.push(
|
||||
{
|
||||
typeName: sourceFile.text.substring(typeAliasTypeElementsNode.pos, typeAliasTypeElementsNode.end),
|
||||
typeKind: typeAliasTypeElementsNode.kind
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
typeAliasName: typeAliasName,
|
||||
typeAliasTypeKind: typeAliasTypeKind,
|
||||
typeAliasTypeElements: typeAliasTypeElements,
|
||||
modifiers: modifiers
|
||||
};
|
||||
}
|
||||
|
||||
export interface TypeAliasEntity {
|
||||
typeAliasName: string,
|
||||
typeAliasTypeKind: number,
|
||||
typeAliasTypeElements: Array<TypeAliasTypeEntity>,
|
||||
modifiers: Array<number>
|
||||
}
|
||||
|
||||
export interface TypeAliasTypeEntity {
|
||||
typeName: string,
|
||||
typeKind: number
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
import { Node, SourceFile, TypeParameterDeclaration } from 'typescript';
|
||||
|
||||
/**
|
||||
* get generic type node info
|
||||
* @param node
|
||||
* @param sourceFile
|
||||
* @returns
|
||||
*/
|
||||
export function getTypeParameterDeclaration(node: Node, sourceFile: SourceFile): TypeParameterEntity {
|
||||
const typeParameterNode = node as TypeParameterDeclaration;
|
||||
const typeParameterName = typeParameterNode.name.escapedText.toString();
|
||||
let constraitValue = '';
|
||||
let constraintDefaultValue = '';
|
||||
const constraint = typeParameterNode.constraint;
|
||||
if (constraint !== undefined) {
|
||||
constraitValue = sourceFile.text.substring(constraint.pos, constraint.end).trimStart().trimEnd();
|
||||
}
|
||||
|
||||
const defaultValue = typeParameterNode.default;
|
||||
if (defaultValue !== undefined) {
|
||||
constraintDefaultValue = sourceFile.text.substring(defaultValue.pos, defaultValue.end).trimStart().trimEnd();
|
||||
}
|
||||
|
||||
return {
|
||||
typeParameterName: typeParameterName,
|
||||
constraitValue: constraitValue,
|
||||
constraintDefaultValue: constraintDefaultValue
|
||||
};
|
||||
}
|
||||
|
||||
export interface TypeParameterEntity {
|
||||
typeParameterName: string,
|
||||
constraitValue: string,
|
||||
constraintDefaultValue: string
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
import { isIdentifier, SourceFile, VariableStatement } from 'typescript';
|
||||
|
||||
/**
|
||||
* get const declaration variable
|
||||
* @param variableStatement
|
||||
* @param sourceFile
|
||||
* @returns
|
||||
*/
|
||||
export function getVariableStatementDeclaration(variableStatement: VariableStatement, sourceFile: SourceFile): Array<StatementEntity> {
|
||||
const statementsArray: Array<StatementEntity> = [];
|
||||
variableStatement.declarationList.declarations.forEach(value => {
|
||||
let statementName = '';
|
||||
let initializer = '';
|
||||
let typeName = '';
|
||||
let typeKind = -1;
|
||||
|
||||
if (isIdentifier(value.name)) {
|
||||
statementName = value.name.escapedText.toString();
|
||||
} else {
|
||||
statementName = sourceFile.text.substring(value.pos, value.end).trimStart().trimEnd();
|
||||
}
|
||||
if (value.initializer !== undefined) {
|
||||
initializer = sourceFile.text.substring(value.initializer.pos, value.initializer.end);
|
||||
}
|
||||
if (value.type !== undefined) {
|
||||
typeName = sourceFile.text.substring(value.type.pos, value.type.end);
|
||||
typeKind = value.type.kind;
|
||||
}
|
||||
statementsArray.push({ statementName: statementName, typeName: typeName, typeKind: typeKind, initializer: initializer });
|
||||
});
|
||||
return statementsArray;
|
||||
}
|
||||
|
||||
export interface StatementEntity {
|
||||
statementName: string,
|
||||
typeName: string,
|
||||
typeKind: number,
|
||||
initializer: string
|
||||
}
|
22
mock-generate/tsconfig.json
Normal file
22
mock-generate/tsconfig.json
Normal file
@ -0,0 +1,22 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"allowJs": true,
|
||||
"moduleResolution": "node",
|
||||
"module": "commonjs",
|
||||
"target": "ES2019",
|
||||
"outDir": "dist",
|
||||
"esModuleInterop": true,
|
||||
"types": [
|
||||
"node"
|
||||
],
|
||||
"typeRoots": [
|
||||
"./node_modules/@types"
|
||||
]
|
||||
},
|
||||
"include": [
|
||||
"./src/**/*.ts"
|
||||
],
|
||||
"exclude": [
|
||||
"node_modules"
|
||||
]
|
||||
}
|
@ -34,8 +34,8 @@ import { FragBlockInterface,
|
||||
import Vm from '../main/model';
|
||||
import { CSS_INHERITANCE } from '../main/app/bundle';
|
||||
import {interceptCallback} from '../main/manage/event/callbackIntercept';
|
||||
import {mockwebgl} from '../main/extend/systemplugin/napi/webgl';
|
||||
import {mockwebgl2} from '../main/extend/systemplugin/napi/webgl2';
|
||||
import {mockWebgl} from '../main/extend/systemplugin/napi/webgl/webgl';
|
||||
import {mockWebgl2} from '../main/extend/systemplugin/napi/webgl/webgl2';
|
||||
import { VmOptions } from '../main/model/vmOptions';
|
||||
/**
|
||||
* Element is a basic class to describe a tree node in vdom.
|
||||
@ -84,9 +84,9 @@ class Element extends Node {
|
||||
// support aceapp callback style
|
||||
args = interceptCallback(args);
|
||||
if (args[0] === 'webgl') {
|
||||
return mockwebgl();
|
||||
return mockWebgl();
|
||||
} else if (args[0] === 'webgl2') {
|
||||
return mockwebgl2();
|
||||
return mockWebgl2();
|
||||
}
|
||||
const ret = taskCenter.send('component', {
|
||||
ref: this.ref,
|
||||
|
Loading…
Reference in New Issue
Block a user