From 304f4754986587c56ccbad6fba99aa5efa59e988 Mon Sep 17 00:00:00 2001 From: David Godfrey Date: Tue, 24 Sep 2019 11:45:43 +0100 Subject: [PATCH] fix(jmvc/no-null-model-calls): ignore null attributes --- lib/rules/jmvc/no-null-model-calls.js | 15 ++++++++++++--- .../jquery-compat/jmvc/no-null-model-calls.js | 15 +++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/lib/rules/jmvc/no-null-model-calls.js b/lib/rules/jmvc/no-null-model-calls.js index 4f2893a..1cb7930 100644 --- a/lib/rules/jmvc/no-null-model-calls.js +++ b/lib/rules/jmvc/no-null-model-calls.js @@ -31,7 +31,8 @@ module.exports = { fixFrom: '1.0.0' }, messages: { - 'no-null-model-calls': 'Cannot encode {{value}} in model call input on path {{path}}' + 'no-null-model-calls': 'Cannot encode {{value}} in model call input on path {{path}}', + 'no-null-model-defaults': 'Cannot encode {{value}} in model defaults on path {{path}}' }, type: 'problem' }, @@ -48,7 +49,7 @@ module.exports = { return } - const badArgumentTuples = node.arguments + let badArgumentTuples = node.arguments .map(node => ({ node, value: resolve()(node, context) @@ -60,6 +61,14 @@ module.exports = { })) .filter(({ badValue }) => !!badValue) + let messageId = 'no-null-model-calls' + const { type: calleeType, name: calleeName } = node.callee.property + if (calleeType === 'Identifier' && calleeName === 'extend') { + messageId = 'no-null-model-defaults' + badArgumentTuples = badArgumentTuples + .filter(({ badValue: { path } }) => path.split('/')[1] === 'defaults') + } + if (badArgumentTuples.length < 1) { return } const { @@ -69,7 +78,7 @@ module.exports = { context.report({ node: badNode, - messageId: 'no-null-model-calls', + messageId, data: { value: "" + badValue.value, path: badValue.path.split('/').join('.') diff --git a/test/lib/rules/jquery-compat/jmvc/no-null-model-calls.js b/test/lib/rules/jquery-compat/jmvc/no-null-model-calls.js index d39f3ca..2b93826 100644 --- a/test/lib/rules/jquery-compat/jmvc/no-null-model-calls.js +++ b/test/lib/rules/jquery-compat/jmvc/no-null-model-calls.js @@ -12,6 +12,9 @@ ruleTester.run( }, { code: 'Foo.Bar.Baz.findAll({ quux: null })' + }, + { + code: 'Foo.Models.Baz.extend({ attributes: { something: null } })' } ], invalid: [ @@ -53,6 +56,18 @@ ruleTester.run( path: '.corge' } }] + }, + { + code: `Foo.Models.Bar.Baz.extend({ defaults: { foo: null } })`, + errors: [{ + messageId: 'no-null-model-defaults', + line: 1, + column: 27, + data: { + value: null, + path: '.defaults.foo' + } + }] } ] })