Skip to content

Fix small mistake in require-valid-default-prop #143

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions lib/rules/require-valid-default-prop.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,23 +102,20 @@ module.exports = {

for (const prop of properties) {
const type = getPropertyNode(prop.value, 'type')
if (!type) {
return
}
if (!type) continue

const typeNames = new Set(getTypes(type.value)
.map(item => item === 'Object' || item === 'Array' ? 'Function' : item) // Object and Array require function
.filter(item => NATIVE_TYPES.has(item)))

if (typeNames.size === 0) { // There is no native types detected
return
}
// There is no native types detected
if (typeNames.size === 0) continue

const def = getPropertyNode(prop.value, 'default')
if (!def) return
if (!def) continue

const defType = getValueType(def.value)
if (typeNames.has(defType)) return
if (!defType || typeNames.has(defType)) continue

context.report({
node: def,
Expand Down
1 change: 1 addition & 0 deletions tests/lib/rules/require-valid-default-prop.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ ruleTester.run('require-valid-default-prop', rule, {
foo: null,
foo: Number,
foo: [String, Number],
foo: { type: Number, default: VAR_BAR },
foo: { type: Number, default: 100 },
foo: { type: [String, Number], default: '' },
foo: { type: [String, Number], default: 0 },
Expand Down