Skip to content

Commit

Permalink
no-system-props: skip html elements (#148)
Browse files Browse the repository at this point in the history
* add check for react component or not

* reverse the check

* Create few-beers-jump.md
  • Loading branch information
siddharthkp authored Mar 8, 2024
1 parent ca14bb6 commit 523e169
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/few-beers-jump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-primer-react": patch
---

no-system-props: skip html elements
1 change: 1 addition & 0 deletions src/rules/__tests__/no-system-props.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ ruleTester.run('no-system-props', rule, {
`import {Button} from '@primer/react'; <Button variant="large" />`,
`import {Button} from '@primer/react'; <Button size="large" />`,
`import {ActionMenu} from '@primer/react'; <ActionMenu.Overlay width="large" />`,
{code: `<img width="200px" />`, options: [{skipImportCheck: true}]},
],
invalid: [
{
Expand Down
10 changes: 9 additions & 1 deletion src/rules/no-system-props.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const {isPrimerComponent} = require('../utils/is-primer-component')
const {isHTMLElement} = require('../utils/is-html-element')
const {getJSXOpeningElementName} = require('../utils/get-jsx-opening-element-name')
const {pick} = require('@styled-system/props')
const {some, last} = require('lodash')
Expand Down Expand Up @@ -81,7 +82,14 @@ module.exports = {

return {
JSXOpeningElement(jsxNode) {
if (!skipImportCheck && !isPrimerComponent(jsxNode.name, context.getScope(jsxNode))) return
if (skipImportCheck) {
// if we skip checking if component is imported from primer,
// we need to atleast skip html elements
if (isHTMLElement(jsxNode)) return
} else {
// skip if component is not imported from primer/react
if (!isPrimerComponent(jsxNode.name, context.getScope(jsxNode))) return
}

const componentName = getJSXOpeningElementName(jsxNode)

Expand Down
11 changes: 11 additions & 0 deletions src/utils/is-html-element.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function isHTMLElement(jsxNode) {
if (jsxNode.name.type === 'JSXIdentifier') {
// this is a very silly proxy, but it works
// React components are capitalised, html elements are not
const firstLetter = jsxNode.name.name
if (firstLetter === firstLetter.toLowerCase()) return true
}

return false
}
exports.isHTMLElement = isHTMLElement

0 comments on commit 523e169

Please sign in to comment.