Skip to content

Commit

Permalink
Fix array pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
Bogdan-Lyashenko committed Nov 15, 2017
1 parent e3dafab commit d9437ea
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 28 deletions.
38 changes: 25 additions & 13 deletions dist/js2flowchart.js
Original file line number Diff line number Diff line change
Expand Up @@ -1309,6 +1309,7 @@ var TOKEN_TYPES = exports.TOKEN_TYPES = {
SPREAD_PROPERTY: 'SpreadProperty',
REST_PROPERTY: 'RestProperty',
OBJECT_PATTERN: 'ObjectPattern',
ARRAY_PATTERN: 'ArrayPattern',
ASSIGNMENT_PATTERN: 'AssignmentPattern'
};

Expand Down Expand Up @@ -4432,6 +4433,13 @@ var DefinitionsMap = exports.DefinitionsMap = (_DefinitionsMap = {}, _defineProp
return path.listKey === 'params' || [_constants.TOKEN_TYPES.VARIABLE_DECLARATOR, _constants.TOKEN_TYPES.ASSIGNMENT_PATTERN].includes(path.parent.type);
},
body: true
}), _defineProperty(_DefinitionsMap, _constants.TOKEN_TYPES.ARRAY_PATTERN, {
type: _constants.TOKEN_TYPES.ARRAY_PATTERN,
getName: _Harmony.arrayPatternConverter,
ignore: function ignore(path) {
return path.listKey === 'params' || [_constants.TOKEN_TYPES.VARIABLE_DECLARATOR, _constants.TOKEN_TYPES.ASSIGNMENT_PATTERN].includes(path.parent.type);
},
body: true
}), _DefinitionsMap);

var DefinitionsList = exports.DefinitionsList = Object.values(DefinitionsMap);
Expand Down Expand Up @@ -16681,7 +16689,14 @@ var variableDeclaratorConverter = exports.variableDeclaratorConverter = function
return parentKind + ' ' + node.id.name + ' = ';
}

var variableName = node.id.type === _constants.TOKEN_TYPES.OBJECT_PATTERN ? '{...}' : node.id.name;
var variableName = '';
if (node.id.type === _constants.TOKEN_TYPES.OBJECT_PATTERN) {
variableName = '{...}';
} else if (node.id.type === _constants.TOKEN_TYPES.ARRAY_PATTERN) {
variableName = '[...]';
} else {
variableName = node.id.name;
}

if (node.init && [_constants.TOKEN_TYPES.CALL_EXPRESSION, _constants.TOKEN_TYPES.NEW_EXPRESSION].includes(node.init.type)) {
return parentKind + ' ' + variableName + ' = ' + callExpressionConverter({ node: node.init });
Expand All @@ -16695,6 +16710,10 @@ var variableDeclaratorConverter = exports.variableDeclaratorConverter = function
return parentKind + ' {...} = ' + node.init.name;
}

if (node.id && node.id.type === _constants.TOKEN_TYPES.ARRAY_PATTERN) {
return parentKind + ' [...] = ' + node.init.name;
}

return parentKind + ' ' + (0, _babelGenerator2.default)(node).code;
};

Expand Down Expand Up @@ -16789,22 +16808,11 @@ var isFunctionType = exports.isFunctionType = function isFunctionType(type) {
return [_constants.TOKEN_TYPES.FUNCTION_EXPRESSION, _constants.TOKEN_TYPES.FUNCTION, _constants.TOKEN_TYPES.ARROW_FUNCTION_EXPRESSION, _constants.TOKEN_TYPES.FUNCTION_DECLARATION].includes(type);
};

//TODO: node.properties, case when function is property.value of object
/* c = {
a: function (b) {
list.push(b.id);
}
};*/
var isNodeContainsFunc = exports.isNodeContainsFunc = function isNodeContainsFunc(node) {
var functions = [_constants.TOKEN_TYPES.ARROW_FUNCTION_EXPRESSION, _constants.TOKEN_TYPES.FUNCTION_EXPRESSION];

return node && functions.indexOf(node.type) !== -1;
};
//TODO: render arrow function/anonymous function on the same Y position shifted to the right.
//const func = (c,d)=> {c++; d++;}
//|func| |(c,d)=>|
// |c++|
// |d++|

