diff --git a/src/validate/js/propValidators/methods.js b/src/validate/js/propValidators/methods.js index daa5bd662a2e..1a5ba5314b7c 100644 --- a/src/validate/js/propValidators/methods.js +++ b/src/validate/js/propValidators/methods.js @@ -1,5 +1,6 @@ import checkForDupes from '../utils/checkForDupes.js'; import checkForComputedKeys from '../utils/checkForComputedKeys.js'; +import usesThisOrArguments from '../utils/usesThisOrArguments.js'; const builtin = { set: true, @@ -23,5 +24,11 @@ export default function methods ( validator, prop ) { if ( builtin[ prop.key.name ] ) { validator.error( `Cannot overwrite built-in method '${prop.key.name}'` ); } + + if ( prop.value.type === 'ArrowFunctionExpression' ) { + if ( usesThisOrArguments( prop.value.body ) ) { + validator.error( `Method '${prop.key.name}' should be a function expression, not an arrow function expression`, prop.start ); + } + } }); } diff --git a/src/validate/js/propValidators/onrender.js b/src/validate/js/propValidators/onrender.js index 6cc31c9cc4d7..33693be8f1fd 100644 --- a/src/validate/js/propValidators/onrender.js +++ b/src/validate/js/propValidators/onrender.js @@ -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 ); + } + } } diff --git a/src/validate/js/propValidators/onteardown.js b/src/validate/js/propValidators/onteardown.js index 96cce604f828..7ce61e223d4f 100644 --- a/src/validate/js/propValidators/onteardown.js +++ b/src/validate/js/propValidators/onteardown.js @@ -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 ); + } + } } diff --git a/src/validate/js/utils/usesThisOrArguments.js b/src/validate/js/utils/usesThisOrArguments.js new file mode 100644 index 000000000000..40fffc0a5cfc --- /dev/null +++ b/src/validate/js/utils/usesThisOrArguments.js @@ -0,0 +1,24 @@ +import { walk } from 'estree-walker'; +import isReference from '../../../utils/isReference.js'; + +export default function usesThisOrArguments ( node ) { + let result = false; + + walk( node, { + enter ( node ) { + if ( result || node.type === 'FunctionExpression' || node.type === 'FunctionDeclaration' ) { + return this.skip(); + } + + if ( node.type === 'ThisExpression' ) { + result = true; + } + + if ( node.type === 'Identifier' && isReference( node ) && node.name === 'arguments' ) { + result = true; + } + } + }); + + return result; +} diff --git a/test/validator/method-arrow-no-this/errors.json b/test/validator/method-arrow-no-this/errors.json new file mode 100644 index 000000000000..fe51488c7066 --- /dev/null +++ b/test/validator/method-arrow-no-this/errors.json @@ -0,0 +1 @@ +[] diff --git a/test/validator/method-arrow-no-this/input.html b/test/validator/method-arrow-no-this/input.html new file mode 100644 index 000000000000..73349597f8f9 --- /dev/null +++ b/test/validator/method-arrow-no-this/input.html @@ -0,0 +1,9 @@ + + + diff --git a/test/validator/method-arrow-this/errors.json b/test/validator/method-arrow-this/errors.json new file mode 100644 index 000000000000..b6fa2711058c --- /dev/null +++ b/test/validator/method-arrow-this/errors.json @@ -0,0 +1,8 @@ +[{ + "message": "Method 'foo' should be a function expression, not an arrow function expression", + "pos": 79, + "loc": { + "line": 6, + "column": 3 + } +}] diff --git a/test/validator/method-arrow-this/input.html b/test/validator/method-arrow-this/input.html new file mode 100644 index 000000000000..2febe624f6d5 --- /dev/null +++ b/test/validator/method-arrow-this/input.html @@ -0,0 +1,11 @@ + + + diff --git a/test/validator/onrender-arrow-no-this/errors.json b/test/validator/onrender-arrow-no-this/errors.json new file mode 100644 index 000000000000..fe51488c7066 --- /dev/null +++ b/test/validator/onrender-arrow-no-this/errors.json @@ -0,0 +1 @@ +[] diff --git a/test/validator/onrender-arrow-no-this/input.html b/test/validator/onrender-arrow-no-this/input.html new file mode 100644 index 000000000000..c82e5c37454f --- /dev/null +++ b/test/validator/onrender-arrow-no-this/input.html @@ -0,0 +1,5 @@ + diff --git a/test/validator/onrender-arrow-this/errors.json b/test/validator/onrender-arrow-this/errors.json new file mode 100644 index 000000000000..5cc9b2c78d4c --- /dev/null +++ b/test/validator/onrender-arrow-this/errors.json @@ -0,0 +1,8 @@ +[{ + "message": "'onrender' should be a function expression, not an arrow function expression", + "pos": 29, + "loc": { + "line": 3, + "column": 2 + } +}] diff --git a/test/validator/onrender-arrow-this/input.html b/test/validator/onrender-arrow-this/input.html new file mode 100644 index 000000000000..e4283e60ba91 --- /dev/null +++ b/test/validator/onrender-arrow-this/input.html @@ -0,0 +1,7 @@ + diff --git a/test/validator/onteardown-arrow-no-this/errors.json b/test/validator/onteardown-arrow-no-this/errors.json new file mode 100644 index 000000000000..fe51488c7066 --- /dev/null +++ b/test/validator/onteardown-arrow-no-this/errors.json @@ -0,0 +1 @@ +[] diff --git a/test/validator/onteardown-arrow-no-this/input.html b/test/validator/onteardown-arrow-no-this/input.html new file mode 100644 index 000000000000..a742898edba0 --- /dev/null +++ b/test/validator/onteardown-arrow-no-this/input.html @@ -0,0 +1,5 @@ + diff --git a/test/validator/onteardown-arrow-this/errors.json b/test/validator/onteardown-arrow-this/errors.json new file mode 100644 index 000000000000..0596578b9577 --- /dev/null +++ b/test/validator/onteardown-arrow-this/errors.json @@ -0,0 +1,8 @@ +[{ + "message": "'onteardown' should be a function expression, not an arrow function expression", + "pos": 29, + "loc": { + "line": 3, + "column": 2 + } +}] diff --git a/test/validator/onteardown-arrow-this/input.html b/test/validator/onteardown-arrow-this/input.html new file mode 100644 index 000000000000..ac2f1d7e8b4b --- /dev/null +++ b/test/validator/onteardown-arrow-this/input.html @@ -0,0 +1,7 @@ +