Support TLA for typescript parser (#16729)

This commit is contained in:
fisker Cheung authored and GitHub committed 2024-10-18 21:11:55 +08:00
1 parent fb8661fee1
commit b19139acb7
16 files changed
+162 -8

No files matched your search

+13
View File
@@ -0,0 +1,13 @@
#### Support "Top-level await statements" (#16729 by @fisker)
<!-- prettier-ignore -->
```tsx
// Input
(await (await fetch()).json()).foo
// Prettier stable
await(await fetch()).json().foo;
// Prettier main
(await (await fetch()).json()).foo;
```
+23 -6
View File
@@ -3,6 +3,7 @@ import createError from "../../common/parser-create-error.js";
import tryCombinations from "../../utils/try-combinations.js";
import postprocess from "./postprocess/index.js";
import createParser from "./utils/create-parser.js";
import getSourceType from "./utils/get-source-type.js";
import replaceHashbang from "./utils/replace-hashbang.js";
/** @import {TSESTreeOptions} from "@typescript-eslint/typescript-estree" */
@@ -50,18 +51,34 @@ const isKnownFileType = (filepath) =>
function getParseOptionsCombinations(text, options) {
const filepath = options?.filepath;
let combinations = [{ ...baseParseOptions, filePath: filepath }];
const sourceType = getSourceType(options);
if (sourceType) {
combinations = combinations.map((parseOptions) => ({
...parseOptions,
sourceType,
}));
} else {
/** @type {("module" | "script") []} */
const sourceTypes = ["module", "script"];
combinations = sourceTypes.flatMap((sourceType) =>
combinations.map((parseOptions) => ({ ...parseOptions, sourceType })),
);
}
if (filepath && isKnownFileType(filepath)) {
return [{ ...baseParseOptions, filePath: filepath }];
return combinations;
}
const shouldEnableJsx = isProbablyJsx(text);
return [
{ ...baseParseOptions, jsx: shouldEnableJsx },
{ ...baseParseOptions, jsx: !shouldEnableJsx },
];
return [shouldEnableJsx, !shouldEnableJsx].flatMap((jsx) =>
combinations.map((parseOptions) => ({ ...parseOptions, jsx })),
);
}
function parse(text, options) {
function parse(text, options = {}) {
const textToParse = replaceHashbang(text);
const parseOptionsCombinations = getParseOptionsCombinations(text, options);
@@ -6,11 +6,11 @@ function getSourceType(options) {
}
filepath = filepath.toLowerCase();
if (filepath.endsWith(".cjs")) {
if (filepath.endsWith(".cjs") || filepath.endsWith(".cts")) {
return "script";
}
if (filepath.endsWith(".mjs")) {
if (filepath.endsWith(".mjs") || filepath.endsWith(".mts")) {
return "module";
}
}
@@ -29,3 +29,45 @@ call(await something());
================================================================================
`;
exports[`test.cjs format 1`] = `
====================================options=====================================
parsers: ["babel", "typescript"]
printWidth: 80
| printWidth
=====================================input======================================
await(1);
=====================================output=====================================
await(1);
================================================================================
`;
exports[`test.js format 1`] = `
====================================options=====================================
parsers: ["babel", "typescript"]
printWidth: 80
| printWidth
=====================================input======================================
await(1);
=====================================output=====================================
await 1;
================================================================================
`;
exports[`test.mjs format 1`] = `
====================================options=====================================
parsers: ["babel", "typescript"]
printWidth: 80
| printWidth
=====================================input======================================
await(1);
=====================================output=====================================
await 1;
================================================================================
`;
+1
View File
@@ -0,0 +1 @@
await(1);
+1
View File
@@ -0,0 +1 @@
await(1);
+1
View File
@@ -0,0 +1 @@
await(1);
@@ -0,0 +1,15 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`test.jsx format 1`] = `
====================================options=====================================
parsers: ["babel", "typescript"]
printWidth: 80
| printWidth
=====================================input======================================
await(1);
=====================================output=====================================
await 1;
================================================================================
`;
@@ -0,0 +1 @@
runFormatTest(import.meta, ["babel", "typescript"]);
@@ -0,0 +1 @@
await(1);
@@ -0,0 +1,57 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`test.cts format 1`] = `
====================================options=====================================
parsers: ["typescript"]
printWidth: 80
| printWidth
=====================================input======================================
await(1);
=====================================output=====================================
await(1);
================================================================================
`;
exports[`test.mts format 1`] = `
====================================options=====================================
parsers: ["typescript"]
printWidth: 80
| printWidth
=====================================input======================================
await(1);
=====================================output=====================================
await 1;
================================================================================
`;
exports[`test.ts format 1`] = `
====================================options=====================================
parsers: ["typescript"]
printWidth: 80
| printWidth
=====================================input======================================
await(1);
=====================================output=====================================
await 1;
================================================================================
`;
exports[`test.tsx format 1`] = `
====================================options=====================================
parsers: ["typescript"]
printWidth: 80
| printWidth
=====================================input======================================
await(1);
=====================================output=====================================
await 1;
================================================================================
`;
@@ -0,0 +1 @@
runFormatTest(import.meta, ["typescript"]);
@@ -0,0 +1 @@
await(1);
@@ -0,0 +1 @@
await(1);
@@ -0,0 +1 @@
await(1);
@@ -0,0 +1 @@
await(1);