Skip to content

Commit 8a0d516

Browse files
committed
Add option to ignore undefined attributes.
This is useful because the existing rule doesn't handle attributes without values, e.g. ``` <input type="checkbox" checked /> ``` Looking at the node I can't see any way to handle these correctly, so being able to disable it is more useful.
1 parent cd33896 commit 8a0d516

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

Readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ then in your ~/.eslintrc:
2424

2525
`jsx/no-undef` also accepts a `varsIgnorePattern` which can be used to ignore certain undefined patterns (e.g. when using custom web elements).
2626

27+
`jsx/no-undef` also accepts a `ignoreAttributes` which can be used disregard linting of attributes (which incorrectly flags boolean attributes as undefined).
28+
2729
All those rules are defined by default though so you can leave out the ones you agree with.
2830

2931
## Thanks

index.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ const markUsedRule = context => {
7272

7373
const noUndefRule = context => {
7474
const config = context.options[0] || {}
75+
const ignoreAttributes = !!config.ignoreAttributes
7576
const ignored = config.varsIgnorePattern && new RegExp(config.varsIgnorePattern)
7677
const isIgnored = ignored
7778
? name => ignored.test(name)
@@ -83,10 +84,13 @@ const noUndefRule = context => {
8384
if (name.type == 'JSXMemberExpression') name = name.object
8485
if (name.type == 'JSXNamespacedName') name = name.namespace
8586
const variables = variablesInScope(context)
86-
node.attributes.forEach(attr => {
87-
if (attr.type == 'JSXSpreadAttribute') return
88-
if (attr.value == null && !isIgnored(attr.name.name)) checkDefined(context, variables, attr.name)
89-
})
87+
88+
if (!ignoreAttributes) {
89+
node.attributes.forEach(attr => {
90+
if (attr.type == 'JSXSpreadAttribute') return
91+
if (attr.value == null && !isIgnored(attr.name.name)) checkDefined(context, variables, attr.name)
92+
})
93+
}
9094
if (!standardTags.has(name.name) && !isIgnored(name.name)) checkDefined(context, variables, name)
9195
}
9296
}

0 commit comments

Comments
 (0)