-
-
Notifications
You must be signed in to change notification settings - Fork 267
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(suite): Add eslint rule for not overriding design system compon…
…ents (#13621)
- Loading branch information
Showing
5 changed files
with
105 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
require('ts-node').register({ | ||
transpileOnly: true, | ||
compilerOptions: { | ||
module: 'commonjs', | ||
}, | ||
}); | ||
|
||
module.exports = require('./rules').default; |
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,70 @@ | ||
import type { Rule } from 'eslint'; | ||
|
||
export default { | ||
'no-override-ds-component': { | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: | ||
'Disallows overriding components imported from a specific package using styled-components', | ||
|
||
category: 'Best Practices', | ||
recommended: false, | ||
}, | ||
messages: { | ||
avoidStyledComponent: | ||
"Please do not override components imported from '{{packageName}}'. Use wrapper component or ask Usability team for help.", | ||
}, | ||
schema: [ | ||
{ | ||
type: 'object', | ||
properties: { | ||
packageName: { | ||
type: 'string', | ||
}, | ||
}, | ||
additionalProperties: false, | ||
}, | ||
], | ||
}, | ||
create(context) { | ||
const packageName = context.options[0] && context.options[0].packageName; | ||
if (!packageName) { | ||
return {}; | ||
} | ||
|
||
const importedComponents = new Set(); | ||
|
||
return { | ||
ImportDeclaration(node) { | ||
if (node.source.value === packageName) { | ||
node.specifiers.forEach(specifier => { | ||
if ( | ||
specifier.type === 'ImportSpecifier' || | ||
specifier.type === 'ImportDefaultSpecifier' | ||
) { | ||
importedComponents.add(specifier.local.name); | ||
} | ||
}); | ||
} | ||
}, | ||
TaggedTemplateExpression(node) { | ||
if ( | ||
node.tag.type === 'CallExpression' && | ||
node.tag.callee.name === 'styled' && | ||
node.tag.arguments[0].type === 'Identifier' && | ||
importedComponents.has(node.tag.arguments[0].name) | ||
) { | ||
context.report({ | ||
node, | ||
messageId: 'avoidStyledComponent', | ||
data: { | ||
packageName, | ||
}, | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}, | ||
} satisfies Record<string, Rule.RuleModule>; |
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