Skip to content

Commit

Permalink
ensure onrender and onteardown are not arrow function expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Dec 11, 2016
1 parent a6c648b commit 017b67a
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/validate/js/propValidators/onrender.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
export default function onrender () {
import usesThisOrArguments from '../utils/usesThisOrArguments.js';

export default function onrender ( validator, prop ) {
if ( prop.value.type === 'ArrowFunctionExpression' ) {
if ( usesThisOrArguments( prop.value.body ) ) {
validator.error( `'onrender' should be a function expression, not an arrow function expression`, prop.start );
}
}
}
8 changes: 7 additions & 1 deletion src/validate/js/propValidators/onteardown.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
export default function onteardown () {
import usesThisOrArguments from '../utils/usesThisOrArguments.js';

export default function onteardown ( validator, prop ) {
if ( prop.value.type === 'ArrowFunctionExpression' ) {
if ( usesThisOrArguments( prop.value.body ) ) {
validator.error( `'onteardown' should be a function expression, not an arrow function expression`, prop.start );
}
}
}
1 change: 1 addition & 0 deletions test/validator/onrender-arrow-no-this/errors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
5 changes: 5 additions & 0 deletions test/validator/onrender-arrow-no-this/input.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
export default {
onrender: () => console.log( 'rendering' )
};
</script>
8 changes: 8 additions & 0 deletions test/validator/onrender-arrow-this/errors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[{
"message": "'onrender' should be a function expression, not an arrow function expression",
"pos": 29,
"loc": {
"line": 3,
"column": 2
}
}]
7 changes: 7 additions & 0 deletions test/validator/onrender-arrow-this/input.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>
export default {
onrender: () => {
this.set({ a: 1 });
}
};
</script>
1 change: 1 addition & 0 deletions test/validator/onteardown-arrow-no-this/errors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
5 changes: 5 additions & 0 deletions test/validator/onteardown-arrow-no-this/input.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
export default {
onteardown: () => console.log( 'tearing down' )
};
</script>
8 changes: 8 additions & 0 deletions test/validator/onteardown-arrow-this/errors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[{
"message": "'onteardown' should be a function expression, not an arrow function expression",
"pos": 29,
"loc": {
"line": 3,
"column": 2
}
}]
7 changes: 7 additions & 0 deletions test/validator/onteardown-arrow-this/input.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>
export default {
onteardown: () => {
this.set({ a: 1 });
}
};
</script>

0 comments on commit 017b67a

Please sign in to comment.