Skip to content

Commit

Permalink
parser updated, replaceFile test added
Browse files Browse the repository at this point in the history
  • Loading branch information
oyilmaztekin committed Nov 3, 2018
1 parent 8fcf26b commit 09b8bb6
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 118 deletions.
98 changes: 49 additions & 49 deletions lib/astStages.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ AstStages.prototype.parseToAst = function(file) {
]
})
return parsedFile
}
}

AstStages.prototype.transformToAst = function(ast){
const traverse = require('babel-traverse').default
Expand All @@ -37,21 +37,21 @@ AstStages.prototype.transformToAst = function(ast){
const node = path.node
const params = node.params
const blockScope = node.body.body

let fn = {
"params": [],
"name": null,
"returnedValue": null,
"loc": null,
"flow":false
}
}
fn.name = node.id.name
fn.loc = node.id.loc

if ( params.length > 0 ) {
Transformers.checkParameterTypeForFlow(params, fn)
}

for( let b in blockScope ){
const activeScope = blockScope[b]
if ( !node.hasOwnProperty('returnType') ) {
Expand All @@ -61,15 +61,15 @@ AstStages.prototype.transformToAst = function(ast){
Transformers.checkReturnedTypeForFlow(node, fn)
}
}

let processedTemplate = AstStages.prototype.createDocTemplate(fn)
let leadComment = [
let leadComment = [
{
type:"CommentBlock",
value: processedTemplate
}
]

path.node.leadingComments = leadComment

} catch (Error) {
Expand All @@ -81,19 +81,19 @@ AstStages.prototype.transformToAst = function(ast){

/**
* @summary Traverses Flow Type Interfaces and collects their all properties.
* @param {object} path - node tree with its sub branches and its parent properties.
* @param {object} path - node tree with its sub branches and its parent properties.
*/

TypeAlias:function ( path ) {

const node = path.node
try {
if ( node.hasOwnProperty('leadingComments') ) {
return
}
const typeName = node.id.name
const props = path.node.right.properties

let fn = {
"params":[],
"name": typeName,
Expand All @@ -118,22 +118,22 @@ AstStages.prototype.transformToAst = function(ast){
}
)
}

}

if ( p.value.hasOwnProperty('types') ) {
// FIXME: fix multiple propname : type problem
for (let t of p.value.types) {
if ( t.hasOwnProperty('id') ) {
fn.params.push(
fn.params.push(
{
"name": p.key.name,
"type": t.id.id.name
}
)
}
else {
fn.params.push(
fn.params.push(
{
"name": p.key.name,
"type": t.type
Expand All @@ -143,24 +143,24 @@ AstStages.prototype.transformToAst = function(ast){
}
}
}

let processedTemplate = AstStages.prototype.createDocTemplate(fn)
let leadComment = [

let leadComment = [
{
type:"CommentBlock",
value: processedTemplate
}
]

path.node.leadingComments = leadComment


} catch ( Error ) {
console.error("\x1b[0m \x1b[31m", "Traverser get exception on the AST") // eslint-disable-line
console.error("\x1b[0m \x1b[31m", `${Error}`) // eslint-disable-line
throw Error

}
},

Expand All @@ -175,19 +175,19 @@ AstStages.prototype.transformToAst = function(ast){
"states": [],
"returnedValue":null,
"flow": false
}
let node = path.node
}

let node = path.node

if ( node.key.name !== "render" ) {
fn.name = node.key.name
fn.loc = node.loc
const params = node.params

Transformers.checkParameterTypeForFlow( params, fn )

const blockScope = node.body.body

for( let b in blockScope ) {
const activeScope = blockScope[b]

Expand All @@ -198,36 +198,36 @@ AstStages.prototype.transformToAst = function(ast){
else {
Transformers.checkReturnedTypeForFlow(node, fn)
}

const isMemberAssignmentObject = activeScope.expression

Transformers.checkStatesForJSFiles( isMemberAssignmentObject, fn )

}

let processedTemplate = AstStages.prototype.createDocTemplate(fn)
let leadComment = [
let leadComment = [
{
type:"CommentBlock",
value: processedTemplate
}
]

path.node.leadingComments = leadComment
}
} catch ( Error ) {
console.error("\x1b[0m \x1b[31m", "Traverser get exception on the AST") // eslint-disable-line
console.error("\x1b[0m \x1b[31m", `${Error}`) // eslint-disable-line
throw Error

}
}
})
return ast
}

AstStages.prototype.generateCode = function(parsedAst) {
const babelGenerator = require('babel-generator');
const babelGenerator = require('@babel/generator');

let generatedCode = "";
let outputFunc= babelGenerator.default(parsedAst, generatedCode);
Expand All @@ -237,8 +237,8 @@ AstStages.prototype.generateCode = function(parsedAst) {

AstStages.prototype.replaceFile = function(file, generatedCode) {
const code = generatedCode.code
fs.writeFileSync(file, code);
fs.writeFileSync(file, code);

return {
"file":file,
"generatedCode": generatedCode
Expand All @@ -252,32 +252,32 @@ AstStages.prototype.createDocTemplate = function(fn){
let stateLiteral = "";
let returnLiteral = "";
let templateLiteral;

if ( fn.params.length > 0 ) {
for ( let p of fn.params ) {
let param = p.type.replace(/TypeAnnotation/gm, "")
paramsLiteral +=
`* @param {${param}} ${p.name} - [description] \n`
}
}
}
if (
fn.hasOwnProperty("states")
&&
fn.states.length > 0
if (
fn.hasOwnProperty("states")
&&
fn.states.length > 0
) {
for ( let s of fn.states ) {
stateLiteral += ` * @argument {type} ${s}  - [description] \n`
}
}
if ( fn.returnedValue ) {
fn.flow ? returnLiteral = ` * @return {${fn.returnedValue}} - [description] \n `
: returnLiteral = ` * @return {type} ${fn.returnedValue} - [description] \n`
: returnLiteral = ` * @return {type} ${fn.returnedValue} - [description] \n`
}


templateLiteral = header.concat(paramsLiteral, stateLiteral, returnLiteral)

return templateLiteral
}

exports.AstStages = AstStages
exports.AstStages = AstStages
58 changes: 1 addition & 57 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
},
"dependencies": {
"@babel/core": "^7.1.2",
"@babel/preset-env": "^7.1.0",
"@babel/generator": "^7.1.3",
"@babel/parser": "^7.1.3",
"@babel/plugin-proposal-async-generator-functions": "^7.1.0",
"@babel/plugin-proposal-class-properties": "^7.1.0",
"@babel/plugin-proposal-object-rest-spread": "^7.0.0",
Expand All @@ -59,10 +60,9 @@
"@babel/plugin-transform-object-super": "^7.1.0",
"@babel/plugin-transform-react-display-name": "^7.0.0",
"@babel/plugin-transform-react-jsx": "^7.0.0",
"@babel/preset-env": "^7.1.0",
"@babel/preset-flow": "^7.0.0",
"@babel/preset-react": "^7.0.0",
"@babel/parser": "^7.1.3",
"babel-traverse": "^6.26.0",
"babel-generator": "^6.26.1"
"babel-traverse": "^6.26.0"
}
}
Loading

0 comments on commit 09b8bb6

Please sign in to comment.