Skip to content

Commit

Permalink
better handle elements within a constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
christian-bromann committed Apr 30, 2021
1 parent 07bdc4a commit c2c4fd6
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
12 changes: 12 additions & 0 deletions protractor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,18 @@ module.exports = function transformer(file, api) {
)).replaceWith((path) => {
j(path).closest(j.FunctionExpression).replaceWith(makeAsync)
j(path).closest(j.BlockStatement).replaceWith(makeAsync)
j(path).closest(j.MethodDefinition, {
key: { name: 'constructor' }
}).forEach((p) => {
throw new TransformError('' +
`With "this.${path.value.property.name}" you are ` +
'trying to access an element within a constructor. Given that it ' +
'is not possible to run asynchronous code in this context, it ' +
'is advised to move this call into a method or getter function.',
path.value,
file
)
})
return j.awaitExpression(path.value)
})

Expand Down
11 changes: 7 additions & 4 deletions protractor/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -481,11 +481,14 @@ function sanitizeAsyncCalls (j, root) {

function makeAsync ({ value, parentPath }) {
if (
parentPath.value.key &&
parentPath.parentPath.value.kind === 'constructor' ||
(
parentPath.value.kind === 'get' ||
parentPath.value.key.name === 'constructor' ||
parentPath.value.key.name.startsWith('async ')
parentPath.value.key &&
(
parentPath.value.kind === 'get' ||
parentPath.value.key.name === 'constructor' ||
parentPath.value.key.name.startsWith('async ')
)
)
) {
return value
Expand Down
6 changes: 6 additions & 0 deletions test/__fixtures__/protractor/source/failing_constructor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class FriendsPage {
constructor() {
this.binding = $('h2.ng-binding')
this.pageLoaded = this.isClickable(this.binding);
}
}
3 changes: 2 additions & 1 deletion test/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ const frameworkTests = {
['./failing_submit.js'],
['./failing_clone.js'],
['./failing_column.js'],
['./failing_anythingProtractor.js']
['./failing_anythingProtractor.js'],
['./failing_constructor.js']
],
v7: [
['./spec.js', './spec.js'],
Expand Down

0 comments on commit c2c4fd6

Please sign in to comment.