2022-04-06 15:41:50 +08:00
|
|
|
import fs from "node:fs";
|
2021-12-02 10:19:32 +08:00
|
|
|
import path from "node:path";
|
|
|
|
|
import createEsmUtils from "esm-utils";
|
2022-02-09 10:11:46 +08:00
|
|
|
import fastGlob from "fast-glob";
|
2021-12-02 10:19:32 +08:00
|
|
|
import { projectRoot } from "../env.js";
|
2022-04-06 16:05:22 +08:00
|
|
|
import createSandBox from "../../config/utils/create-sandbox.cjs";
|
2022-02-23 20:16:30 +08:00
|
|
|
import * as coreOptions from "../../../src/main/core-options.js";
|
2021-12-02 10:19:32 +08:00
|
|
|
import codeSamples from "../../../website/playground/codeSamples.mjs";
|
|
|
|
|
import jestPathSerializer from "../path-serializer.js";
|
2020-09-17 01:08:44 +09:00
|
|
|
|
2021-12-02 10:19:32 +08:00
|
|
|
const { require } = createEsmUtils(import.meta);
|
|
|
|
|
|
|
|
|
|
expect.addSnapshotSerializer(jestPathSerializer);
|
2020-09-17 01:08:44 +09:00
|
|
|
|
|
|
|
|
const parserNames = coreOptions.options.parser.choices.map(
|
|
|
|
|
({ value }) => value
|
|
|
|
|
);
|
|
|
|
|
const distDirectory = path.join(projectRoot, "dist");
|
|
|
|
|
|
2022-04-02 16:14:32 +08:00
|
|
|
// Files including U+FFEE can't load in Chrome Extension
|
|
|
|
|
// `prettier-chrome-extension` https://github.com/prettier/prettier-chrome-extension
|
|
|
|
|
// details https://github.com/prettier/prettier/pull/8534
|
|
|
|
|
test("code", async () => {
|
|
|
|
|
const files = await fastGlob(["**/*"], {
|
|
|
|
|
cwd: distDirectory,
|
|
|
|
|
absolute: true,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
for (const file of files) {
|
|
|
|
|
const text = await fs.promises.readFile(file, "utf8");
|
|
|
|
|
expect(text.includes("\ufffe")).toBe(false);
|
|
|
|
|
}
|
2021-12-02 10:19:32 +08:00
|
|
|
});
|
|
|
|
|
|
2020-09-17 01:08:44 +09:00
|
|
|
describe("standalone", () => {
|
|
|
|
|
const standalone = require(path.join(distDirectory, "standalone.js"));
|
2022-02-05 16:24:10 +03:00
|
|
|
const plugins = fastGlob
|
2020-09-17 01:08:44 +09:00
|
|
|
.sync(["parser-*.js"], { cwd: distDirectory, absolute: true })
|
|
|
|
|
.map((file) => require(file));
|
|
|
|
|
|
2022-04-06 15:41:50 +08:00
|
|
|
let esmStandalone;
|
|
|
|
|
let esmPlugins;
|
|
|
|
|
beforeAll(async () => {
|
2022-05-05 15:03:08 +08:00
|
|
|
esmStandalone = await import(
|
2022-04-06 15:41:50 +08:00
|
|
|
path.join(distDirectory, "esm/standalone.mjs")
|
2022-05-05 15:03:08 +08:00
|
|
|
);
|
2022-04-06 15:41:50 +08:00
|
|
|
esmPlugins = await Promise.all(
|
|
|
|
|
fastGlob
|
|
|
|
|
.sync(["esm/parser-*.mjs"], { cwd: distDirectory, absolute: true })
|
|
|
|
|
.map(async (file) => (await import(file)).default)
|
|
|
|
|
);
|
|
|
|
|
});
|
2020-09-17 01:08:44 +09:00
|
|
|
|
|
|
|
|
for (const parser of parserNames) {
|
2022-04-29 14:22:24 +08:00
|
|
|
test(parser, async () => {
|
2020-09-17 01:08:44 +09:00
|
|
|
const input = codeSamples(parser);
|
2022-04-29 14:22:24 +08:00
|
|
|
const umdOutput = await standalone.format(input, {
|
2020-09-17 01:08:44 +09:00
|
|
|
parser,
|
|
|
|
|
plugins,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(typeof input).toBe("string");
|
|
|
|
|
expect(typeof umdOutput).toBe("string");
|
|
|
|
|
expect(umdOutput).not.toBe(input);
|
|
|
|
|
|
2022-04-29 14:22:24 +08:00
|
|
|
const esmOutput = await esmStandalone.format(input, {
|
2020-09-17 01:08:44 +09:00
|
|
|
parser,
|
|
|
|
|
plugins: esmPlugins,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(esmOutput).toBe(umdOutput);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
2022-04-02 16:14:32 +08:00
|
|
|
|
|
|
|
|
test("global objects", async () => {
|
|
|
|
|
const files = await fastGlob(["standalone.js", "parser-*.js"], {
|
|
|
|
|
cwd: distDirectory,
|
|
|
|
|
absolute: true,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const allowedGlobalObjects = new Set(["prettier", "prettierPlugins"]);
|
|
|
|
|
const getGlobalObjects = (file) => {
|
|
|
|
|
const sandbox = createSandBox({ files: [file] });
|
|
|
|
|
return Object.fromEntries(
|
|
|
|
|
Object.entries(sandbox).filter(
|
|
|
|
|
([property]) => !allowedGlobalObjects.has(property)
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
for (const file of files) {
|
|
|
|
|
const globalObjects = getGlobalObjects(file);
|
|
|
|
|
|
|
|
|
|
expect(globalObjects).toStrictEqual({});
|
|
|
|
|
}
|
|
|
|
|
});
|