Skip to content
This repository has been archived by the owner on Sep 13, 2020. It is now read-only.

Commit

Permalink
Ignore nested arrow functions (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
timneutkens authored Jun 22, 2018
1 parent d64b215 commit 8ac03f1
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 18 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"version": "1.0.0",
"license": "MIT",
"scripts": {
"test": "npm run lint && jest",
"test": "npm run lint && npm run testonly",
"testonly": "jest",
"lint": "zeit-eslint --ext .jsx,.js .",
"lint-staged": "git diff --diff-filter=ACMRT --cached --name-only '*.js' '*.jsx' | xargs zeit-eslint"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const examples = [{ name: 'ex1', url: 'https://google.fr/' }]

export default () => (
<div>
{examples.map(example => (
<div key={example.name}>
{example.name} - {example.url}
</div>
))}
</div>
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const examples = [{ name: 'ex1', url: 'https://google.fr/' }]

export default () => (
<div>
{examples.map(example => (
<div key={example.name}>
{example.name} - {example.url}
</div>
))}
</div>
)
3 changes: 2 additions & 1 deletion transforms/__tests__/url-to-withrouter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ const fixtures = [
'with-nested-arrow-function',
'componentdidupdate',
'componentwillreceiveprops',
'first-parameter-hoc'
'first-parameter-hoc',
'url-property-not-part-of-this-props'
];

for (const fixture of fixtures) {
Expand Down
39 changes: 23 additions & 16 deletions transforms/url-to-withrouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,24 +165,31 @@ export default function transformer(file, api) {

const arrowFunctions = j(rule).find(j.ArrowFunctionExpression);
(() => {
if (arrowFunctions.length > 0) {
arrowFunctions.forEach((r) => {
if (!r.value.params || !r.value.params[0]) {
return;
}

const name = r.value.params[0].name;
const propsUrlUsage = getPropsUrlNodes(j, j(r), name);
if (propsUrlUsage.length === 0) {
return;
}

turnUrlIntoRouter(j, propsUrlUsage);
wrapDefaultExportInWithRouter();
addWithRouterImport(j, root);
});
if (arrowFunctions.length === 0) {
return;
}

arrowFunctions.forEach((r) => {
// This makes sure we don't match nested functions, only the top one
if (j(r).closest(j.Expression).length !== 0) {
return;
}

if (!r.value.params || !r.value.params[0]) {
return;
}

const name = r.value.params[0].name;
const propsUrlUsage = getPropsUrlNodes(j, j(r), name);
if (propsUrlUsage.length === 0) {
return;
}

turnUrlIntoRouter(j, propsUrlUsage);
wrapDefaultExportInWithRouter();
addWithRouterImport(j, root);
});
return;
})();

if (declaration.type === 'CallExpression') {
Expand Down

0 comments on commit 8ac03f1

Please sign in to comment.