-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add a new eslint rule prevent using margin (#17024)
* chore: add a new eslint rule prevent using margin
- Loading branch information
Showing
6 changed files
with
131 additions
and
34 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
46 changes: 46 additions & 0 deletions
46
scripts/eslint-plugin-opentrons/lib/rules/no-margins-in-css.js
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,46 @@ | ||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'Disallow the use of margin-related properties in css-in-js', | ||
category: 'Best Practices', | ||
recommended: false, | ||
}, | ||
messages: { | ||
noMarginInCssInJs: "Avoid using '{{property}}' in css-in-js.", | ||
}, | ||
}, | ||
create(context) { | ||
return { | ||
// Check for CSS-in-JS template literals | ||
TaggedTemplateExpression(node) { | ||
const forbiddenMargins = [ | ||
'margin', | ||
// 'margin-top', | ||
// 'margin-left', | ||
// 'margin-right', | ||
// 'margin-bottom', | ||
] | ||
|
||
if (node.tag.type === 'Identifier' && node.tag.name === 'css') { | ||
const templateLiteral = node.quasi | ||
templateLiteral.quasis.forEach(quasi => { | ||
const text = quasi.value.raw | ||
forbiddenMargins.forEach(property => { | ||
const regex = new RegExp(`\\b${property}\\b`, 'i') | ||
if (regex.test(text)) { | ||
context.report({ | ||
node: quasi, | ||
messageId: 'noMarginInCssInJs', | ||
data: { | ||
property, | ||
}, | ||
}) | ||
} | ||
}) | ||
}) | ||
} | ||
}, | ||
} | ||
}, | ||
} |
52 changes: 52 additions & 0 deletions
52
scripts/eslint-plugin-opentrons/lib/rules/no-margins-inline.js
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,52 @@ | ||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'Disallow the use of margin-related styles', | ||
category: 'Best Practices', | ||
recommended: false, | ||
}, | ||
messages: { | ||
noMarginInline: "Avoid using '{{property}}' in your components.", | ||
}, | ||
schema: [], | ||
}, | ||
create(context) { | ||
const forbiddenMargins = [ | ||
'margin', | ||
'marginLeft', | ||
'marginRight', | ||
'marginTop', | ||
'marginBottom', | ||
'marginX', | ||
'marginY', | ||
] | ||
|
||
return { | ||
// Existing visitor for object properties | ||
Property(node) { | ||
if (forbiddenMargins.includes(node.key.name || node.key.value)) { | ||
context.report({ | ||
node: node.key, | ||
messageId: 'noMarginInline', | ||
data: { | ||
property: node.key.name || node.key.value, | ||
}, | ||
}) | ||
} | ||
}, | ||
// New visitor for JSX attributes | ||
JSXAttribute(node) { | ||
if (forbiddenMargins.includes(node.name.name)) { | ||
context.report({ | ||
node: node.name, | ||
messageId: 'noMarginInline', | ||
data: { | ||
property: node.name.name, | ||
}, | ||
}) | ||
} | ||
}, | ||
} | ||
}, | ||
} |