Skip to content

Commit

Permalink
fix(jmvc/no-null-model-calls): ignore null attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
david-at-frog committed Sep 24, 2019
1 parent 03b40b1 commit 304f475
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
15 changes: 12 additions & 3 deletions lib/rules/jmvc/no-null-model-calls.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
},
Expand All @@ -48,7 +49,7 @@ module.exports = {
return
}

const badArgumentTuples = node.arguments
let badArgumentTuples = node.arguments
.map(node => ({
node,
value: resolve()(node, context)
Expand All @@ -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 {
Expand All @@ -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('.')
Expand Down
15 changes: 15 additions & 0 deletions test/lib/rules/jquery-compat/jmvc/no-null-model-calls.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ ruleTester.run(
},
{
code: 'Foo.Bar.Baz.findAll({ quux: null })'
},
{
code: 'Foo.Models.Baz.extend({ attributes: { something: null } })'
}
],
invalid: [
Expand Down Expand Up @@ -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'
}
}]
}
]
})

0 comments on commit 304f475

Please sign in to comment.