javascript2typescript
Advanced tools
Comparing version 0.1.4 to 0.2.0
#!/usr/bin/env node | ||
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
@@ -32,4 +33,5 @@ }); | ||
const arg = `${args._}`; | ||
const files = glob.sync(/.js$/.test(arg) ? arg : `${arg}/**`).filter(file => /.js$/.test(file)); | ||
aigle_1.default.eachLimit(files, limit, (file) => __awaiter(this, void 0, void 0, function* () { | ||
const re = /.js$/; | ||
const files = glob.sync(re.test(arg) ? arg : `${arg}/**`).filter(file => re.test(file)); | ||
aigle_1.default.eachLimit(files, limit, (file) => __awaiter(void 0, void 0, void 0, function* () { | ||
console.log(`Converting... ${file}`); | ||
@@ -36,0 +38,0 @@ const command = `J2T_DEFAULT_EXPORT=${defaultStr} ${hookpath} --require ${indexpath} ${file}`; |
@@ -12,3 +12,2 @@ "use strict"; | ||
prettier_hook_1.hooks.babylon.addHook(parse); | ||
prettier_hook_1.hooks.typescript.addHook(parse); | ||
//# sourceMappingURL=index.js.map |
@@ -7,18 +7,33 @@ "use strict"; | ||
function resolve(node) { | ||
new prettier_hook_1.Ast().set('ClassDeclaration', resolveInstanceVariables).resolveAst(node); | ||
const staticMap = new Map(); | ||
const instanceMap = new Map(); | ||
new prettier_hook_1.Ast() | ||
.set('ClassDeclaration', (node, key, ast) => { | ||
const { name } = node[key].id; | ||
staticMap.set(name, new Map()); | ||
instanceMap.set(name, new Map()); | ||
return ast.super(node, key); | ||
}) | ||
.set('AssignmentExpression', (node, key) => checkAssignmentVariables(node, key, staticMap, instanceMap)) | ||
.resolveAst(node); | ||
// define class properties | ||
new prettier_hook_1.Ast() | ||
.set('ClassDeclaration', (node, key) => { | ||
const { name } = node[key].id; | ||
return resolveClassProperties(node, key, staticMap.get(name), instanceMap.get(name)); | ||
}) | ||
.resolveAst(node); | ||
} | ||
exports.resolve = resolve; | ||
const classMethod = Symbol('classMethod'); | ||
function resolveInstanceVariables(node, key) { | ||
const staticPropSymbolMap = new Map(); | ||
const instancePropSymbolMap = new Map(); | ||
function resolveClassProperties(node, key, staticPropMap, instancePropMap) { | ||
let propMap; | ||
const tree = node[key]; | ||
checkMethods(tree, staticPropSymbolMap, instancePropSymbolMap); | ||
checkMethods(tree, staticPropMap, instancePropMap); | ||
new prettier_hook_1.Ast() | ||
.set('ClassMethod', (node, key, ast) => { | ||
propMap = node[key].static ? staticPropSymbolMap : instancePropSymbolMap; | ||
propMap = node[key].static ? staticPropMap : instancePropMap; | ||
return ast.super(node, key); | ||
}) | ||
.set('AssignmentExpression', (node, key, ast) => { | ||
.set('AssignmentExpression', (node, key) => { | ||
const { left, right } = node[key]; | ||
@@ -28,8 +43,3 @@ if (!new prettier_hook_1.Ast().set('ThisExpression', () => true).resolveAst(left)) { | ||
} | ||
const name = util_1.get(left, ['object', 'property', 'name']) || util_1.get(left, ['property', 'name']); | ||
if (!name) { | ||
console.error(JSON.stringify(left, null, 4)); | ||
throw new Error('name not found'); | ||
} | ||
types_1.setTypeToPropMap(name, propMap, right.type); | ||
types_1.setTypeToPropMap(getAssignmentName(left), propMap, right.type); | ||
return true; | ||
@@ -39,7 +49,7 @@ }) | ||
tree.body = tree.body || { type: 'ClassBody', body: [] }; | ||
assignPropMap(tree, instancePropSymbolMap, false); | ||
assignPropMap(tree, staticPropSymbolMap, true); | ||
assignPropMap(tree, instancePropMap, false); | ||
assignPropMap(tree, staticPropMap, true); | ||
return true; | ||
} | ||
function checkMethods(tree, staticPropSymbolMap, instancePropSymbolMap) { | ||
function checkMethods(tree, staticPropMap, instancePropMap) { | ||
let propMap; | ||
@@ -49,3 +59,3 @@ new prettier_hook_1.Ast() | ||
const tree = node[key]; | ||
propMap = tree.static ? staticPropSymbolMap : instancePropSymbolMap; | ||
propMap = tree.static ? staticPropMap : instancePropMap; | ||
propMap.set(tree.key.name, classMethod); | ||
@@ -56,2 +66,25 @@ return true; | ||
} | ||
function checkAssignmentVariables(node, key, staticPropMap, instancePropMap) { | ||
return new prettier_hook_1.Ast() | ||
.set('AssignmentExpression', (tree, key) => { | ||
let { left, right } = tree[key]; | ||
const prototype = util_1.get(left, ['object', 'property', 'name']) === 'prototype'; | ||
const className = util_1.get(left, prototype ? ['object', 'object', 'name'] : ['object', 'name']); | ||
const propMap = prototype ? instancePropMap.get(className) : staticPropMap.get(className); | ||
if (!propMap) { | ||
return false; | ||
} | ||
types_1.setTypeToPropMap(getAssignmentName(left), propMap, right.type); | ||
return true; | ||
}) | ||
.resolveAst(node, key); | ||
} | ||
function getAssignmentName(left) { | ||
const name = util_1.get(left, ['property', 'name']) || util_1.get(left, ['object', 'property', 'name']); | ||
if (!name) { | ||
console.error(JSON.stringify(left, null, 4)); | ||
throw new Error('name not found'); | ||
} | ||
return name; | ||
} | ||
function assignPropMap(node, propMap, staticProperty) { | ||
@@ -58,0 +91,0 @@ const props = []; |
@@ -28,2 +28,3 @@ "use strict"; | ||
if (leadingComments.length !== 0) { | ||
// TODO: find a better js doc parser | ||
const [{ value }] = leadingComments; | ||
@@ -48,2 +49,8 @@ value | ||
} | ||
new prettier_hook_1.Ast() | ||
.set('AssignmentExpression', (tree, key) => { | ||
const { left, right } = tree[key]; | ||
return resolveAssignmentExpression(left, right, paramMap, optionalMap); | ||
}) | ||
.resolveAst(node, key); | ||
for (let tree of params) { | ||
@@ -53,6 +60,3 @@ switch (tree.type) { | ||
const { left, right } = tree; | ||
types_1.setTypeToPropMap(left.name, paramMap, tree.right.type); | ||
if (right) { | ||
optionalMap.delete(left.name); | ||
} | ||
resolveAssignmentExpression(left, right, paramMap, optionalMap); | ||
tree = left; | ||
@@ -73,2 +77,10 @@ case 'Identifier': | ||
} | ||
function resolveAssignmentExpression(left, right, paramMap, optionalMap) { | ||
if (right.type === 'LogicalExpression') { | ||
return resolveAssignmentExpression(left, right.right, paramMap, optionalMap); | ||
} | ||
types_1.setTypeToPropMap(left.name, paramMap, right.type); | ||
optionalMap.delete(left.name); | ||
return true; | ||
} | ||
//# sourceMappingURL=funcs.js.map |
@@ -11,4 +11,3 @@ "use strict"; | ||
const defaultList = (process.env.J2T_DEFAULT_EXPORT || '').split(',').map(str => new RegExp(str)); | ||
// TODO: for now, it only supports top level imports, not support dynamic imports | ||
// TODO: module.export => export = xxx | ||
// it only supports top level imports, not support dynamic imports | ||
function resolveImport(node) { | ||
@@ -39,2 +38,6 @@ const body = util_1.get(node, ['program', 'body']); | ||
} | ||
const [source] = node[key].expression.arguments; | ||
if (isJson(source)) { | ||
return false; | ||
} | ||
/* | ||
@@ -68,2 +71,6 @@ * require('fs'); | ||
if (declarator.id.type === 'ObjectPattern') { | ||
const [source] = declarator.init.arguments; | ||
if (isJson(source)) { | ||
return false; | ||
} | ||
body.splice(idx, 1, { | ||
@@ -75,3 +82,3 @@ type: 'ImportDeclaration', | ||
})), | ||
source: declarator.init.arguments[0] | ||
source | ||
}); | ||
@@ -88,2 +95,6 @@ return true; | ||
if (declarator.init.property) { | ||
const [source] = declarator.init.object.arguments; | ||
if (isJson(source)) { | ||
return false; | ||
} | ||
body.splice(idx, 1, { | ||
@@ -98,3 +109,3 @@ type: 'ImportDeclaration', | ||
], | ||
source: declarator.init.object.arguments[0] | ||
source | ||
}); | ||
@@ -104,2 +115,5 @@ return true; | ||
const [source] = declarator.init.arguments; | ||
if (isJson(source)) { | ||
return false; | ||
} | ||
const type = defaultList.find(re => re.test(source.value)) | ||
@@ -113,3 +127,3 @@ ? 'ImportDefaultSpecifier' | ||
* import * as test from 'path'; | ||
* import test from './path'; | ||
* import * as test from './path'; | ||
* | ||
@@ -142,2 +156,5 @@ * with `-d` config, | ||
} | ||
function isJson(tree) { | ||
return /.json$/.test(tree.value); | ||
} | ||
function resolveExportDefault(node) { | ||
@@ -155,9 +172,13 @@ const map = { | ||
* ↓ | ||
* export default {}; | ||
* export = {}; | ||
*/ | ||
case map.default: | ||
node.splice(key, 1, { | ||
type: 'ExportDefaultDeclaration', | ||
declaration: tree.expression.right | ||
type: 'TSExportAssignment', | ||
expression: tree.expression.right | ||
}); | ||
// node.splice(key, 1, { | ||
// type: 'ExportDefaultDeclaration', | ||
// declaration: tree.expression.right | ||
// }); | ||
break; | ||
@@ -164,0 +185,0 @@ case map.named: |
@@ -15,2 +15,6 @@ "use strict"; | ||
case 'ArrayExpression': | ||
// if elements exist, use type inference | ||
if (util_1.get(tree, ['init', 'elements', 'length'])) { | ||
return false; | ||
} | ||
tree.id.typeAnnotation = { | ||
@@ -17,0 +21,0 @@ type: 'TypeAnnotation', |
{ | ||
"name": "javascript2typescript", | ||
"version": "0.1.4", | ||
"version": "0.2.0", | ||
"main": "index.js", | ||
@@ -16,19 +16,19 @@ "repository": "git@github.com:suguru03/javascript2typescript.git", | ||
"dependencies": { | ||
"aigle": "^1.13.0", | ||
"glob": "^7.1.3", | ||
"aigle": "^1.14.1", | ||
"glob": "^7.1.6", | ||
"minimist": "^1.2.0", | ||
"prettier": "^1.16.4", | ||
"prettier-hook": "^0.4.3" | ||
"prettier": "^1.19.1", | ||
"prettier-hook": "^0.4.6" | ||
}, | ||
"devDependencies": { | ||
"@types/mocha": "^5.2.6", | ||
"@types/node": "^10.12.21", | ||
"husky": "^1.3.1", | ||
"lint-staged": "^8.1.1", | ||
"mocha": "^6.0.2", | ||
"@types/mocha": "^5.2.7", | ||
"@types/node": "^12.12.14", | ||
"husky": "^3.1.0", | ||
"lint-staged": "^9.5.0", | ||
"mocha": "^6.2.2", | ||
"mocha.parallel": "^0.15.6", | ||
"ts-node": "^8.0.3", | ||
"ts-publisher": "^0.1.0", | ||
"typescript": "^3.3.1", | ||
"vm-agent": "^0.5.8" | ||
"ts-node": "^8.5.4", | ||
"ts-publisher": "^0.2.1", | ||
"typescript": "^3.7.2", | ||
"vm-agent": "^0.6.0" | ||
}, | ||
@@ -35,0 +35,0 @@ "husky": { |
# javascript2typescript | ||
The library helps to convert JavaScript to TypeScript. | ||
The library is a helper to convert JavaScript to TypeScript. | ||
@@ -10,3 +10,2 @@ ## Features | ||
## How to run | ||
@@ -29,1 +28,8 @@ | ||
## Options | ||
|command|description| | ||
|---|---| | ||
|--write|create TypeScript files| | ||
|--rm|remove JavaScript files| | ||
|-d|convert `export = xxx` to `export default xxx`| |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
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
44143
665
34
Updatedaigle@^1.14.1
Updatedglob@^7.1.6
Updatedprettier@^1.19.1
Updatedprettier-hook@^0.4.6