Skip to content

Commit

Permalink
chore(eslint-plugin): Format
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielmfern committed Jan 24, 2024
1 parent 2c2eead commit e77ce12
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ export const createNoStylePropertyRule = (
type: "suggestion",
schema: [],
messages: {
"not-supported-on-most-email-clients": `The CSS ${isRuleForMultipleProperties ? "properties" : "property"
} ${propertyNames.join(", ")} ${isRuleForMultipleProperties ? "are" : "is"
} only supported on ${supportPercentage.toFixed(
2,
)}% of email clients, see ${caniemailLink}`,
"not-supported-on-most-email-clients": `The CSS ${
isRuleForMultipleProperties ? "properties" : "property"
} ${propertyNames.join(", ")} ${
isRuleForMultipleProperties ? "are" : "is"
} only supported on ${supportPercentage.toFixed(
2,
)}% of email clients, see ${caniemailLink}`,
},
},
create(context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,15 @@ export const createNoStyleValueForProperty = (

const isStylePropertyDisallowed = (name: string, value: string) => {
return (
value.trim() === valueToDisallow &&
camelCasedPropertyName === name
value.trim() === valueToDisallow && camelCasedPropertyName === name
);
};

return getRuleListenersForJSXStyleProperties(
isStylePropertyDisallowed,
context.sourceCode,
(nodeOrLocation) => {
if ('start' in nodeOrLocation) {
if ("start" in nodeOrLocation) {
const location = nodeOrLocation;
context.report({
loc: location,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ export const createNoStyleValueKeywordRule = (

const definedMessageOrDefault =
message ??
`The CSS ${isRuleForMultipleValues ? "values" : "value"
} ${valueKeywords.join(", ")} ${isRuleForMultipleValues ? "are" : "is"
`The CSS ${
isRuleForMultipleValues ? "values" : "value"
} ${valueKeywords.join(", ")} ${
isRuleForMultipleValues ? "are" : "is"
} only supported on ${supportPercentage.toFixed(
2,
)}% of email clients, see ${caniemailLink}`;
Expand All @@ -37,7 +39,7 @@ export const createNoStyleValueKeywordRule = (
isStylePropertyDisallowed,
context.sourceCode,
(nodeOrLocation) => {
if ('start' in nodeOrLocation) {
if ("start" in nodeOrLocation) {
const location = nodeOrLocation;
context.report({
loc: location,
Expand Down
8 changes: 6 additions & 2 deletions packages/eslint-plugin/src/utils/esquery.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// taken from @types/esquery but slightly modified
declare module 'esquery' {
declare module "esquery" {
import type { TSESTree } from "@typescript-eslint/utils";

type Node = TSESTree.Node;
Expand Down Expand Up @@ -35,7 +35,11 @@ declare module 'esquery' {
options?: ESQueryOptions,
): boolean;
/** Query the code AST using the selector string. */
function query(ast: Node, selector: string, options?: ESQueryOptions): Node[];
function query(
ast: Node,
selector: string,
options?: ESQueryOptions,
): Node[];

/** From a JS AST and a selector AST, collect all JS AST nodes that match the selector. */
function traverse(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AST_NODE_TYPES, type TSESTree } from "@typescript-eslint/utils";
import esquery from 'esquery';
import esquery from "esquery";
import type {
RuleListener,
SourceCode,
Expand Down Expand Up @@ -49,7 +49,10 @@ export const getRuleListenersForJSXStyleProperties = (
const propertiesPerClassname = new Map<string, Property[]>();

for (const className of classNameAttribute.split(" ")) {
for (const rule of generateTailwindCssRules([className], tailwindContext).rules) {
for (const rule of generateTailwindCssRules(
[className],
tailwindContext,
).rules) {
const properties = [] as Property[];
rule.walk((node) => {
if (node.type === "decl") {
Expand All @@ -63,7 +66,9 @@ export const getRuleListenersForJSXStyleProperties = (
return propertiesPerClassname;
};

ruleListener['JSXAttribute[name.name="className"] Literal'] = (node: TSESTree.Literal) => {
ruleListener['JSXAttribute[name.name="className"] Literal'] = (
node: TSESTree.Literal,
) => {
const classNameAttribute = node.value;
if (typeof classNameAttribute === "string") {
const propertiesForClassName =
Expand Down Expand Up @@ -91,7 +96,7 @@ export const getRuleListenersForJSXStyleProperties = (
}
}
}
}
};
}

const accumulatedProblemPropertiesPerVariableName = new Map<
Expand All @@ -102,7 +107,10 @@ export const getRuleListenersForJSXStyleProperties = (
// we can't directly get the ObjectExpression from VariableDeclarator
// because it can be wrapped with something like TSAsExpression
// which might cause issues here
for (const node of (esquery(sourceCode.ast, "VariableDeclarator ObjectExpression > Property") as TSESTree.Property[])) {
for (const node of esquery(
sourceCode.ast,
"VariableDeclarator ObjectExpression > Property",
) as TSESTree.Property[]) {
const [propertyName, propertyValue] = metadataFromPropertyNode(node);
if (isStylePropertyDisallowed(propertyName, propertyValue)) {
const variableNode = sourceCode.getAncestors!(node).findLast(
Expand Down
18 changes: 8 additions & 10 deletions packages/eslint-plugin/src/utils/tailwind-internals.d.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
/* eslint-disable @typescript-eslint/no-unused-vars */

declare module "tailwindcss/lib/lib/evaluateTailwindFunctions" {
import type { JitContext } from 'tailwindcss/lib/lib/setupContextUtils';
import type { Root } from 'postcss';
import type { JitContext } from "tailwindcss/lib/lib/setupContextUtils";
import type { Root } from "postcss";

function evaluateTailwindFunctions(
context: JITContext,
): ((root: Root) => void);
function evaluateTailwindFunctions(context: JITContext): (root: Root) => void;

export default {
default: evaluateTailwindFunctions
}
default: evaluateTailwindFunctions,
};
}

declare module "tailwindcss/lib/lib/generateRules" {
import type { JitContext } from 'tailwindcss/lib/lib/setupContextUtils';
import type { Rule } from 'postcss';
import type { JitContext } from "tailwindcss/lib/lib/setupContextUtils";
import type { Rule } from "postcss";

export function generateRules(
classNames: Set<string>,
context: JITContext,
): ([bigint, Rule])[];
): [bigint, Rule][];
}

// taken from https://github.com/vinicoder/tw-to-css/blob/main/types.d.ts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ export const getSourceCodeTailwindMetadata = (
):
| { hasTailwind: false }
| {
hasTailwind: true;
tailwindConfig: TailwindComponentConfig;
tailwindContext: JitContext;
} => {
hasTailwind: true;
tailwindConfig: TailwindComponentConfig;
tailwindContext: JitContext;
} => {
const tailwindComponentNode = esquery(
sourceCode.ast,
'JSXOpeningElement[name.name="Tailwind"]',
Expand Down

0 comments on commit e77ce12

Please sign in to comment.