From d36dfe3f0e92ef705397165d2b2c6141753f3b3d Mon Sep 17 00:00:00 2001 From: Nikolaus Piccolotto Date: Fri, 16 Oct 2015 19:14:13 +0200 Subject: [PATCH] #303 normalize code a bit, more concise code --- .gitignore | 1 + client/codemods/unflummox.js | 96 --------------------- client/lib/yourturn/src/search/search.jsx | 8 +- client/lib/yourturn/src/sidebar/sidebar.jsx | 10 ++- codemods/unflummox/test.jsx | 28 ++++++ codemods/unflummox/unflummox.js | 96 +++++++++++++++++++++ 6 files changed, 136 insertions(+), 103 deletions(-) delete mode 100644 client/codemods/unflummox.js create mode 100644 codemods/unflummox/test.jsx create mode 100644 codemods/unflummox/unflummox.js diff --git a/.gitignore b/.gitignore index a5616f6f..ccdd87b1 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ scm-source.json *.log stats.json +.DS_Store \ No newline at end of file diff --git a/client/codemods/unflummox.js b/client/codemods/unflummox.js deleted file mode 100644 index d049770d..00000000 --- a/client/codemods/unflummox.js +++ /dev/null @@ -1,96 +0,0 @@ -function camelCase(word) { - return word[0].toUpperCase() + word.substring(1); -} - -module.exports = function(file, api) { - if (/^router/.test(file.path)) { - // do not touch routers here - return; - } - if (/test.js$/.test(file.path)) { - // also do not touch tests - return; - } - - let j = api.jscodeshift, - result = j(file.source); - - // this.store = props.flux.getStore('user') => this.store = props.userStore - // this.stores = { 'user' : props.flux.getStore('user') } => this.stores = { 'user': props.userStore } - result - .find(j.Expression) - .forEach(expr => { - let {value} = expr; - // if we encounter props.flux.getStore(x) - // => constructor ? props.xStore : this.props.xStore - - if (value.type === 'AssignmentExpression' && - value.left.type === 'MemberExpression' && - value.left.object.type === 'ThisExpression' && - /stores?/.test(value.left.property.name)) { - - // we're accessing this.stores or this.store - - if (value.right.type === 'ObjectExpression') { - // object on the right - let obj = value.right; - obj.properties.forEach(prop => { - // accessing props.flux.getStore - if (prop.value.type === 'CallExpression' && - prop.value.callee.type === 'MemberExpression' && - prop.value.callee.object.object.name === 'props' && - prop.value.callee.object.property.name === 'flux' && - prop.value.callee.property.name === 'getStore') { - - let name = prop.value.arguments[0].value + 'Store'; - prop.value = j.memberExpression(j.identifier('props'), j.identifier(name)); - } - }) - } else if (value.right.type === 'CallExpression' && - value.right.callee.type === 'MemberExpression' && - value.right.callee.object.object.name === 'props' && - value.right.callee.object.property.name === 'flux' && - value.right.callee.property.name === 'getStore') { - // call on right side - let name = value.right.arguments[0].value + 'Store'; - value.right = j.memberExpression(j.identifier('props'), j.identifier(name)); - } - - - } - }); - - // if we encounter this.stores.x.getSomething(y) - // => add xGetters to imports - // => xGetters.getSometing(this.props.xStore, y) - - // this.action = props.flux.getActions('user') => delete - result - .find(j.AssignmentExpression) - .filter(assignment => assignment.value.right.type === 'CallExpression') - .find(j.CallExpression) - .filter(call => call.value.callee.type === 'MemberExpression') - .filter(call => call.value.callee.property.name === 'getActions') - .closest(j.AssignmentExpression) - .remove(); - - // flux.getActions('blahr').foo() => blahrActionsFoo() - result - .find(j.MemberExpression) - .filter(member => member.value.property.name === 'getActions') - .closest(j.CallExpression) - .filter(call => call.parentPath.value.type === 'MemberExpression') - .map(call => call.parentPath.parentPath) - .replaceWith(expr => { - let method = expr.value.callee.property.name, - args = expr.value.arguments, - actions = expr.value.callee.object.arguments[0].value; - return j.callExpression( - j.memberExpression( - j.memberExpression(j.thisExpression(), j.identifier('props')), - j.identifier(actions + 'Actions' + camelCase(method))), - args); - }); - - return result.toSource(); -}; diff --git a/client/lib/yourturn/src/search/search.jsx b/client/lib/yourturn/src/search/search.jsx index 99fddf0e..45d2d0b5 100644 --- a/client/lib/yourturn/src/search/search.jsx +++ b/client/lib/yourturn/src/search/search.jsx @@ -7,7 +7,9 @@ import 'common/asset/less/yourturn/search.less'; class Search extends React.Component { constructor(props) { super(); - this.store = props.flux.getStore('search'); + this.stores = { + search: props.flux.getStore('search') + }; this.actions = props.flux.getActions('search'); this.state = { term: '' @@ -25,7 +27,7 @@ class Search extends React.Component { if (!term.length) { this.actions.clearSearchResults(term); } else { - if (this.store.hasResults(term)) { + if (this.stores.search.hasResults(term)) { this.actions.clearSearchResults(term); } Object @@ -41,7 +43,7 @@ class Search extends React.Component { render() { let {term} = this.state, - results = this.store.getSearchResults(term); + results = this.stores.search.getSearchResults(term); return

Search

diff --git a/client/lib/yourturn/src/sidebar/sidebar.jsx b/client/lib/yourturn/src/sidebar/sidebar.jsx index d99fb8a7..a58d25ae 100644 --- a/client/lib/yourturn/src/sidebar/sidebar.jsx +++ b/client/lib/yourturn/src/sidebar/sidebar.jsx @@ -9,7 +9,9 @@ class Sidebar extends React.Component { constructor(props) { super(); this.actions = props.flux.getActions('user'); - this.store = props.flux.getStore('user'); + this.stores = { + user: props.flux.getStore('user') + }; this.interval = false; this.state = { isTokenValid: true @@ -30,7 +32,7 @@ class Sidebar extends React.Component { } updateExpiryDate() { - let tokeninfo = this.store.getTokenInfo(), + let tokeninfo = this.stores.user.getTokenInfo(), NOW = Date.now(); this.setState({ currentDate: NOW, // to enforce state change @@ -51,8 +53,8 @@ class Sidebar extends React.Component { } render() { - let tokeninfo = this.store.getTokenInfo(), - userinfo = this.store.getUserInfo(), + let tokeninfo = this.stores.user.getTokenInfo(), + userinfo = this.stores.user.getUserInfo(), {router} = this.context; return