-
Notifications
You must be signed in to change notification settings - Fork 842
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ESLint rule to detect forwardRef components without display name (#3451)
* Added es lint rule to ensure display name for forwardRef components * Added display name for `forwardRef` components * removed fix * Added cl * Updated message in tests * Check if nodes parent is a member expression * Updated cl * Fixed issue with rule Co-authored-by: Chandler Prall <chandler.prall@gmail.com>
- Loading branch information
1 parent
e8eb712
commit 574429d
Showing
10 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'Enforce display name to forwardRef components', | ||
}, | ||
}, | ||
create: function(context) { | ||
const forwardRefUsages = []; | ||
const displayNameUsages = []; | ||
return { | ||
VariableDeclarator(node) { | ||
if (node.init && node.init.type === 'CallExpression') { | ||
if ( | ||
node.init.callee && | ||
node.init.callee.type === 'MemberExpression' | ||
) { | ||
if ( | ||
node.init.callee.property && | ||
node.init.callee.property.name === 'forwardRef' | ||
) { | ||
forwardRefUsages.push(node.id); | ||
} | ||
} | ||
if (node.init.callee && node.init.callee.name === 'forwardRef') { | ||
forwardRefUsages.push(node.id); | ||
} | ||
} | ||
}, | ||
MemberExpression(node) { | ||
const { property } = node; | ||
if ( | ||
property && | ||
property.type === 'Identifier' && | ||
property.name === 'displayName' | ||
) { | ||
displayNameUsages.push(node.object); | ||
} | ||
}, | ||
'Program:exit'() { | ||
forwardRefUsages.forEach(identifier => { | ||
if (!isDisplayNameUsed(identifier)) { | ||
context.report({ | ||
node: identifier, | ||
message: 'Forward ref components must use a display name', | ||
}); | ||
} | ||
}); | ||
}, | ||
}; | ||
function isDisplayNameUsed(identifier) { | ||
const node = displayNameUsages.find( | ||
displayName => displayName.name === identifier.name | ||
); | ||
return !!node; | ||
} | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import rule from './forward_ref_display_name.js'; | ||
const RuleTester = require('eslint').RuleTester; | ||
|
||
const ruleTester = new RuleTester({ | ||
parser: 'babel-eslint', | ||
}); | ||
|
||
const valid = [ | ||
`const Component = React.forwardRef<ref>(() => {}) | ||
Component.displayName = "EuiBadgeGroup" | ||
`, | ||
]; | ||
|
||
const invalid = [ | ||
{ | ||
code: 'const Component = React.forwardRef<ref>(() => {})', | ||
errors: [ | ||
{ | ||
message: 'Forward ref components must use a display name', | ||
}, | ||
], | ||
}, | ||
]; | ||
|
||
ruleTester.run('forward_ref_display_name', rule, { | ||
valid, | ||
invalid, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters