Skip to content

Commit

Permalink
Merge pull request #5 from ony3000/improve-performance
Browse files Browse the repository at this point in the history
Improve performance
  • Loading branch information
ony3000 committed Sep 24, 2023
2 parents fc4caad + 6f7a8c9 commit 6a9c110
Show file tree
Hide file tree
Showing 10 changed files with 201 additions and 78 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
**/*.*js
**/*.jsx
**/*.d.ts
11 changes: 10 additions & 1 deletion .prettierrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,14 @@
"trailingComma": "all",
"plugins": ["@trivago/prettier-plugin-sort-imports"],
"importOrder": ["<THIRD_PARTY_MODULES>", "^@/(.*)$", "^\\.(.*)$"],
"importOrderSeparation": true
"importOrderSeparation": true,
"overrides": [
{
"files": ["tests/**/performance/Callout.*"],
"options": {
"printWidth": 80,
"singleQuote": false
}
}
]
}
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
"dev": "vite build --watch --minify=false",
"build": "vite build",
"lint": "eslint src",
"test": "jest --ci --passWithNoTests",
"test:watch": "jest --watch"
"test": "pnpm run build && jest --ci --passWithNoTests"
},
"dependencies": {
"diff": "5.1.0"
Expand All @@ -37,6 +36,7 @@
"@trivago/prettier-plugin-sort-imports": "4.1.1",
"@types/diff": "5.0.3",
"@types/jest": "29.5.2",
"@types/node": "20.2.5",
"@typescript-eslint/eslint-plugin": "5.54.0",
"@typescript-eslint/parser": "5.54.0",
"eslint": "8.35.0",
Expand All @@ -47,6 +47,8 @@
"eslint-import-resolver-typescript": "3.5.3",
"eslint-plugin-import": "2.27.5",
"jest": "29.5.0",
"ppm-012": "npm:prettier-plugin-merge@0.1.2",
"ppm-020": "npm:prettier-plugin-merge@0.2.0",
"prettier": "~2.8.4",
"prettier-plugin-brace-style": "0.2.1",
"prettier-plugin-classnames": "0.1.1",
Expand Down
102 changes: 39 additions & 63 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 10 additions & 11 deletions src/printers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,20 @@ function sequentialFormattingAndTryMerging(
rangeEnd: Infinity,
};

let sequentiallyFormattedText = originalText;
let sequentiallyMergedText: string | undefined;
let sequentiallyMergedText = originalText;

/**
* Changes that may be removed during the sequential formatting process.
*/
const patches: SubstitutePatch[] = [];

plugins.forEach((plugin) => {
sequentiallyFormattedText = format(sequentiallyFormattedText, {
const temporaryFormattedText = format(sequentiallyMergedText, {
...sequentialFormattingOptions,
plugins: [plugin],
});

const temporaryFormattedText = format(sequentiallyMergedText ?? originalText, {
...sequentialFormattingOptions,
plugins: [plugin],
});

if (sequentiallyMergedText === undefined) {
if (sequentiallyMergedText === originalText) {
sequentiallyMergedText = temporaryFormattedText;
return;
}
Expand Down Expand Up @@ -106,7 +100,7 @@ function sequentialFormattingAndTryMerging(
);
});

return sequentiallyMergedText ?? sequentiallyFormattedText;
return sequentiallyMergedText;
}

function createPrinter(): Printer {
Expand All @@ -125,6 +119,7 @@ function createPrinter(): Printer {
throw new Error('A default plugin with the detected parser does not exist.');
}

const parserName = options.parser as string;
const node = path.getValue();

if (node?.comments) {
Expand All @@ -149,9 +144,13 @@ function createPrinter(): Printer {
);
}

const parserImplementedPlugins = plugins
.slice(0, pluginIndex)
.filter((plugin) => plugin.parsers?.[parserName]);

return sequentialFormattingAndTryMerging(
options,
plugins.slice(0, pluginIndex),
parserImplementedPlugins,
defaultPluginCandidate,
);
}
Expand Down
11 changes: 11 additions & 0 deletions tests/babel/performance/Callout.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// @ts-nocheck
export default function Callout({ children }) {
return (
<div
className="rounded-xl border border-zinc-400/30 bg-gray-100/50 px-4 py-4
dark:border-neutral-500/30 dark:bg-neutral-900/50"
>
{children}
</div>
);
}
Loading

0 comments on commit 6a9c110

Please sign in to comment.