Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create ESLint plugin for Griffel #99

Merged
merged 2 commits into from
Apr 22, 2022

Conversation

layershifter
Copy link
Member

@layershifter layershifter commented Apr 21, 2022

This PR adds @griffel/eslint-plugin with a single ESLint rule to enforce usage of CSS longhands.

Fixes #76.

Rule Details

Griffel does not support usage of CSS shorthands, microsoft/griffel#30.
This is enforced by typings, but there are cases when they don't work as expected microsoft/griffel#78.

Examples of incorrect code for this rule:

import { makeStyles } from '@griffel/react';

export const useStyles = makeStyles({
  root: {
    background: 'red',
    padding: '10px 20px',
  },
});

image

Examples of correct code for this rule:

import { makeStyles } from '@griffel/react';

export const useStyles = makeStyles({
  root: {
    backgroundColor: 'red',
    ...shorthands.padding('10px', '20px'),
  },
});

@github-actions
Copy link

github-actions bot commented Apr 21, 2022

📊 Bundle size report

Unchanged fixtures
Package & Exports Size (minified/GZIP)
core
makeStyles + mergeClasses (build time)
1.8 kB
861 B
core
makeStyles + mergeClasses (runtime)
20.029 kB
7.479 kB
react
makeStaticStyles (runtime)
8.247 kB
3.595 kB
react
makeStyles + mergeClasses (runtime)
21.054 kB
7.911 kB
react
makeStyles + mergeClasses (build time)
2.811 kB
1.275 kB
🤖 This report was generated against 09eb2760eabcb6abc3d21b7333d0ec14f6c4b5d2

Comment on lines +9 to +11
// This collection is a map simply for faster access when checking if a CSS property is unsupported
// @griffel/core has the same definition, but ESLint plugin should not depend on it
const UNSUPPORTED_CSS_PROPERTIES: Record<keyof CSS.StandardShorthandProperties, true> = {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's nonsense to have there dependency on @griffel/core, so unfortunately - copy-paste.

Comment on lines +7 to +11
export const isIdentifier: IsHelper<AST_NODE_TYPES.Identifier> = ASTUtils.isIdentifier;
export const isObjectExpression: IsHelper<AST_NODE_TYPES.ObjectExpression> = ASTUtils.isNodeOfType(
AST_NODE_TYPES.ObjectExpression,
);
export const isProperty: IsHelper<AST_NODE_TYPES.Property> = ASTUtils.isNodeOfType(AST_NODE_TYPES.Property);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had to type this manually otherwise TypeScript goes wild:

image

@layershifter layershifter marked this pull request as ready for review April 21, 2022 14:46
@layershifter layershifter requested a review from a team as a code owner April 21, 2022 14:46
@@ -65,4 +65,5 @@ export type {
GriffelRenderer,
} from './types';

// Private exports, are used by devtools
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe prefix with @internal and then strip on build and use stripInternal in the tsconfig ?

you'll be able to use it with aliases in the monorepo but the types will be stripped on build

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't know about stripInternal, it's a good idea. I will try it in another PR.

Comment on lines +67 to +69
if (isProperty(propertyNode)) {
if (isIdentifier(propertyNode.key) && !isRoot) {
if (Object.prototype.hasOwnProperty.call(UNSUPPORTED_CSS_PROPERTIES, propertyNode.key.name)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: these nested if blocks can just be reduced to a single one right ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but IMO it's more readable

@@ -0,0 +1,11 @@
import { AST_NODE_TYPES, ASTUtils, TSESTree } from '@typescript-eslint/utils';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it necessary to use typescript-eslint to create the lint rule ?

Copy link
Member Author

@layershifter layershifter Apr 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK there are no similar utils in ESLint itself. I was using eslint-plugin-testing-library as a reference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Lint rules for GriffelStyle
2 participants