Skip to content

Commit 523e169

Browse files
authored
no-system-props: skip html elements (#148)
* add check for react component or not * reverse the check * Create few-beers-jump.md
1 parent ca14bb6 commit 523e169

File tree

4 files changed

+26
-1
lines changed

4 files changed

+26
-1
lines changed

.changeset/few-beers-jump.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"eslint-plugin-primer-react": patch
3+
---
4+
5+
no-system-props: skip html elements

src/rules/__tests__/no-system-props.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ ruleTester.run('no-system-props', rule, {
2222
`import {Button} from '@primer/react'; <Button variant="large" />`,
2323
`import {Button} from '@primer/react'; <Button size="large" />`,
2424
`import {ActionMenu} from '@primer/react'; <ActionMenu.Overlay width="large" />`,
25+
{code: `<img width="200px" />`, options: [{skipImportCheck: true}]},
2526
],
2627
invalid: [
2728
{

src/rules/no-system-props.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const {isPrimerComponent} = require('../utils/is-primer-component')
2+
const {isHTMLElement} = require('../utils/is-html-element')
23
const {getJSXOpeningElementName} = require('../utils/get-jsx-opening-element-name')
34
const {pick} = require('@styled-system/props')
45
const {some, last} = require('lodash')
@@ -81,7 +82,14 @@ module.exports = {
8182

8283
return {
8384
JSXOpeningElement(jsxNode) {
84-
if (!skipImportCheck && !isPrimerComponent(jsxNode.name, context.getScope(jsxNode))) return
85+
if (skipImportCheck) {
86+
// if we skip checking if component is imported from primer,
87+
// we need to atleast skip html elements
88+
if (isHTMLElement(jsxNode)) return
89+
} else {
90+
// skip if component is not imported from primer/react
91+
if (!isPrimerComponent(jsxNode.name, context.getScope(jsxNode))) return
92+
}
8593

8694
const componentName = getJSXOpeningElementName(jsxNode)
8795

src/utils/is-html-element.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function isHTMLElement(jsxNode) {
2+
if (jsxNode.name.type === 'JSXIdentifier') {
3+
// this is a very silly proxy, but it works
4+
// React components are capitalised, html elements are not
5+
const firstLetter = jsxNode.name.name
6+
if (firstLetter === firstLetter.toLowerCase()) return true
7+
}
8+
9+
return false
10+
}
11+
exports.isHTMLElement = isHTMLElement

0 commit comments

Comments
 (0)