@waiting/shared-types-dev
Advanced tools
Comparing version 7.4.0 to 8.0.0
@@ -6,2 +6,13 @@ # Change Log | ||
# 8.0.0 (2021-03-29) | ||
### Features | ||
* **types-dev:** add createObjectLiteralExpression() ([6320785](https://github.com/waitingsong/shared/commit/6320785d552895ceb564f3359d6839198ef5f48a)) | ||
# 7.4.0 (2021-03-26) | ||
@@ -8,0 +19,0 @@ |
@@ -5,3 +5,3 @@ /** | ||
* | ||
* @version 7.3.0 | ||
* @version 7.4.0 | ||
* @author waiting | ||
@@ -17,3 +17,8 @@ * @license MIT | ||
var tsMorph = require('ts-morph'); | ||
var ts = require('typescript'); | ||
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } | ||
var ts__default = /*#__PURE__*/_interopDefaultLegacy(ts); | ||
function createSourceFile(tsConfigFilePath, sourcePath) { | ||
@@ -25,2 +30,14 @@ const project = new tsMorph.Project({ tsConfigFilePath }); | ||
} | ||
function isTypeImported(file, matchTypeName, moduleName) { | ||
if (!moduleName) { | ||
throw new Error('Value of param moduleName empty'); | ||
} | ||
const arr = file.getDescendantsOfKind(tsMorph.SyntaxKind.ImportSpecifier); | ||
const exists = arr.map(item => item.getText()); | ||
const name = matchTypeName.trim(); | ||
if (exists.includes(name)) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
function hasImportNecessaryType(file, matchTypeNames, moduleName) { | ||
@@ -31,7 +48,5 @@ if (!moduleName) { | ||
let inserted = 0; | ||
const arr = file.getDescendantsOfKind(tsMorph.SyntaxKind.ImportSpecifier); | ||
const exists = arr.map(item => item.getText()); | ||
matchTypeNames.forEach((typeName) => { | ||
const name = typeName.trim(); | ||
if (exists.includes(name)) { | ||
if (isTypeImported(file, name, moduleName)) { | ||
return; | ||
@@ -45,4 +60,8 @@ } | ||
} | ||
function retrieveTypeArgmentFromCallExpression(input) { | ||
const [node] = input.getTypeArguments(); | ||
function retrieveTypeArgsFromCallExpression(input) { | ||
const nodes = input.getTypeArguments(); | ||
return nodes; | ||
} | ||
function retrieveFirstTypeArgTextFromCallExpression(input) { | ||
const [node] = retrieveTypeArgsFromCallExpression(input); | ||
if (!node) { | ||
@@ -62,7 +81,2 @@ return ''; | ||
} | ||
// const parentNode = expression.getParent() | ||
// if (parentNode) { | ||
// const pKindName = parentNode.getKindName() | ||
// console.log({ code, pKindName }) | ||
// } | ||
return true; | ||
@@ -72,2 +86,21 @@ }); | ||
} | ||
function retrieveVarnameFromCallExpression(expression) { | ||
const parentNode = expression.getParent(); | ||
if (!parentNode) { | ||
throw new TypeError('expression has no parent node'); | ||
} | ||
const name = retrieveVarnameFromVariableDeclaration(parentNode); | ||
return name; | ||
} | ||
function retrieveVarnameFromVariableDeclaration(input) { | ||
const kind = input.getKind(); | ||
const sym = input.getSymbol(); | ||
if (kind === tsMorph.SyntaxKind.VariableDeclaration && sym) { | ||
// eslint-disable-next-line | ||
// const name = input.getNameNode().getText() as string | ||
const name = sym.getName(); | ||
return name; | ||
} | ||
throw new TypeError('input is not VariableDeclaration node'); | ||
} | ||
@@ -98,6 +131,10 @@ // @ts-nocheck | ||
}; | ||
/** | ||
* @returns Map<varname, computer object> | ||
*/ | ||
function transformCallExpressionToLiteralType(options) { | ||
const { sourceFile, needle, necessaryType, importModuleName, leadingString, trailingString, saveFile, } = options; | ||
const { sourceFile, needle, resultType, importModuleName, leadingString, trailingString, } = options; | ||
const ret = new Map(); | ||
const insertedNum = importModuleName | ||
? hasImportNecessaryType(sourceFile, [necessaryType], importModuleName) | ||
? hasImportNecessaryType(sourceFile, [resultType], importModuleName) | ||
: 0; | ||
@@ -110,5 +147,7 @@ const expressions = findCallExpressionsByName(sourceFile, needle); | ||
needle, | ||
necessaryType, | ||
resultType, | ||
}; | ||
const obj = processExpression(opts); | ||
const varname = retrieveVarnameFromCallExpression(express); | ||
const obj = genLiteralObjectFromExpression(opts); | ||
ret.set(varname, obj); | ||
const jsonCode = leadingString | ||
@@ -123,10 +162,11 @@ + JSON.stringify(obj, null, 2) | ||
// const ft2 = sourceFile.getFullText() | ||
if (saveFile) { | ||
sourceFile.saveSync(); | ||
} | ||
// if (saveFile) { | ||
// sourceFile.saveSync() | ||
// } | ||
return ret; | ||
} | ||
function processExpression(options) { | ||
const { file, express, needle, necessaryType, } = options; | ||
function genLiteralObjectFromExpression(options) { | ||
const { file, express, needle, resultType, } = options; | ||
const ret = {}; | ||
const doName = retrieveTypeArgmentFromCallExpression(express); | ||
const doName = retrieveFirstTypeArgTextFromCallExpression(express); | ||
if (!doName) { | ||
@@ -137,3 +177,3 @@ // throw new Error(`Parameter D of ${AstKey.genDbDict}<D>() missing`) | ||
const aliasName = 'T' + Math.random().toString().slice(-5); | ||
file.addStatements(`type ${aliasName} = ${necessaryType}<${doName}>`); | ||
file.addStatements(`type ${aliasName} = ${resultType}<${doName}>`); | ||
// const ft = file.getFullText() | ||
@@ -212,7 +252,42 @@ const aliasDec = file.getTypeAlias(aliasName); | ||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
function createObjectLiteralExpression(input) { | ||
const arr = Object.entries(input).map(([key, value]) => { | ||
if (Array.isArray(value)) { | ||
throw new TypeError('property value not literal object, but array. key: ' + key); | ||
} | ||
else if (typeof value === 'string') { | ||
const node = createPropertyAssignmentOfString(key, value); | ||
return node; | ||
} | ||
else if (typeof value === 'object' && Object.keys(value).length) { | ||
const node = createPropertyAssignmentOfObject(key, value); | ||
return node; | ||
} | ||
throw new TypeError('property value not literal object. key: ' + key); | ||
}); | ||
const ret = ts__default['default'].factory.createObjectLiteralExpression(arr, true); | ||
return ret; | ||
} | ||
function createPropertyAssignmentOfString(key, value) { | ||
const ret = ts__default['default'].factory.createPropertyAssignment(ts__default['default'].factory.createIdentifier(key), ts__default['default'].factory.createStringLiteral(value)); | ||
return ret; | ||
} | ||
function createPropertyAssignmentOfObject(key, value) { | ||
const id = ts__default['default'].factory.createIdentifier(key); | ||
const expression = createObjectLiteralExpression(value); | ||
const arr = ts__default['default'].factory.createPropertyAssignment(id, expression); | ||
return arr; | ||
} | ||
exports.createObjectLiteralExpression = createObjectLiteralExpression; | ||
exports.createSourceFile = createSourceFile; | ||
exports.findCallExpressionsByName = findCallExpressionsByName; | ||
exports.hasImportNecessaryType = hasImportNecessaryType; | ||
exports.retrieveTypeArgmentFromCallExpression = retrieveTypeArgmentFromCallExpression; | ||
exports.isTypeImported = isTypeImported; | ||
exports.retrieveFirstTypeArgTextFromCallExpression = retrieveFirstTypeArgTextFromCallExpression; | ||
exports.retrieveTypeArgsFromCallExpression = retrieveTypeArgsFromCallExpression; | ||
exports.retrieveVarnameFromCallExpression = retrieveVarnameFromCallExpression; | ||
exports.retrieveVarnameFromVariableDeclaration = retrieveVarnameFromVariableDeclaration; | ||
exports.transformCallExpressionToLiteralType = transformCallExpressionToLiteralType; | ||
//# sourceMappingURL=index.cjs.js.map |
@@ -1,2 +0,3 @@ | ||
export * from './common'; | ||
export { transformCallExpressionToLiteralType, } from './tpl-literal'; | ||
export * from './ts-morph/common'; | ||
export { transformCallExpressionToLiteralType, } from './ts-morph/tpl-literal'; | ||
export { createObjectLiteralExpression } from './ts/common'; |
{ | ||
"name": "@waiting/shared-types-dev", | ||
"author": "waiting", | ||
"version": "7.4.0", | ||
"version": "8.0.0", | ||
"description": "shared typescript types devel", | ||
@@ -28,7 +28,7 @@ "private": false, | ||
"dependencies": { | ||
"@waiting/shared-types": "^7.4.0", | ||
"@waiting/shared-types": "^8.0.0", | ||
"ts-morph": "10" | ||
}, | ||
"devDependencies": { | ||
"@waiting/shared-core": "^7.4.0", | ||
"@waiting/shared-core": "^8.0.0", | ||
"cross-env": "7" | ||
@@ -91,3 +91,3 @@ }, | ||
}, | ||
"gitHead": "655b95354bbaa8c785ca455a44a42a2acb834ace" | ||
"gitHead": "a4c5a19b23baee4ef2eceb5ecb50dfc61ce70195" | ||
} |
export * from './common' | ||
export * from './ts-morph/common' | ||
export { | ||
@@ -7,3 +7,5 @@ ProcessExpressionOptions, | ||
transformCallExpressionToLiteralType, | ||
} from './tpl-literal' | ||
} from './ts-morph/tpl-literal' | ||
export { createObjectLiteralExpression } from './ts/common' | ||
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
43588
17
875
1
+ Added@waiting/shared-types@8.6.0(transitive)
- Removed@waiting/shared-types@7.4.0(transitive)
Updated@waiting/shared-types@^8.0.0