Some checks failed
App Prod-Parity E2E Dispatch / dispatch (push) Failing after 10s
CD Storybook UI / build (push) Failing after 3m25s
CD Storybook UI / dispatch (push) Has been skipped
CD deploy main / website-changed-files (push) Successful in 18m8s
CI Agent Skills / changed-files-check (push) Successful in 18m9s
CI Create App E2E minimal / changed-files-check (push) Successful in 24s
CI Create App / changed-files-check (push) Successful in 26s
CI Docs / changed-files-check (push) Successful in 26s
CI Emails / changed-files-check (push) Successful in 28s
CI Example App Hello World / changed-files-check (push) Successful in 23s
CI Front / changed-files-check (push) Has been skipped
CI Example App Postcard / changed-files-check (push) Successful in 27s
CI UI / changed-files-check (push) Has been skipped
CI Utils Test / changed-files-check (push) Successful in 45s
CI Twenty Apps / discover (push) Successful in 1m3s
CD deploy main / deploy-main (push) Failing after 3s
CI Agent Skills / agent-skills-validate (push) Has been skipped
CI Create App / create-app-test (lint) (push) Has been skipped
CI Emails / emails-test (push) Has been skipped
CI Front / front-task (lingui:compile) (push) Has been skipped
CI Front / front-task (lint) (push) Has been skipped
CI Example App Postcard / example-app-postcard (push) Has been skipped
CI UI / ui-task (lint) (push) Has been skipped
CI UI / ui-task (pack-size) (push) Has been skipped
CI UI / ui-task (size) (push) Has been skipped
CI UI / ui-task (test) (push) Has been skipped
CI UI / ui-task (typecheck) (push) Has been skipped
CI Utils Test / utils-test (push) Has been skipped
CI Twenty Apps / ${{ matrix.app.name }} (push) Has been skipped
CI Emails / ci-emails-status-check (push) Successful in 2s
CI Example App Hello World / ci-example-app-hello-world-status-check (push) Successful in 2s
CI Example App Postcard / ci-example-app-postcard-status-check (push) Successful in 3s
CI UI / ui-sb-test (push) Has been skipped
CI Utils Test / ci-utils-test-status-check (push) Successful in 2s
CI Twenty Apps / ci-twenty-apps-status-check (push) Successful in 2s
CI UI / ci-ui-status-check (push) Successful in 2s
CI Desktop / desktop (push) Failing after 1m8s
Push docs to Crowdin / Push documentation to Crowdin (push) Failing after 55s
Push translations to Crowdin / Extract and upload translations (push) Failing after 5s
Push website translations to Crowdin / Extract and upload website translations (push) Failing after 5s
CI Create App / create-app-test (test) (push) Has been skipped
CI Create App / create-app-test (typecheck) (push) Has been skipped
CI Docs / docs-lint (push) Has been skipped
CI Example App Hello World / example-app-hello-world (push) Has been skipped
CI Front / front-task (lingui:extract) (push) Has been skipped
CI Front / front-task (test) (push) Has been skipped
CI Front / front-task (typecheck) (push) Has been skipped
CI UI / ui-sb-build (push) Failing after 1m32s
CI Twenty Apps / ${{ matrix.app.name }} (${{ matrix.server-source }}) (push) Has been skipped
CI Agent Skills / agent-skills-publish (push) Has been skipped
CI Create App / ci-create-app-status-check (push) Successful in 2s
CI Agent Skills / agent-skills-remote-source (push) Has been skipped
CI Create App E2E minimal / create-app-e2e-minimal (push) Has been cancelled
CI E2E Main / e2e-test (push) Has been cancelled
CI Front / front-sb-build (push) Has been cancelled
CI Front / front-build (push) Has been cancelled
CI Create App E2E minimal / ci-create-app-e2e-minimal-status-check (push) Has been cancelled
CI E2E Main / ci-e2e-main-status-check (push) Has been cancelled
CI E2E Main / notify-main-ci-failure (push) Has been cancelled
CI E2E Main / qa-scout-comment (push) Has been cancelled
CI Front / front-sb-test (push) Has been cancelled
CI Front / front-sb-screenshots (push) Has been cancelled
CI Front / ci-front-status-check (push) Has been cancelled
DPA Sub-Processor Sync / sync-subprocessors (push) Failing after 3s
AI Catalog Sync / sync-catalog (push) Failing after 2s
271 lines
6.4 KiB
TypeScript
271 lines
6.4 KiB
TypeScript
import postcss from 'postcss';
|
|
import { defineRule } from '@oxlint/plugins';
|
|
|
|
export const RULE_NAME = 'sort-css-properties-alphabetically';
|
|
|
|
interface Loc {
|
|
start: {
|
|
line: number;
|
|
column: number;
|
|
};
|
|
end: {
|
|
line: number;
|
|
column: number;
|
|
};
|
|
}
|
|
|
|
const isMemberExpression = (node: any): boolean =>
|
|
node.type === 'MemberExpression';
|
|
const isCallExpression = (node: any): boolean =>
|
|
node.type === 'CallExpression';
|
|
|
|
const isStyledTagname = (node: any): boolean => {
|
|
if (node.tag?.type === 'Identifier') {
|
|
return node.tag.name === 'css';
|
|
}
|
|
|
|
if (
|
|
isMemberExpression(node.tag) &&
|
|
node.tag.object?.type === 'Identifier'
|
|
) {
|
|
return node.tag.object.name === 'styled';
|
|
}
|
|
|
|
if (
|
|
isCallExpression(node.tag) &&
|
|
node.tag.callee?.type === 'Identifier'
|
|
) {
|
|
return node.tag.callee.name === 'styled';
|
|
}
|
|
|
|
if (
|
|
isCallExpression(node.tag) &&
|
|
isMemberExpression(node.tag.callee) &&
|
|
node.tag.callee.object?.type === 'Identifier'
|
|
) {
|
|
return node.tag.callee.object.name === 'styled';
|
|
}
|
|
|
|
if (
|
|
isCallExpression(node.tag) &&
|
|
isMemberExpression(node.tag.callee) &&
|
|
isMemberExpression(node.tag.callee.object) &&
|
|
node.tag.callee.object.object?.type === 'Identifier'
|
|
) {
|
|
return node.tag.callee.object.object.name === 'styled';
|
|
}
|
|
|
|
return false;
|
|
};
|
|
|
|
const isValidAtomicRule = (
|
|
rule: postcss.Rule,
|
|
): { isValid: boolean; loc?: Loc } => {
|
|
const decls = rule.nodes.filter(
|
|
(node) => node.type === 'decl',
|
|
) as unknown as postcss.Declaration[];
|
|
|
|
const invalidDeclIndex = decls.findIndex((decl, index) => {
|
|
if (index === 0) return false;
|
|
|
|
const current = decl.prop;
|
|
const prev = decls[index - 1].prop;
|
|
|
|
return current < prev;
|
|
});
|
|
|
|
return invalidDeclIndex > 0
|
|
? {
|
|
isValid: false,
|
|
loc: {
|
|
start: {
|
|
line: decls[invalidDeclIndex - 1].source!.start!.line,
|
|
column:
|
|
decls[invalidDeclIndex - 1].source!.start!.column - 1,
|
|
},
|
|
end: {
|
|
line: decls[invalidDeclIndex].source!.end!.line,
|
|
column: decls[invalidDeclIndex].source!.end!.column - 1,
|
|
},
|
|
},
|
|
}
|
|
: { isValid: true };
|
|
};
|
|
|
|
const isValidRule = (
|
|
rule: postcss.Rule,
|
|
): { isValid: boolean; loc?: Loc } => {
|
|
const { isValid, loc } = rule.nodes.reduce<{
|
|
isValid: boolean;
|
|
loc?: Loc;
|
|
}>(
|
|
(map, node) => {
|
|
return node.type === 'rule' ? isValidRule(node) : map;
|
|
},
|
|
{ isValid: true },
|
|
);
|
|
|
|
if (!isValid) {
|
|
return { isValid, loc };
|
|
}
|
|
|
|
return isValidAtomicRule(rule);
|
|
};
|
|
|
|
const getNodeStyles = (node: any): string => {
|
|
const [firstQuasi, ...quasis] = node.quasi.quasis;
|
|
const lineBreakCount = node.quasi.loc.start.line - 1;
|
|
let styles = `${'\n'.repeat(lineBreakCount)}${' '.repeat(
|
|
node.quasi.loc.start.column + 1,
|
|
)}${firstQuasi.value.raw}`;
|
|
|
|
quasis.forEach(({ value, loc }: any, idx: number) => {
|
|
const prevLoc = idx === 0 ? firstQuasi.loc : quasis[idx - 1].loc;
|
|
const lineBreaksCount = loc.start.line - prevLoc.end.line;
|
|
const spacesCount =
|
|
loc.start.line === prevLoc.end.line
|
|
? loc.start.column - prevLoc.end.column + 2
|
|
: loc.start.column + 1;
|
|
styles = `${styles}${' '}${'\n'.repeat(lineBreaksCount)}${' '.repeat(
|
|
spacesCount,
|
|
)}${value.raw}`;
|
|
});
|
|
|
|
return styles;
|
|
};
|
|
|
|
const fix = ({
|
|
rule,
|
|
fixer,
|
|
src,
|
|
}: {
|
|
rule: postcss.Rule;
|
|
fixer: any;
|
|
src: any;
|
|
}): any[] => {
|
|
const fixings = rule.nodes
|
|
.filter((node): node is postcss.Rule => node.type === 'rule')
|
|
.flatMap((node) => fix({ rule: node, fixer, src }));
|
|
|
|
const declarations = rule.nodes.filter(
|
|
(node): node is postcss.Declaration => node.type === 'decl',
|
|
);
|
|
const sortedDeclarations = sortDeclarations(declarations);
|
|
|
|
return [
|
|
...fixings,
|
|
...declarations.flatMap((decl, index) => {
|
|
if (!areSameDeclarations(decl, sortedDeclarations[index])) {
|
|
try {
|
|
const range = getDeclRange({ decl, src });
|
|
const sortedDeclText = getDeclText({
|
|
decl: sortedDeclarations[index],
|
|
src,
|
|
});
|
|
|
|
return [
|
|
fixer.removeRange([range.startIdx, range.endIdx + 1]),
|
|
fixer.insertTextAfterRange(
|
|
[range.startIdx, range.startIdx],
|
|
sortedDeclText,
|
|
),
|
|
];
|
|
} catch (e) {
|
|
console.log(e);
|
|
}
|
|
}
|
|
}),
|
|
];
|
|
};
|
|
|
|
const areSameDeclarations = (
|
|
a: postcss.ChildNode,
|
|
b: postcss.ChildNode,
|
|
): boolean =>
|
|
a.source!.start!.line === b.source!.start!.line &&
|
|
a.source!.start!.column === b.source!.start!.column;
|
|
|
|
const getDeclRange = ({
|
|
decl,
|
|
src,
|
|
}: {
|
|
decl: postcss.ChildNode;
|
|
src: any;
|
|
}): { startIdx: number; endIdx: number } => {
|
|
const loc = {
|
|
start: {
|
|
line: decl.source!.start!.line,
|
|
column: decl.source!.start!.column - 1,
|
|
},
|
|
end: {
|
|
line: decl.source!.end!.line,
|
|
column: decl.source!.end!.column - 1,
|
|
},
|
|
};
|
|
|
|
const startIdx = src.getIndexFromLoc(loc.start);
|
|
const endIdx = src.getIndexFromLoc(loc.end);
|
|
return { startIdx, endIdx };
|
|
};
|
|
|
|
const getDeclText = ({
|
|
decl,
|
|
src,
|
|
}: {
|
|
decl: postcss.ChildNode;
|
|
src: any;
|
|
}) => {
|
|
const { startIdx, endIdx } = getDeclRange({ decl, src });
|
|
return src.getText().substring(startIdx, endIdx + 1);
|
|
};
|
|
|
|
const sortDeclarations = (declarations: postcss.Declaration[]) =>
|
|
declarations
|
|
.slice()
|
|
.sort((declA, declB) => (declA.prop > declB.prop ? 1 : -1));
|
|
|
|
export const rule = defineRule({
|
|
meta: {
|
|
docs: {
|
|
description: 'Styles are sorted alphabetically.',
|
|
},
|
|
messages: {
|
|
sortCssPropertiesAlphabetically:
|
|
'Declarations should be sorted alphabetically.',
|
|
},
|
|
type: 'suggestion',
|
|
schema: [],
|
|
fixable: 'code',
|
|
},
|
|
create: (context) => {
|
|
return {
|
|
TaggedTemplateExpression: (node: any) => {
|
|
if (!isStyledTagname(node)) return;
|
|
|
|
try {
|
|
const root = postcss.parse(
|
|
getNodeStyles(node),
|
|
) as unknown as postcss.Rule;
|
|
|
|
const { isValid } = isValidRule(root);
|
|
|
|
if (!isValid) {
|
|
return context.report({
|
|
node,
|
|
messageId: 'sortCssPropertiesAlphabetically',
|
|
fix: (fixer) =>
|
|
fix({
|
|
rule: root,
|
|
fixer,
|
|
src: context.sourceCode,
|
|
}),
|
|
});
|
|
}
|
|
} catch (e) {
|
|
return true;
|
|
}
|
|
},
|
|
};
|
|
},
|
|
});
|