Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add transform to fix up misuse of arguments in arrow functions.
- Loading branch information
Showing
4 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
'use strict'; | ||
|
||
var fn1 = () => console.log(arguments); | ||
|
||
var fn2 = (a, b, c) => { | ||
console.log(arguments); | ||
|
||
return arguments; | ||
}; | ||
|
||
var fn3 = (a, b, args) => { | ||
console.log(args); | ||
|
||
return arguments; | ||
}; | ||
|
||
var fn4 = function(a, b, c) { | ||
console.log(arguments); | ||
var fn5 = () => arguments; | ||
var fn6 = () => (function() { return arguments; }); | ||
class A { | ||
constructor() { | ||
console.log(arguments); | ||
} | ||
} | ||
}; | ||
|
||
var fn5 = (a, ...b) => arguments; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
'use strict'; | ||
|
||
var fn1 = (...args) => console.log(args); | ||
|
||
var fn2 = (a, b, c, ...args) => { | ||
console.log([a, b, c, ...args]); | ||
|
||
return [a, b, c, ...args]; | ||
}; | ||
|
||
var fn3 = (a, b, args) => { | ||
console.log(args); | ||
|
||
return arguments; | ||
}; | ||
|
||
var fn4 = function(a, b, c) { | ||
console.log(arguments); | ||
var fn5 = (...args) => args; | ||
var fn6 = () => (function() { return arguments; }); | ||
class A { | ||
constructor() { | ||
console.log(arguments); | ||
} | ||
} | ||
}; | ||
|
||
var fn5 = (a, ...b) => [a, ...b]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// ----------------------------------------------------------------------------- | ||
// Update arrow function arguments to args | ||
function arrowFunctionArguments(file, api, options) { | ||
const j = api.jscodeshift; | ||
|
||
const printOptions = options.printOptions || {quote: 'single'}; | ||
const root = j(file.source); | ||
|
||
const ARGUMENTS = 'arguments'; | ||
const ARGS = 'args'; | ||
|
||
const createArrowFunctionExpression = (fn, args) => { | ||
const arrow = j.arrowFunctionExpression( | ||
(fn.params || []).concat( | ||
// https://github.com/benjamn/recast/pull/179 | ||
// j.spreadElementPattern(args) | ||
), | ||
fn.body, | ||
fn.generator | ||
); | ||
arrow.rest = args; | ||
return arrow; | ||
}; | ||
|
||
const filterArrowFunctions = path => { | ||
while (path.parent) { | ||
switch (path.value.type) { | ||
case 'ArrowFunctionExpression': | ||
if (j(path).find(j.Identifier, {name: ARGS}).size()) { | ||
console.error( | ||
file.path + ': arrow function uses "' + ARGS + '" already. ' + | ||
'Please rename this identifier first.' | ||
); | ||
return false; | ||
} | ||
return true; | ||
case 'FunctionExpression': | ||
case 'MethodDeclaration': | ||
case 'Function': | ||
case 'FunctionDeclaration': | ||
return false; | ||
default: | ||
break; | ||
} | ||
path = path.parent; | ||
} | ||
return false; | ||
}; | ||
|
||
const updateArgumentsCalls = path => { | ||
var afPath = path; | ||
while (afPath.parent) { | ||
if (afPath.value.type == 'ArrowFunctionExpression') { | ||
break; | ||
} | ||
afPath = afPath.parent; | ||
} | ||
|
||
const {value: fn} = afPath; | ||
const {params} = fn; | ||
const args = fn.rest || j.identifier(ARGS); | ||
j(afPath).replaceWith(createArrowFunctionExpression(fn, args)); | ||
|
||
if (params.length) { | ||
j(path).replaceWith( | ||
j.arrayExpression(params.concat( | ||
j.spreadElement(args) | ||
)) | ||
); | ||
} else { | ||
j(path).replaceWith(args); | ||
} | ||
}; | ||
|
||
const didTransform = root | ||
.find(j.Identifier, {name: ARGUMENTS}) | ||
.filter(filterArrowFunctions) | ||
.forEach(updateArgumentsCalls) | ||
.size() > 0; | ||
|
||
return didTransform ? root.toSource(printOptions) + '\n' : null; | ||
} | ||
|
||
module.exports = arrowFunctionArguments; |