Skip to content

Commit

Permalink
Add JSX check to namespace rule
Browse files Browse the repository at this point in the history
  • Loading branch information
jf248 committed Aug 2, 2018
1 parent c34f14f commit 40d07b4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/rules/namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ module.exports = {
return
}

for (let specifier of declaration.specifiers) {
for (const specifier of declaration.specifiers) {
switch (specifier.type) {
case 'ImportNamespaceSpecifier':
if (!imports.size) {
Expand Down Expand Up @@ -160,8 +160,12 @@ module.exports = {

if (pattern.type !== 'ObjectPattern') return

for (let property of pattern.properties) {
if (property.type === 'ExperimentalRestProperty' || !property.key) {
for (const property of pattern.properties) {
if (
property.type === 'ExperimentalRestProperty'
|| property.type === 'RestElement'
|| !property.key
) {
continue
}

Expand Down Expand Up @@ -189,6 +193,17 @@ module.exports = {

testKey(id, namespaces.get(init.name))
},

'JSXMemberExpression': function({object, property}) {
if (!namespaces.has(object.name)) return
var namespace = namespaces.get(object.name)
if (!namespace.has(property.name)) {
context.report({
node: property,
message: makeMessage(property, [object.name]),
})
}
},
}
},
}
21 changes: 21 additions & 0 deletions tests/src/rules/namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ const valid = [
parser: 'babel-eslint',
}),

// JSX
test({
code: 'import * as Names from "./named-exports"; const Foo = <Names.a/>',
parserOptions: {
ecmaFeatures: {
jsx: true,
},
},
}),

...SYNTAX_CASES,
]

Expand Down Expand Up @@ -185,6 +195,17 @@ const invalid = [
errors: [`'default' not found in imported namespace 'ree'.`],
}),

// JSX
test({
code: 'import * as Names from "./named-exports"; const Foo = <Names.e/>',
errors: [error('e', 'Names')],
parserOptions: {
ecmaFeatures: {
jsx: true,
},
},
}),

]

///////////////////////
Expand Down

0 comments on commit 40d07b4

Please sign in to comment.