Skip to content

Commit

Permalink
Merge branch 'main' into aspect-ratio
Browse files Browse the repository at this point in the history
  • Loading branch information
FloEdelmann committed Apr 26, 2024
2 parents a7c1523 + 1264a4c commit fcc9f86
Show file tree
Hide file tree
Showing 21 changed files with 363 additions and 191 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/dev-test.yml
Expand Up @@ -69,8 +69,9 @@ jobs:
run: yarn c8 yarn test

- name: Upload Coverage
uses: codecov/codecov-action@v3
uses: codecov/codecov-action@v4
if: ${{ matrix.ENABLE_CODE_COVERAGE }}
with:
fail_ci_if_error: true
files: coverage/lcov.info
token: ${{ secrets.CODECOV_TOKEN }}
21 changes: 21 additions & 0 deletions changelog_unreleased/markdown/16158.md
@@ -0,0 +1,21 @@
#### Improve wrapping for markdown / mdx (#16158 by @seiyab)

```json
{ "proseWrap": "always" }
```

<!-- prettier-ignore -->
```md
<!-- Input -->
\
word very-very-very-very-very-very-very-very-very-very-long-word very-very-very-very-very-very-very-very-very-very-long-word

<!-- Prettier stable -->
\
word very-very-very-very-very-very-very-very-very-very-long-word very-very-very-very-very-very-very-very-very-very-long-word

<!-- Prettier main -->
\
word very-very-very-very-very-very-very-very-very-very-long-word
very-very-very-very-very-very-very-very-very-very-long-word
```
1 change: 1 addition & 0 deletions cspell.json
Expand Up @@ -38,6 +38,7 @@
"bottlenecked",
"Breakell",
"Brevik",
"bunx",
"Burgener",
"callees",
"camelcase",
Expand Down
10 changes: 8 additions & 2 deletions docs/configuration.md
Expand Up @@ -37,7 +37,10 @@ JS (ES Modules):
```js
// prettier.config.js, .prettierrc.js, prettier.config.mjs, or .prettierrc.mjs

/** @type {import("prettier").Config} */
/**
* @see https://prettier.io/docs/en/configuration.html
* @type {import("prettier").Config}
*/
const config = {
trailingComma: "es5",
tabWidth: 4,
Expand All @@ -53,7 +56,10 @@ JS (CommonJS):
```js
// prettier.config.js, .prettierrc.js, prettier.config.cjs, or .prettierrc.cjs

