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 authored and ljharb committed Aug 2, 2018
1 parent 8252344 commit e30a757
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/rules/namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ module.exports = {
return {

// pick up all imports at body entry time, to properly respect hoisting
'Program': function ({ body }) {
Program: function ({ body }) {
function processBodyStatement(declaration) {
if (declaration.type !== 'ImportDeclaration') return

Expand Down Expand Up @@ -83,7 +83,7 @@ module.exports = {
},

// same as above, but does not add names to local map
'ExportNamespaceSpecifier': function (namespace) {
ExportNamespaceSpecifier: function (namespace) {
var declaration = importDeclaration(context)

var imports = Exports.get(declaration.source.value, context)
Expand All @@ -102,7 +102,7 @@ module.exports = {

// todo: check for possible redefinition

'MemberExpression': function (dereference) {
MemberExpression: function (dereference) {
if (dereference.object.type !== 'Identifier') return
if (!namespaces.has(dereference.object.name)) return

Expand Down Expand Up @@ -146,7 +146,7 @@ module.exports = {

},

'VariableDeclarator': function ({ id, init }) {
VariableDeclarator: function ({ id, init }) {
if (init == null) return
if (init.type !== 'Identifier') return
if (!namespaces.has(init.name)) return
Expand Down Expand Up @@ -193,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 e30a757

Please sign in to comment.