From c9ad4a3a2822f20e7d0b8df3dbf4b9d592284e78 Mon Sep 17 00:00:00 2001 From: Emil Ajdyna Date: Wed, 27 Dec 2017 22:38:04 +0100 Subject: [PATCH] Check if data is a function --- src/validate/js/propValidators/data.ts | 6 ++---- .../properties-data-no-call-expression/errors.json | 8 ++++++++ .../properties-data-no-call-expression/input.html | 12 ++++++++++++ .../errors.json | 0 .../input.html | 0 .../properties-data-no-references/errors.json | 8 ++++++++ .../samples/properties-data-no-references/input.html | 8 ++++++++ 7 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 test/validator/samples/properties-data-no-call-expression/errors.json create mode 100644 test/validator/samples/properties-data-no-call-expression/input.html rename test/validator/samples/{properties-data-must-be-function => properties-data-no-object-expression}/errors.json (100%) rename test/validator/samples/{properties-data-must-be-function => properties-data-no-object-expression}/input.html (100%) create mode 100644 test/validator/samples/properties-data-no-references/errors.json create mode 100644 test/validator/samples/properties-data-no-references/input.html diff --git a/src/validate/js/propValidators/data.ts b/src/validate/js/propValidators/data.ts index b49931cf21..10b0195d53 100644 --- a/src/validate/js/propValidators/data.ts +++ b/src/validate/js/propValidators/data.ts @@ -1,14 +1,12 @@ import { Validator } from '../../'; import { Node } from '../../../interfaces'; -const disallowed = new Set(['Literal', 'ObjectExpression', 'ArrayExpression']); +const allowed = new Set(['FunctionExpression', 'ArrowFunctionExpression']) export default function data(validator: Validator, prop: Node) { while (prop.type === 'ParenthesizedExpression') prop = prop.expression; - // TODO should we disallow references and expressions as well? - - if (disallowed.has(prop.value.type)) { + if (!allowed.has(prop.value.type)) { validator.error(`'data' must be a function`, prop.value.start); } } diff --git a/test/validator/samples/properties-data-no-call-expression/errors.json b/test/validator/samples/properties-data-no-call-expression/errors.json new file mode 100644 index 0000000000..4f27fe8c15 --- /dev/null +++ b/test/validator/samples/properties-data-no-call-expression/errors.json @@ -0,0 +1,8 @@ +[{ + "message": "'data' must be a function", + "loc": { + "line": 10, + "column": 8 + }, + "pos": 131 +}] diff --git a/test/validator/samples/properties-data-no-call-expression/input.html b/test/validator/samples/properties-data-no-call-expression/input.html new file mode 100644 index 0000000000..d58079dc2f --- /dev/null +++ b/test/validator/samples/properties-data-no-call-expression/input.html @@ -0,0 +1,12 @@ +
+ + diff --git a/test/validator/samples/properties-data-must-be-function/errors.json b/test/validator/samples/properties-data-no-object-expression/errors.json similarity index 100% rename from test/validator/samples/properties-data-must-be-function/errors.json rename to test/validator/samples/properties-data-no-object-expression/errors.json diff --git a/test/validator/samples/properties-data-must-be-function/input.html b/test/validator/samples/properties-data-no-object-expression/input.html similarity index 100% rename from test/validator/samples/properties-data-must-be-function/input.html rename to test/validator/samples/properties-data-no-object-expression/input.html diff --git a/test/validator/samples/properties-data-no-references/errors.json b/test/validator/samples/properties-data-no-references/errors.json new file mode 100644 index 0000000000..928e7ece84 --- /dev/null +++ b/test/validator/samples/properties-data-no-references/errors.json @@ -0,0 +1,8 @@ +[{ + "message": "'data' must be a function", + "loc": { + "line": 6, + "column": 2 + }, + "pos": 76 +}] diff --git a/test/validator/samples/properties-data-no-references/input.html b/test/validator/samples/properties-data-no-references/input.html new file mode 100644 index 0000000000..ddf39d80f3 --- /dev/null +++ b/test/validator/samples/properties-data-no-references/input.html @@ -0,0 +1,8 @@ +
+ +