/** @type {import("prettier").Config} */
/**
* @see https://prettier.io/docs/en/configuration.html
* @type {import("prettier").Config}
*/
const config = {
trailingComma: "es5",
tabWidth: 4,
Expand Down
22 changes: 22 additions & 0 deletions docs/install.md
Expand Up @@ -24,6 +24,12 @@ yarn add --dev --exact prettier
pnpm add --save-dev --save-exact prettier
```

<!--bun-->

```bash
bun add --dev --exact prettier
```

<!--END_DOCUSAURUS_CODE_TABS-->

Then, create an empty config file to let editors and other tools know you are using Prettier:
Expand Down Expand Up @@ -80,6 +86,14 @@ pnpm exec prettier . --write

> What is `pnpm` doing at the start? `pnpm prettier` runs the locally installed version of Prettier. We’ll leave off the `pnpm` part for brevity throughout the rest of this file!
<!--bun-->

```bash
bun prettier . --write
```

> What is `bun` doing at the start? `bun prettier` runs the locally installed version of Prettier. We’ll leave off the `bun` part for brevity throughout the rest of this file!
<!--END_DOCUSAURUS_CODE_TABS-->

`prettier --write .` is great for formatting everything, but for a big project it might take a little while. You may run `prettier --write app/` to format a certain directory, or `prettier --write app/components/Button.js` to format a certain file. Or use a _glob_ like `prettier --write "app/**/*.test.js"` to format all tests in a directory (see [fast-glob](https://github.com/mrmlnc/fast-glob#pattern-syntax) for supported glob syntax).
Expand Down Expand Up @@ -143,6 +157,14 @@ For example, you can do the following to have Prettier run before each commit:
node --eval "fs.writeFileSync('.husky/pre-commit','pnpm exec lint-staged\n')"
```

<!--bun-->

```bash
bun add --dev husky lint-staged
bunx husky init
bun --eval "fs.writeFileSync('.husky/pre-commit','bunx lint-staged\n')"
```

<!--END_DOCUSAURUS_CODE_TABS-->

2. Add the following to your `package.json`:
Expand Down
8 changes: 8 additions & 0 deletions docs/precommit.md
Expand Up @@ -93,6 +93,14 @@ pnpm add --save-dev git-format-staged
pnpm husky set .husky/pre-commit "git-format-staged -f 'prettier --ignore-unknown --stdin --stdin-filepath \"{}\"' ."
```

<!--bun-->

```bash
bunx husky-init
bun add --dev git-format-staged
echo "git-format-staged -f 'prettier --ignore-unknown --stdin --stdin-filepath \"{}\"' ." > .husky/pre-commit
```

<!--END_DOCUSAURUS_CODE_TABS-->

Add or remove file extensions to suit your project. Note that regardless of which extensions you list formatting will respect any `.prettierignore` files in your project.
Expand Down
12 changes: 12 additions & 0 deletions eslint.config.js
Expand Up @@ -182,6 +182,18 @@ export default [
// Hard to fix
"regexp/no-empty-alternative": "off",
"regexp/no-super-linear-backtracking": "off",
"regexp/unicode-property": [
"error",
{
generalCategory: "never",
key: "long",
property: {
binary: "long",
generalCategory: "long",
script: "long",
},
},
],

"simple-import-sort/imports": "error",
"simple-import-sort/exports": "error",
Expand Down
2 changes: 2 additions & 0 deletions jest.config.js
Expand Up @@ -49,6 +49,8 @@ if (SKIP_TESTS_WITH_NEW_SYNTAX) {
"<rootDir>/tests/integration/__tests__/normalize-doc.js",
"<rootDir>/tests/integration/__tests__/doc-utils-clean-doc.js",
"<rootDir>/tests/integration/__tests__/config-invalid.js",
// Fails on Node.js v14
"<rootDir>/tests/dts/unit/run.js",
);
}

Expand Down
13 changes: 6 additions & 7 deletions package.json
Expand Up @@ -38,8 +38,8 @@
"@iarna/toml": "2.2.5",
"@prettier/is-es5-identifier-name": "0.2.0",
"@prettier/parse-srcset": "3.1.0",
"@typescript-eslint/typescript-estree": "7.5.0",
"@typescript-eslint/visitor-keys": "7.5.0",
"@typescript-eslint/typescript-estree": "7.6.0",
"@typescript-eslint/visitor-keys": "7.6.0",
"acorn": "8.11.3",
"acorn-jsx": "5.3.2",
"angular-estree-parser": "10.0.1",
Expand Down Expand Up @@ -67,7 +67,7 @@
"hermes-parser": "0.20.1",
"html-element-attributes": "3.4.0",
"html-tag-names": "2.1.0",
"html-ua-styles": "0.0.5",
"html-ua-styles": "0.0.6",
"ignore": "5.3.1",
"import-meta-resolve": "4.0.0",
"iterate-directory-up": "1.1.1",
Expand Down Expand Up @@ -97,7 +97,7 @@
"sdbm": "2.0.0",
"strip-ansi": "7.1.0",
"to-fast-properties": "4.0.0",
"typescript": "5.3.3",
"typescript": "5.4.5",
"unicode-regex": "4.0.0",
"unified": "9.2.2",
"url-or-path": "2.3.0",
Expand All @@ -114,9 +114,8 @@
"@types/eslint__js": "8.42.3",
"@types/estree": "1.0.5",
"@types/file-entry-cache": "5.0.4",
"@types/find-cache-dir": "3.2.1",
"@types/jest": "29.5.12",
"@typescript-eslint/eslint-plugin": "7.5.0",
"@typescript-eslint/eslint-plugin": "7.6.0",
"benchmark": "2.1.4",
"browserslist": "4.23.0",
"browserslist-to-esbuild": "2.1.1",
Expand All @@ -135,7 +134,7 @@
"eslint-plugin-jest": "27.9.0",
"eslint-plugin-n": "16.6.2",
"eslint-plugin-react": "7.34.1",
"eslint-plugin-regexp": "2.4.0",
"eslint-plugin-regexp": "2.5.0",
"eslint-plugin-simple-import-sort": "12.0.0",
"eslint-plugin-unicorn": "52.0.0",
"esm-utils": "4.2.1",
Expand Down
22 changes: 13 additions & 9 deletions scripts/build/modify-typescript-module.js
Expand Up @@ -56,11 +56,13 @@ class TypeScriptModuleSource {

replaceModule(module, replacement) {
if (typeof module === "string") {
module = this.modules.find((searching) => searching.path === module);
}
const found = this.modules.find((searching) => searching.path === module);

if (!found) {
throw new Error(`Module '${module}' not found`);
}

if (!module) {
throw Object.assign(new Error("Module not found"), { module });
module = found;
}

const { esmModuleInitFunctionName } = module;
Expand All @@ -77,13 +79,13 @@ class TypeScriptModuleSource {
}

removeModule(module) {
if (typeof module === "string") {
module = this.modules.find((searching) => searching.path === module);
}

return this.replaceModule(module, "");
}

hasModule(module) {
return this.modules.some((searching) => searching.path === module);
}

replaceAlignedCode({ start, end, replacement = "" }) {
const regexp = new RegExp(
[
Expand Down Expand Up @@ -278,7 +280,9 @@ function modifyTypescriptModule(text) {
);

// `pnp`
source.removeModule("src/compiler/pnp.ts");
if (source.hasModule("src/compiler/pnp.ts")) {
source.removeModule("src/compiler/pnp.ts");
}

/* spell-checker: disable */
// `ts.createParenthesizerRules`
Expand Down
8 changes: 8 additions & 0 deletions src/document/printer.js
Expand Up @@ -380,6 +380,7 @@ function printDocToString(doc, options) {
case MODE_BREAK: {
shouldRemeasure = false;

/** @type {Command} */
const next = { ind, mode: MODE_FLAT, doc: doc.contents };
const rem = width - pos;
const hasLineSuffix = lineSuffix.length > 0;
Expand Down Expand Up @@ -413,6 +414,7 @@ function printDocToString(doc, options) {
break;
} else {
const state = doc.expandedStates[i];
/** @type {Command} */
const cmd = { ind, mode: MODE_FLAT, doc: state };

if (fits(cmd, cmds, rem, hasLineSuffix, groupModeMap)) {
Expand Down Expand Up @@ -465,7 +467,9 @@ function printDocToString(doc, options) {
}

const [content, whitespace] = parts;
/** @type {Command} */
const contentFlatCmd = { ind, mode: MODE_FLAT, doc: content };
/** @type {Command} */
const contentBreakCmd = { ind, mode: MODE_BREAK, doc: content };
const contentFits = fits(
contentFlatCmd,
Expand All @@ -485,7 +489,9 @@ function printDocToString(doc, options) {
break;
}

/** @type {Command} */
const whitespaceFlatCmd = { ind, mode: MODE_FLAT, doc: whitespace };
/** @type {Command} */
const whitespaceBreakCmd = { ind, mode: MODE_BREAK, doc: whitespace };

if (parts.length === 2) {
Expand All @@ -503,10 +509,12 @@ function printDocToString(doc, options) {
// elements to a new array would make this algorithm quadratic,
// which is unusable for large arrays (e.g. large texts in JSX).
parts.splice(0, 2);
/** @type {Command} */
const remainingCmd = { ind, mode, doc: fill(parts) };

const secondContent = parts[0];

/** @type {Command} */
const firstAndSecondContentFlatCmd = {
ind,
mode: MODE_FLAT,
Expand Down
25 changes: 5 additions & 20 deletions src/document/utils.js
Expand Up @@ -337,10 +337,11 @@ function cleanDocFn(doc) {

return doc;
}
// A safer version of `normalizeDoc`
// - `normalizeDoc` concat strings and flat array in `fill`, while `cleanDoc` don't
// - On array, `normalizeDoc` always return object with `parts`, `cleanDoc` may return strings
// - `cleanDoc` also remove nested `group`s and empty `fill`/`align`/`indent`/`line-suffix`/`if-break` if possible

// - concat strings
// - flat arrays except for parts of `fill`
// - merge arrays of strings into single strings
// - remove nested `group`s and empty `fill`/`align`/`indent`/`line-suffix`/`if-break` if possible
function cleanDoc(doc) {
return mapDoc(doc, (currentDoc) => cleanDocFn(currentDoc));
}
Expand Down Expand Up @@ -376,21 +377,6 @@ function normalizeParts(parts) {
return newParts;
}

function normalizeDoc(doc) {
return mapDoc(doc, (currentDoc) => {
if (Array.isArray(currentDoc)) {
return normalizeParts(currentDoc);
}
if (!currentDoc.parts) {
return currentDoc;
}
return {
...currentDoc,
parts: normalizeParts(currentDoc.parts),
};
});
}

function replaceEndOfLine(doc, replacement = literalline) {
return mapDoc(doc, (currentDoc) =>
typeof currentDoc === "string"
Expand Down Expand Up @@ -422,7 +408,6 @@ export {
getDocType,
inheritLabel,
mapDoc,
normalizeDoc,
normalizeParts,
propagateBreaks,
removeLines,
Expand Down
39 changes: 37 additions & 2 deletions src/language-markdown/print-paragraph.js
@@ -1,5 +1,6 @@
import { fill } from "../document/builders.js";
import { normalizeDoc } from "../document/utils.js";
import { DOC_TYPE_ARRAY, DOC_TYPE_FILL } from "../document/constants.js";
import { getDocType } from "../document/utils.js";

/**
* @typedef {import("../common/ast-path.js").default} AstPath
Expand All @@ -14,7 +15,41 @@ import { normalizeDoc } from "../document/utils.js";
*/
function printParagraph(path, options, print) {
const parts = path.map(print, "children");
return normalizeDoc(fill(parts));
return flattenFill(parts);
}

/**
* @param {Doc[]} docs
* @returns {Doc}
*/
function flattenFill(docs) {
/*
* We assume parts always meet following conditions:
* - parts.length is odd
* - odd elements are line-like doc that comes from odd element off inner fill
*/
/** @type {Doc[]} */
const parts = [""];

(function rec(/** @type {*} */ docArray) {
for (const doc of docArray) {
const docType = getDocType(doc);
if (docType === DOC_TYPE_ARRAY) {
rec(doc);
continue;
}

let head = doc;
let rest = [];
if (docType === DOC_TYPE_FILL) {
[head, ...rest] = doc.parts;
}

parts.push([parts.pop(), head], ...rest);
}
})(docs);

return fill(parts);
}

export { printParagraph };

0 comments on commit fcc9f86

Please sign in to comment.