Skip to content
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

Add JSX check to namespace rule #1151

Merged
merged 1 commit into from
Aug 11, 2018
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
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