Merge branch 'main' into next

# Conflicts:
#	.eslintrc.cjs
#	.github/workflows/prod-test.yml
#	.github/workflows/validate-vendors.yml
#	scripts/build/bundler.mjs
#	scripts/vendors/bundle-vendors.mjs
#	scripts/vendors/utils.mjs
#	scripts/vendors/vendor-meta.json
#	scripts/vendors/vendors.mjs
#	src/cli/logger.js
#	src/cli/options/get-options-for-file.js
#	src/cli/options/parse-cli-arguments.js
#	src/common/util.js
#	src/language-html/utils/html-tag-names.js
#	tests/integration/__tests__/bundle.js
#	tests/integration/__tests__/loglevel.js
#	tests/integration/run-prettier.js
#	vendors/string-width.js
This commit is contained in:
fisker Cheung committed 2022-04-06 15:41:50 +08:00
commit 6016404f59
44 files changed
+689 -508

No files matched your search

+5 -28
View File
@@ -1,37 +1,14 @@
"use strict";
const fs = require("fs");
const vm = require("vm");
const fastGlob = require("fast-glob");
const createSandBox = require("./utils/create-sandbox.js");
const sandbox = vm.createContext();
const source = fastGlob
.sync(["standalone.js", "parser-*.js"], {
const sandbox = createSandBox({
files: fastGlob.sync(["standalone.js", "parser-*.js"], {
cwd: process.env.PRETTIER_DIR,
absolute: true,
})
.map((file) => fs.readFileSync(file, "utf8"))
.join(";");
vm.runInContext(source, sandbox);
const allowedGlobalObjects = new Set(["prettier", "prettierPlugins"]);
const globalObjects = Object.keys(sandbox).filter(
(property) => !allowedGlobalObjects.has(property)
);
if (globalObjects.length > 0) {
throw new Error(
`Global ${globalObjects
.map(
(property) =>
`"${property}"(${Object.prototype.toString
.call(sandbox[property])
.slice(8, -1)})`
)
.join(", ")} should not be exposed.`
);
}
}),
});
// TODO: maybe expose (and write tests) for `format`, `utils`, and
// `__debug` methods
+13
View File
@@ -0,0 +1,13 @@
import vm from "node:vm";
import fs from "node:fs";
function createSandBox({ files }) {
const source = files.map((file) => fs.readFileSync(file, "utf8")).join(";");
const sandbox = vm.createContext();
vm.runInContext(source, sandbox);
return sandbox;
}
export default createSandBox;
@@ -325,6 +325,28 @@ class B {
================================================================================
`;
exports[`super.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
| printWidth
=====================================input======================================
class A extends B {
#a() {
super.x();
}
}
=====================================output=====================================
class A extends B {
#a() {
super.x();
}
}
================================================================================
`;
exports[`ternary.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
+5
View File
@@ -0,0 +1,5 @@
class A extends B {
#a() {
super.x();
}
}
@@ -28,6 +28,22 @@ printWidth: 80
================================================================================
`;
exports[`multiple-flags.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
| printWidth
=====================================input======================================
/.*/ms;
/.*/my;
=====================================output=====================================
/.*/ms;
/.*/my;
================================================================================
`;
exports[`test.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
+2
View File
@@ -0,0 +1,2 @@
/.*/ms;
/.*/my;
@@ -13,3 +13,19 @@ await something();
================================================================================
`;
exports[`in-expression.js format 1`] = `
====================================options=====================================
parsers: ["babel", "typescript"]
printWidth: 80
| printWidth
=====================================input======================================
call(
await something()
)
=====================================output=====================================
call(await something());
================================================================================
`;
@@ -0,0 +1,3 @@
call(
await something()
)
@@ -5076,6 +5076,94 @@ singleQuote: true
================================================================================
`;
exports[`regex.js - {"singleQuote":false,"jsxSingleQuote":false} format 1`] = `
====================================options=====================================
jsxSingleQuote: false
parsers: ["flow", "babel", "typescript"]
printWidth: 80
singleQuote: false
| printWidth
=====================================input======================================
x = <div>one</div>, <div>two</div>;
x = <a>{}</a>
x = <a>{1/2}</a>
x = <a>{/w/.test(s)}</a>
=====================================output=====================================
(x = <div>one</div>), (<div>two</div>);
x = <a>{}</a>;
x = <a>{1 / 2}</a>;
x = <a>{/w/.test(s)}</a>;
================================================================================
`;
exports[`regex.js - {"singleQuote":false,"jsxSingleQuote":true} format 1`] = `
====================================options=====================================
jsxSingleQuote: true
parsers: ["flow", "babel", "typescript"]
printWidth: 80
singleQuote: false
| printWidth
=====================================input======================================
x = <div>one</div>, <div>two</div>;
x = <a>{}</a>
x = <a>{1/2}</a>
x = <a>{/w/.test(s)}</a>
=====================================output=====================================
(x = <div>one</div>), (<div>two</div>);
x = <a>{}</a>;
x = <a>{1 / 2}</a>;
x = <a>{/w/.test(s)}</a>;
================================================================================
`;
exports[`regex.js - {"singleQuote":true,"jsxSingleQuote":false} format 1`] = `
====================================options=====================================
jsxSingleQuote: false
parsers: ["flow", "babel", "typescript"]
printWidth: 80
singleQuote: true
| printWidth
=====================================input======================================
x = <div>one</div>, <div>two</div>;
x = <a>{}</a>
x = <a>{1/2}</a>
x = <a>{/w/.test(s)}</a>
=====================================output=====================================
(x = <div>one</div>), (<div>two</div>);
x = <a>{}</a>;
x = <a>{1 / 2}</a>;
x = <a>{/w/.test(s)}</a>;
================================================================================
`;
exports[`regex.js - {"singleQuote":true,"jsxSingleQuote":true} format 1`] = `
====================================options=====================================
jsxSingleQuote: true
parsers: ["flow", "babel", "typescript"]
printWidth: 80
singleQuote: true
| printWidth
=====================================input======================================
x = <div>one</div>, <div>two</div>;
x = <a>{}</a>
x = <a>{1/2}</a>
x = <a>{/w/.test(s)}</a>
=====================================output=====================================
(x = <div>one</div>), (<div>two</div>);
x = <a>{}</a>;
x = <a>{1 / 2}</a>;
x = <a>{/w/.test(s)}</a>;
================================================================================
`;
exports[`return-statement.js - {"singleQuote":false,"jsxSingleQuote":false} format 1`] = `
====================================options=====================================
jsxSingleQuote: false
+4
View File
@@ -0,0 +1,4 @@
x = <div>one</div>, <div>two</div>;
x = <a>{}</a>
x = <a>{1/2}</a>
x = <a>{/w/.test(s)}</a>
+51 -11
View File
@@ -1,7 +1,9 @@
import fs from "node:fs";
import path from "node:path";
import createEsmUtils from "esm-utils";
import fastGlob from "fast-glob";
import { projectRoot } from "../env.js";
import createSandBox from "../../config/utils/create-sandbox.js";
import * as coreOptions from "../../../src/main/core-options.js";
import codeSamples from "../../../website/playground/codeSamples.mjs";
import jestPathSerializer from "../path-serializer.js";
@@ -15,17 +17,19 @@ const parserNames = coreOptions.options.parser.choices.map(
);
const distDirectory = path.join(projectRoot, "dist");
let esmStandalone;
let esmPlugins;
beforeAll(async () => {
({ default: esmStandalone } = await import(
path.join(distDirectory, "esm/standalone.mjs")
));
esmPlugins = await Promise.all(
fastGlob
.sync(["esm/parser-*.mjs"], { cwd: distDirectory, absolute: true })
.map(async (file) => (await import(file)).default)
);
// 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);
}
});
describe("standalone", () => {
@@ -34,6 +38,19 @@ describe("standalone", () => {
.sync(["parser-*.js"], { cwd: distDirectory, absolute: true })
.map((file) => require(file));
let esmStandalone;
let esmPlugins;
beforeAll(async () => {
({ default: esmStandalone } = await import(
path.join(distDirectory, "esm/standalone.mjs")
));
esmPlugins = await Promise.all(
fastGlob
.sync(["esm/parser-*.mjs"], { cwd: distDirectory, absolute: true })
.map(async (file) => (await import(file)).default)
);
});
for (const parser of parserNames) {
test(parser, () => {
const input = codeSamples(parser);
@@ -55,3 +72,26 @@ describe("standalone", () => {
});
}
});
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({});
}
});