Skip to content

Commit

Permalink
Merge pull request #11 from Automattic/fix/jsx-classname-namespace-no…
Browse files Browse the repository at this point in the history
…n-index

jsx-classname-namespace: Don't consider non-index as root
  • Loading branch information
aduth authored and sirreal committed Dec 11, 2018
1 parent 79bcde2 commit 225e4d7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@

var path = require( 'path' );

//------------------------------------------------------------------------------
// Constants
//------------------------------------------------------------------------------

var REGEXP_INDEX_PATH = /\/index\.jsx?$/;

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -72,10 +78,14 @@ var rule = module.exports = function( context ) {
return false;
}

function isRootRenderedElement( node ) {
function isRootRenderedElement( node, filename ) {
var parent = node.parent.parent,
functionExpression, functionName, isRoot;

if ( ! REGEXP_INDEX_PATH.test( filename ) ) {
return false;
}

do {
parent = parent.parent;

Expand Down Expand Up @@ -161,7 +171,7 @@ var rule = module.exports = function( context ) {

return {
JSXAttribute: function( node ) {
var rawClassName, classNames, isError, isRoot, namespace, expected;
var rawClassName, filename, classNames, isError, isRoot, namespace, expected;

if ( 'className' !== node.name.name ) {
return;
Expand All @@ -177,9 +187,10 @@ var rule = module.exports = function( context ) {
return;
}

filename = context.getFilename();
classNames = rawClassName.value.split( ' ' );
namespace = path.basename( path.dirname( context.getFilename() ) );
isRoot = isRootRenderedElement( node );
namespace = path.basename( path.dirname( filename ) );
isRoot = isRootRenderedElement( node, filename );

// `null` return value indicates intent to abort validation
if ( null === isRoot ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,11 @@ EXPECTED_FOO_PREFIX_ERROR = formatMessage( rule.ERROR_MESSAGE, { expected: 'foo_
code: 'import { render } from "react-dom"; render( <div className="quux"><div className="quux__child" /></div>, document.body );',
parserOptions: { ecmaFeatures: { jsx: true }, sourceType: 'module' },
filename: '/tmp/foo/index.js'
},
{
code: 'export default function() { return <div className="foo__child" />; }',
parserOptions: { ecmaFeatures: { jsx: true }, sourceType: 'module' },
filename: '/tmp/foo/foo-child.js'
}
],

Expand Down Expand Up @@ -361,6 +366,14 @@ EXPECTED_FOO_PREFIX_ERROR = formatMessage( rule.ERROR_MESSAGE, { expected: 'foo_
errors: [ {
message: EXPECTED_FOO_ERROR
} ]
},
{
code: 'export default function() { return <div className="foo" />; }',
parserOptions: { ecmaFeatures: { jsx: true }, sourceType: 'module' },
filename: '/tmp/foo/foo-child.js',
errors: [ {
message: EXPECTED_FOO_PREFIX_ERROR
} ]
}
]
} );

0 comments on commit 225e4d7

Please sign in to comment.