Skip to content

Commit

Permalink
respect arrow function expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
christian-bromann committed Apr 30, 2021
1 parent 019dd0c commit e22c4eb
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 14 deletions.
17 changes: 4 additions & 13 deletions protractor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ const {
matchesSelectorExpression,
replaceCommands,
parseConfigProperties,
sanitizeAsyncCalls
sanitizeAsyncCalls,
makeAsync
} = require('./utils')

module.exports = function transformer(file, api) {
Expand Down Expand Up @@ -846,18 +847,8 @@ module.exports = function transformer(file, api) {
path.parentPath.value.type !== 'AwaitExpression' &&
[...elementGetters.keys()].map((p) => p.name).includes(path.value.property.name)
)).replaceWith((path) => {
j(path).closest(j.FunctionExpression).replaceWith(({ value, parentPath }) => {
if (
parentPath.value.kind === 'get' ||
parentPath.value.key.name === 'constructor' ||
parentPath.value.key.name.startsWith('async ')
) {
return value
}

parentPath.value.key.name = `async ${parentPath.value.key.name}`
return value
})
j(path).closest(j.FunctionExpression).replaceWith(makeAsync)
j(path).closest(j.BlockStatement).replaceWith(makeAsync)
return j.awaitExpression(path.value)
})

Expand Down
23 changes: 22 additions & 1 deletion protractor/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -479,12 +479,33 @@ function sanitizeAsyncCalls (j, root) {
))
}

function makeAsync ({ value, parentPath }) {
if (
parentPath.value.key &&
(
parentPath.value.kind === 'get' ||
parentPath.value.key.name === 'constructor' ||
parentPath.value.key.name.startsWith('async ')
)
) {
return value
}

parentPath.value.key
// FunctionExpression
? (value.async = true)
// ArrowFunctionExpression
: (parentPath.value.async = true)
return value
}

module.exports = {
isCustomStrategy,
TransformError,
getSelectorArgument,
matchesSelectorExpression,
replaceCommands,
parseConfigProperties,
sanitizeAsyncCalls
sanitizeAsyncCalls,
makeAsync
}
3 changes: 3 additions & 0 deletions test/__fixtures__/protractor/source/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ class FriendsPage extends BasePage {
this.addnameBox.setValue(user);
this.actualCount.setValue(pass);
console.log(this.searchBox);
browser.call(() => {
this.actualCount.setValue(pass);
})
return this.deleteButton.click();
}

Expand Down
3 changes: 3 additions & 0 deletions test/__fixtures__/protractor/transformed/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ class FriendsPage extends BasePage {
(await this.addnameBox).setValue(user);
(await this.actualCount).setValue(pass);
console.log(await this.searchBox);
browser.call(async () => {
(await this.actualCount).setValue(pass);
})
return (await this.deleteButton).click();
}

Expand Down

0 comments on commit e22c4eb

Please sign in to comment.