/***/ }),
/* 167 */
Expand Down Expand Up @@ -37627,7 +37635,7 @@ function JSXEmptyExpression() {}
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.objectPatternConverter = exports.classDeclarationConverter = exports.exportDefaultDeclarationConverter = exports.exportNamedDeclarationConverter = exports.importDeclarationConverter = undefined;
exports.arrayPatternConverter = exports.objectPatternConverter = exports.classDeclarationConverter = exports.exportDefaultDeclarationConverter = exports.exportNamedDeclarationConverter = exports.importDeclarationConverter = undefined;

var _babelGenerator = __webpack_require__(167);

Expand Down Expand Up @@ -37696,6 +37704,10 @@ var objectPatternConverter = exports.objectPatternConverter = function objectPat
return '{...}';
};

var arrayPatternConverter = exports.arrayPatternConverter = function arrayPatternConverter() {
return '[...]';
};

/***/ }),
/* 445 */
/***/ (function(module, exports, __webpack_require__) {
Expand Down
2 changes: 1 addition & 1 deletion dist/js2flowchart.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "js2flowchart",
"version": "1.0.2",
"version": "1.0.3",
"author": "Bohdan Liashenko",
"license": "MIT",
"repository": {
Expand Down
2 changes: 2 additions & 0 deletions src/builder/converters/Harmony.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,5 @@ export const classDeclarationConverter = ({ node }) => {
};

export const objectPatternConverter = () => '{...}';

export const arrayPatternConverter = () => '[...]';
24 changes: 12 additions & 12 deletions src/builder/converters/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,14 @@ export const variableDeclaratorConverter = path => {
return `${parentKind} ${node.id.name} = `;
}

const variableName = (node.id.type === TOKEN_TYPES.OBJECT_PATTERN) ? '{...}' : node.id.name;
let variableName = '';
if (node.id.type === TOKEN_TYPES.OBJECT_PATTERN) {
variableName = '{...}';
} else if (node.id.type === TOKEN_TYPES.ARRAY_PATTERN) {
variableName = '[...]';
} else {
variableName = node.id.name;
}

if (
node.init &&
Expand All @@ -186,6 +193,10 @@ export const variableDeclaratorConverter = path => {
return `${parentKind} {...} = ${node.init.name}`;
}

if (node.id && node.id.type === TOKEN_TYPES.ARRAY_PATTERN) {
return `${parentKind} [...] = ${node.init.name}`;
}

return parentKind + ' ' + generate(node).code;
};

Expand Down Expand Up @@ -292,19 +303,8 @@ export const isFunctionType = type => {
].includes(type);
};

//TODO: node.properties, case when function is property.value of object
/* c = {
a: function (b) {
list.push(b.id);
}
};*/
export const isNodeContainsFunc = node => {
const functions = [TOKEN_TYPES.ARROW_FUNCTION_EXPRESSION, TOKEN_TYPES.FUNCTION_EXPRESSION];

return node && functions.indexOf(node.type) !== -1;
};
//TODO: render arrow function/anonymous function on the same Y position shifted to the right.
//const func = (c,d)=> {c++; d++;}
//|func| |(c,d)=>|
// |c++|
// |d++|
16 changes: 15 additions & 1 deletion src/builder/entryDefinitionsMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ import {
exportNamedDeclarationConverter,
exportDefaultDeclarationConverter,
classDeclarationConverter,
objectPatternConverter
objectPatternConverter,
arrayPatternConverter
} from './converters/Harmony';

const singleTypeFilter = path => {
Expand Down Expand Up @@ -302,6 +303,19 @@ export const DefinitionsMap = {
);
},
body: true
},
[TOKEN_TYPES.ARRAY_PATTERN]: {
type: TOKEN_TYPES.ARRAY_PATTERN,
getName: arrayPatternConverter,
ignore: path => {
return (
path.listKey === 'params' ||
[TOKEN_TYPES.VARIABLE_DECLARATOR, TOKEN_TYPES.ASSIGNMENT_PATTERN].includes(
path.parent.type
)
);
},
body: true
}
};

Expand Down
1 change: 1 addition & 0 deletions src/shared/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export const TOKEN_TYPES = {
SPREAD_PROPERTY: 'SpreadProperty',
REST_PROPERTY: 'RestProperty',
OBJECT_PATTERN: 'ObjectPattern',
ARRAY_PATTERN: 'ArrayPattern',
ASSIGNMENT_PATTERN: 'AssignmentPattern'
};

Expand Down

1 comment on commit d9437ea

@troybryant1984
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PQS = '&fs=${start}'
PLAYER_URL = 'http

Please sign in to comment.