Skip to content

Commit

Permalink
refactor: Redesign helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
youthfulhps committed Dec 10, 2023
1 parent 0c4322f commit e8917b9
Show file tree
Hide file tree
Showing 16 changed files with 53 additions and 57 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import TAILWINDCLASS from '../preprocessor/constants';
import { StyleDeclaration, StyleRule } from '~/helpers/extractor';
import { preprocessProperty, preprocessValue } from '../preprocessor';
import { preprocessShorthand } from '~/helpers/preprocessor/shorthand';
import { StyleDeclaration, StyleRule } from '~/types';
import {
preprocessProperty,
preprocessValue,
preprocessShorthand,
TAILWINDCLASS,
} from './preprocessor';

export function convertStyles(styles: StyleRule[]) {
const rules = preprocessShorthand(styles);

return rules.reduce((combinedStyles, rule) => {
const selectors = convertSelector(rule.selectors);
const utilities = rule.declarations.map((declaration) =>
const utilities = rule.declarations.map((declaration: StyleDeclaration) =>
convertCss(declaration),
);

Expand All @@ -18,7 +21,7 @@ export function convertStyles(styles: StyleRule[]) {
(combinedSelectors, selector) =>
combinedSelectors +
utilities.reduce(
(combinedUtilities, util) =>
(combinedUtilities: string, util: string) =>
combinedUtilities + `${selector}${selector ? ':' : ''}${util} `,
'',
),
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { convertUnit } from '~/helpers/preprocessor/unit';
import { convertUnit } from './unit';

export function preprocessDimension(value: string) {
if (value === '0' || value === '0px') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import {
preprocessLetterSpacing,
preprocessLineHeight,
} from './text';
import { preprocessSpacing } from '~/helpers/preprocessor/spacing';
import { preprocessDimension } from '~/helpers/preprocessor/dimension';
import { preprocessSpacing } from './spacing';
import { preprocessDimension } from './dimension';

export { preprocessShorthand } from './shorthand';
export { default as TAILWINDCLASS } from './constants';

export type CSSStyleEntity = {
property: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import TAILWINDCLASS from './constants';
import { StyleDeclaration, StyleRule } from '~/helpers/extractor';
import { isColor } from '~/helpers/preprocessor/color';
import { StyleDeclaration, StyleRule } from '~/types';
import { isColor } from './color';

export function preprocessShorthand(styleRules: StyleRule[]) {
return styleRules.map((styleRule) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { convertUnit } from '~/helpers/preprocessor/unit';
import { convertUnit } from './unit';

export function preprocessSpacing(value: string) {
return convertUnit(value, 'rem');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { convertUnit } from '~/helpers/preprocessor/unit';
import { convertUnit } from './unit';

export function preprocessFontWeight(value: string) {
switch (value) {
Expand Down
File renamed without changes.
35 changes: 8 additions & 27 deletions src/helpers/extractor.ts → src/helpers/css-extractor.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { AST } from 'prettier';
import {
generateConcatenatedCSSTemplateLiteral,
generateJSXOpeningElementClassNameAttribute,
} from '~/helpers/generator';
import { convertStyles } from '~/helpers/converter';
import { parseSass } from '~/helpers/css-parser';
manipulateConcatenatedCSSTemplateLiteral,
manipulateJSXOpeningElementClassNameAttribute,
} from './manipulator';
import { convertStyles } from './css-converter';
import { parseSass } from './css-parser';
import { ComponentDeclaration } from '~/types';

export function extractVariableDeclarations(ast: AST): any[] {
if (!ast.program || !ast.program.body.length) {
Expand All @@ -16,26 +17,6 @@ export function extractVariableDeclarations(ast: AST): any[] {
);
}

export type StyleRule = {
type: string;
selectors: string[];
declarations: StyleDeclaration[];
position: any;
};

export type StyleDeclaration = {
type: string;
property: string;
value: string;
position: any;
};

type ComponentDeclaration = {
name: string;
tag: string;
styles: StyleRule[];
};

export function isObject(arg: unknown): arg is object {
return typeof arg === 'object' && arg !== null;
}
Expand Down Expand Up @@ -98,7 +79,7 @@ export function getVariableDeclarationThroughStyledRecursively(ast: AST) {
'value' in node.init.quasi.quasis[0] &&
isObject(node.init.quasi.quasis[0].value)
) {
const sassScript = generateConcatenatedCSSTemplateLiteral(
const sassScript = manipulateConcatenatedCSSTemplateLiteral(
node.init.quasi.quasis,
);

Expand Down Expand Up @@ -168,7 +149,7 @@ export function overrideClassnameAttributeRecursively(
if (targetComponentDeclarations.length) {
const { tag, styles } = targetComponentDeclarations[0];

const newAttributes = generateJSXOpeningElementClassNameAttribute(
const newAttributes = manipulateJSXOpeningElementClassNameAttribute(
node.attributes,
convertStyles(styles),
);
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/generator.ts → src/helpers/manipulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// @ts-nocheck
import type { JSXAttribute, JSXSpreadAttribute } from 'estree-jsx';

export function generateJSXOpeningElementClassNameAttribute(
export function manipulateJSXOpeningElementClassNameAttribute(
attributes: (JSXAttribute | JSXSpreadAttribute)[],
attributeValue: string,
): JSXAttribute[] {
Expand Down Expand Up @@ -67,7 +67,7 @@ export function generateJSXOpeningElementClassNameAttribute(
return attributes;
}

export function generateConcatenatedCSSTemplateLiteral(quasis: any[]) {
export function manipulateConcatenatedCSSTemplateLiteral(quasis: any[]) {
return quasis.reduce((sum, curr) => {
if (!curr.tail) {
const replaced = curr.value.raw.replace(/\n|\s/g, '');
Expand Down
10 changes: 0 additions & 10 deletions src/helpers/reader.ts

This file was deleted.

6 changes: 3 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { format } from 'prettier';
import { scrapRawScript, writeFormattedScript } from '~/helpers/reader';
import plugin from './plugin';
import chalk from 'chalk';
import fs from 'fs/promises';

export default (async () => {
const args = process.argv.slice(2);

if (args[0] === '--target') {
try {
const filePath = args[1];
const rawScript = await scrapRawScript(filePath);
const rawScript = await fs.readFile(filePath, 'utf-8');

const formattedScript = format(rawScript, {
plugins: [plugin],
Expand All @@ -26,7 +26,7 @@ export default (async () => {
const directory = `${writeDirectory.join(
'/',
)}/${tailwindFileName}.${fileExtension}`;
await writeFormattedScript(directory, formattedScript);
await fs.writeFile(directory, formattedScript);

console.log(
chalk.green(
Expand Down
2 changes: 1 addition & 1 deletion src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { parsers as babelParsers } from 'prettier/parser-babel';
import { parsers as typescriptParsers } from 'prettier/parser-typescript';

import { printers } from './helpers/printer';
import { printers } from './printer';

export const parsers: { [parserName: string]: any } = {
babel: {
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/printer.ts → src/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
extractVariableDeclarations,
getVariableDeclarationThroughStyledRecursively,
overrideClassnameAttributeRecursively,
} from '~/helpers/extractor';
} from '~/helpers/css-extractor';

export function extractPrinter(options: ParserOptions): Printer | undefined {
const pluginOrNot = (
Expand Down
19 changes: 19 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export type StyleRule = {
type: string;
selectors: string[];
declarations: StyleDeclaration[];
position: any;
};

export type StyleDeclaration = {
type: string;
property: string;
value: string;
position: any;
};

export type ComponentDeclaration = {
name: string;
tag: string;
styles: StyleRule[];
};

0 comments on commit e8917b9

Please sign in to comment.