From 017b67aa38d53b96dff91ad3139540276a351309 Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Sun, 11 Dec 2016 10:42:44 -0500 Subject: [PATCH] ensure onrender and onteardown are not arrow function expressions --- src/validate/js/propValidators/onrender.js | 8 +++++++- src/validate/js/propValidators/onteardown.js | 8 +++++++- test/validator/onrender-arrow-no-this/errors.json | 1 + test/validator/onrender-arrow-no-this/input.html | 5 +++++ test/validator/onrender-arrow-this/errors.json | 8 ++++++++ test/validator/onrender-arrow-this/input.html | 7 +++++++ test/validator/onteardown-arrow-no-this/errors.json | 1 + test/validator/onteardown-arrow-no-this/input.html | 5 +++++ test/validator/onteardown-arrow-this/errors.json | 8 ++++++++ test/validator/onteardown-arrow-this/input.html | 7 +++++++ 10 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 test/validator/onrender-arrow-no-this/errors.json create mode 100644 test/validator/onrender-arrow-no-this/input.html create mode 100644 test/validator/onrender-arrow-this/errors.json create mode 100644 test/validator/onrender-arrow-this/input.html create mode 100644 test/validator/onteardown-arrow-no-this/errors.json create mode 100644 test/validator/onteardown-arrow-no-this/input.html create mode 100644 test/validator/onteardown-arrow-this/errors.json create mode 100644 test/validator/onteardown-arrow-this/input.html diff --git a/src/validate/js/propValidators/onrender.js b/src/validate/js/propValidators/onrender.js index 6cc31c9cc4..33693be8f1 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 96cce604f8..7ce61e223d 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/test/validator/onrender-arrow-no-this/errors.json b/test/validator/onrender-arrow-no-this/errors.json new file mode 100644 index 0000000000..fe51488c70 --- /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 0000000000..c82e5c3745 --- /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 0000000000..5cc9b2c78d --- /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 0000000000..e4283e60ba --- /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 0000000000..fe51488c70 --- /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 0000000000..a742898edb --- /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 0000000000..0596578b95 --- /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 0000000000..ac2f1d7e8b --- /dev/null +++ b/test/validator/onteardown-arrow-this/input.html @@ -0,0 +1,7 @@ +