Skip to content

Commit e30a757

Browse files
jf248ljharb
authored andcommitted
Add JSX check to namespace rule
1 parent 8252344 commit e30a757

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

Diff for: src/rules/namespace.js

+15-4
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ module.exports = {
4444
return {
4545

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

@@ -83,7 +83,7 @@ module.exports = {
8383
},
8484

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

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

103103
// todo: check for possible redefinition
104104

105-
'MemberExpression': function (dereference) {
105+
MemberExpression: function (dereference) {
106106
if (dereference.object.type !== 'Identifier') return
107107
if (!namespaces.has(dereference.object.name)) return
108108

@@ -146,7 +146,7 @@ module.exports = {
146146

147147
},
148148

149-
'VariableDeclarator': function ({ id, init }) {
149+
VariableDeclarator: function ({ id, init }) {
150150
if (init == null) return
151151
if (init.type !== 'Identifier') return
152152
if (!namespaces.has(init.name)) return
@@ -193,6 +193,17 @@ module.exports = {
193193

194194
testKey(id, namespaces.get(init.name))
195195
},
196+
197+
JSXMemberExpression: function({object, property}) {
198+
if (!namespaces.has(object.name)) return
199+
var namespace = namespaces.get(object.name)
200+
if (!namespace.has(property.name)) {
201+
context.report({
202+
node: property,
203+
message: makeMessage(property, [object.name]),
204+
})
205+
}
206+
},
196207
}
197208
},
198209
}

Diff for: tests/src/rules/namespace.js

+21
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,16 @@ const valid = [
104104
parser: 'babel-eslint',
105105
}),
106106

107+
// JSX
108+
test({
109+
code: 'import * as Names from "./named-exports"; const Foo = <Names.a/>',
110+
parserOptions: {
111+
ecmaFeatures: {
112+
jsx: true,
113+
},
114+
},
115+
}),
116+
107117
...SYNTAX_CASES,
108118
]
109119

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

198+
// JSX
199+
test({
200+
code: 'import * as Names from "./named-exports"; const Foo = <Names.e/>',
201+
errors: [error('e', 'Names')],
202+
parserOptions: {
203+
ecmaFeatures: {
204+
jsx: true,
205+
},
206+
},
207+
}),
208+
188209
]
189210

190211
///////////////////////

0 commit comments

Comments
 (0)