@atlaskit/eslint-plugin-ui-styling-standard
Version:
The eslint plugin to enforce and educate on Atlassian's UI Styling Standard
67 lines • 2.24 kB
JavaScript
import { createLintRule } from '../utils/create-rule';
const restrictedFileTypes = ['.css', '.less', '.sass', '.scss'];
const restrictedImports = {
'@emotion/core': ['Global'],
'@emotion/react': ['Global'],
'styled-components': ['createGlobalStyle', 'injectGlobal']
};
function isRestrictedImportSource(importSource) {
return importSource in restrictedImports;
}
const rule = createLintRule({
meta: {
name: 'no-global-styles',
docs: {
description: 'Prevents global styles through CSS-in-JS or CSS module imports',
recommended: true,
severity: 'error'
},
messages: {
'no-global-styles': "Move global declarations into local cssMap() or css() styles on their owning elements. Use @atlaskit/css-reset only for the supported global reset.",
'no-css-modules': "Replace the CSS-file import with local cssMap() or css() declarations from @atlaskit/css and apply them on the owning elements; preserve the imported styles during migration."
},
type: 'problem'
},
create(context) {
function checkFileType(node) {
const importSource = node.source.value;
if (restrictedFileTypes.some(suffix => importSource.endsWith(suffix))) {
context.report({
node: node.source,
messageId: 'no-css-modules'
});
}
}
function checkImports(node) {
const importSource = node.source.value;
if (!isRestrictedImportSource(importSource)) {
return;
}
for (const importName of restrictedImports[importSource]) {
const specifier = node.specifiers.filter(isImportSpecifier).find(specifier => 'name' in specifier.imported && specifier.imported.name === importName);
if (specifier) {
context.report({
node: specifier,
messageId: 'no-global-styles'
});
}
}
}
return {
ImportDeclaration(node) {
checkFileType(node);
checkImports(node);
},
'JSXOpeningElement[name.name="style"]'(node) {
context.report({
node,
messageId: 'no-global-styles'
});
}
};
}
});
export default rule;
function isImportSpecifier(node) {
return node.type === 'ImportSpecifier';
}