Skip to content

Commit e800d69

Browse files
committed
pre-load plugins in standalone
1 parent a7e0dcb commit e800d69

File tree

3 files changed

+50
-11
lines changed

3 files changed

+50
-11
lines changed

tests/config/get-plugins.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { createRequire } from "node:module";
2+
import path from "node:path";
3+
import createEsmUtils from "esm-utils";
4+
import { satisfies } from "semver";
5+
import getPrettier from "./get-prettier.js";
6+
7+
const { __dirname } = createEsmUtils(import.meta);
8+
9+
async function getPluginsInternal() {
10+
if (!process.env.TEST_STANDALONE) {
11+
return [path.join(__dirname, "../../src/index.ts")];
12+
}
13+
14+
const require = createRequire(import.meta.url);
15+
const prettier = await getPrettier();
16+
17+
const prettierPath = path.dirname(require.resolve("prettier"));
18+
const pluginPrefix = satisfies(prettier.version, "^2.3.0")
19+
? "parser-" // Prettier V2
20+
: "plugins/"; // Prettier V3
21+
const babel = path.join(prettierPath, `${pluginPrefix}babel.js`);
22+
const markdown = path.join(prettierPath, `${pluginPrefix}markdown.js`);
23+
const standalone = path.join(__dirname, "../../dist/standalone.js");
24+
25+
return [
26+
(await import(babel)).default,
27+
(await import(markdown)).default,
28+
(await import(standalone)).default,
29+
];
30+
}
31+
32+
let promise;
33+
function getPlugins() {
34+
promise = promise ?? getPluginsInternal();
35+
36+
return promise;
37+
}
38+
39+
export default getPlugins;

tests/config/run-format-test.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ import url from "node:url";
55
import createEsmUtils from "esm-utils";
66

77
import getPrettier from "./get-prettier.js";
8+
import getPlugins from "./get-plugins.js";
89
import compileContract from "./utils/compile-contract.js";
910
import consistentEndOfLine from "./utils/consistent-end-of-line.js";
1011
import createSnapshot from "./utils/create-snapshot.js";
1112
import stringifyOptionsForTitle from "./utils/stringify-options-for-title.js";
1213
import visualizeEndOfLine from "./utils/visualize-end-of-line.js";
13-
import standalone from "../../dist/standalone.js";
1414

1515
const { __dirname } = createEsmUtils(import.meta);
1616

17-
const { FULL_TEST, TEST_STANDALONE } = process.env;
17+
const { FULL_TEST } = process.env;
1818
const BOM = "\uFEFF";
1919

2020
const CURSOR_PLACEHOLDER = "<|>";
@@ -190,9 +190,6 @@ function runFormatTest(fixtures, parsers, options) {
190190

191191
describe(title, () => {
192192
const formatOptions = {
193-
plugins: TEST_STANDALONE
194-
? [standalone]
195-
: [path.join(__dirname, "../../src/index.ts")],
196193
printWidth: 80,
197194
...options,
198195
filepath: filename,
@@ -381,9 +378,12 @@ function shouldSkipEolTest(code, options) {
381378

382379
async function parse(source, options) {
383380
const prettier = await getPrettier();
384-
const { ast } = await prettier.__debug.parse(source, options, {
385-
massage: true,
386-
});
381+
382+
const { ast } = await prettier.__debug.parse(
383+
source,
384+
{ ...options, plugins: await getPlugins() },
385+
{ massage: true },
386+
);
387387
return ast;
388388
}
389389

@@ -437,7 +437,7 @@ async function format(originalText, originalOptions) {
437437

438438
const { formatted: output, cursorOffset } = await prettier.formatWithCursor(
439439
input,
440-
options,
440+
{ ...options, plugins: await getPlugins() },
441441
);
442442
const outputWithCursor = insertCursor(output, cursorOffset);
443443
const eolVisualizedOutput = visualizeEndOfLine(outputWithCursor);

tests/format/ModifierInvocations/__snapshots__/format.test.js.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ printWidth: 80
77
| printWidth
88
=====================================input======================================
99
// SPDX-License-Identifier: MIT
10-
pragma solidity 0.8.27;
10+
pragma solidity 0.8.28;
1111
1212
contract ModifierDefinitions {
1313
// We enforce the use of parentheses in modifiers without parameters.
@@ -26,7 +26,7 @@ contract ModifierInvocations is ModifierDefinitions {
2626
2727
=====================================output=====================================
2828
// SPDX-License-Identifier: MIT
29-
pragma solidity 0.8.27;
29+
pragma solidity 0.8.28;
3030
3131
contract ModifierDefinitions {
3232
// We enforce the use of parentheses in modifiers without parameters.

0 commit comments

Comments
 (0)