Visitar URL original
Support TypeScript 5.3 by fisker · Pull Request #15718 · prettier/prettier · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions changelog_unreleased/typescript/15718.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#### Support "Import Attributes" (#15718 by @fisker)

[TypeScript 5.3](https://devblogs.microsoft.com/typescript/announcing-typescript-5-3/#import-attributes) supports the latest updates to the [import attributes](https://github.com/tc39/proposal-import-attributes) proposal.

```tsx
import something from "./something.json" with { type: "json" };
```
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
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": "6.10.0",
"@typescript-eslint/visitor-keys": "6.10.0",
"@typescript-eslint/typescript-estree": "6.13.2",
"@typescript-eslint/visitor-keys": "6.13.2",
"acorn": "8.11.2",
"acorn-jsx": "5.3.2",
"angular-estree-parser": "9.0.0",
Expand Down Expand Up @@ -98,7 +98,7 @@
"sdbm": "2.0.0",
"strip-ansi": "7.1.0",
"to-fast-properties": "4.0.0",
"typescript": "5.2.2",
"typescript": "5.3.2",
"unicode-regex": "4.0.0",
"unified": "9.2.2",
"url-or-path": "2.3.0",
Expand All @@ -113,7 +113,7 @@
"@types/file-entry-cache": "5.0.4",
"@types/find-cache-dir": "3.2.1",
"@types/jest": "29.5.8",
"@typescript-eslint/eslint-plugin": "6.10.0",
"@typescript-eslint/eslint-plugin": "6.13.2",
"benchmark": "2.1.4",
"browserslist-to-esbuild": "1.2.0",
"c8": "8.0.1",
Expand Down
10 changes: 10 additions & 0 deletions scripts/build/modify-typescript-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,13 @@ function modifyTypescriptModule(text) {
}
}

// server
for (const module of source.modules) {
if (module.path.startsWith("src/server/")) {
source.removeModule(module);
}
}

// `transformers`
source.removeModule("src/compiler/transformer.ts");
for (const module of source.modules) {
Expand Down Expand Up @@ -268,6 +275,9 @@ function modifyTypescriptModule(text) {
`,
);

// `pnp`
source.removeModule("src/compiler/pnp.ts");

/* spell-checker: disable */
// `ts.createParenthesizerRules`
source.replaceAlignedCode({
Expand Down
10 changes: 5 additions & 5 deletions src/language-js/parse/postprocess/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ function postprocess(ast, options) {
ast.extra = { ...ast.extra, __isUsingHackPipeline: true };
break;

// TODO: Remove this when https://github.com/meriyah/meriyah/issues/200 get fixed
case "ExportAllDeclaration": {
const { exported } = node;
if (parser === "meriyah" && exported?.type === "Identifier") {
case "ExportAllDeclaration":
// TODO: Remove this when https://github.com/meriyah/meriyah/issues/200 get fixed
if (parser === "meriyah" && node.exported?.type === "Identifier") {
const { exported } = node;
const raw = text.slice(locStart(exported), locEnd(exported));
if (raw.startsWith('"') || raw.startsWith("'")) {
node.exported = {
Expand All @@ -112,7 +112,7 @@ function postprocess(ast, options) {
}
}
break;
}

// In Flow parser, it doesn't generate union/intersection types for single type
case "TSUnionType":
case "TSIntersectionType":
Expand Down
1 change: 1 addition & 0 deletions src/language-js/parse/typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const baseParseOptions = {
tokens: true,
loggerFn: false,
project: [],
jsDocParsingMode: "none",
// TODO: Use new properties when update printer
suppressDeprecatedPropertyWarnings: true,
};
Expand Down
59 changes: 54 additions & 5 deletions src/language-js/print/module.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import assert from "node:assert";
import isNonEmptyArray from "../../utils/is-non-empty-array.js";
import UnexpectedNodeError from "../../utils/unexpected-node-error.js";
import {
Expand All @@ -20,7 +21,7 @@ import {
rawText,
createTypeCheckFunction,
} from "../utils/index.js";
import { locStart, hasSameLoc } from "../loc.js";
import { locStart, hasSameLoc, locEnd } from "../loc.js";
import { printDecoratorsBeforeExport } from "./decorators.js";
import { printDeclareToken } from "./misc.js";

Expand Down Expand Up @@ -247,6 +248,57 @@ function shouldNotPrintSpecifiers(node, options) {
);
}

function getTextWithoutComments(options, start, end) {
let text = options.originalText.slice(start, end);

for (const comment of options[Symbol.for("comments")]) {
const commentStart = locStart(comment);
// Comments are sorted, we can escape if the comment is after the range
if (commentStart > end) {
break;
}

const commentEnd = locEnd(comment);
if (commentEnd < start) {
continue;
}

const commentLength = commentEnd - commentStart;
text =
text.slice(0, commentStart - start) +
" ".repeat(commentLength) +
text.slice(commentEnd - start);
}

if (process.env.NODE_ENV !== "production") {
assert(text.length === end - start);
}

return text;
}

function getImportAttributesOrAssertionsKeyword(node, options) {
if (
// Babel parser add this property to indicate the keyword is `assert`
node.extra?.deprecatedAssertSyntax ||
!isNonEmptyArray(node.attributes)
) {
return "assert";
}

const textBetweenSourceAndAttributes = getTextWithoutComments(
options,
node.source.range[1],
node.attributes[0].range[0],
);

if (textBetweenSourceAndAttributes.trimStart().startsWith("assert")) {
return "assert";
}

return "with";
}

/**
* Print Import Attributes syntax.
* If old ImportAssertions syntax is used, print them here.
Expand All @@ -264,10 +316,7 @@ function printImportAttributes(path, options, print) {
return "";
}

const keyword =
property === "assertions" || node.extra?.deprecatedAssertSyntax
? "assert"
: "with";
const keyword = getImportAttributesOrAssertionsKeyword(node, options);
return [
` ${keyword} {`,
options.bracketSpacing ? " " : "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -742,17 +742,6 @@ exports[`valid-for-using-binding-of-of.js [meriyah] format 1`] = `
Cause: [2:21]: Invalid left-hand side in for-of"
`;

exports[`valid-for-using-binding-of-of.js [typescript] format 1`] = `
"')' expected. (2:26)
1 | async function f() {
> 2 | for (await using of of of);
| ^
3 | for await (await using of of of);
4 | }
5 |
Cause: ')' expected."
`;

exports[`valid-for-using-binding-of-of.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
Expand Down
3 changes: 0 additions & 3 deletions tests/format/js/explicit-resource-management/jsfmt.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ run_spec(import.meta, ["babel", "flow", "typescript"], {
"valid-await-using-comments.js",
"using-declarations.js",

// https://github.com/microsoft/TypeScript/issues/55555
"valid-for-using-binding-of-of.js",

// TypeScript does not support module expr
"valid-module-block-top-level-using-binding.js",
"valid-module-block-top-level-await-using-binding.js",
Expand Down
115 changes: 62 additions & 53 deletions tests/format/js/import-attributes/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,6 @@ exports[`empty.js [meriyah] format 1`] = `
Cause: [2:36]: Unexpected token: 'with'"
`;

exports[`empty.js [typescript] format 1`] = `
"';' expected. (2:33)
1 | export * as foo from "foo.json"
> 2 | export * as bar from "bar.json" with { }
| ^
3 | export * as baz from "baz.json" with { /* comment */ }
4 |
5 | import * as foo from "foo.json"
Cause: ';' expected."
`;

exports[`empty.js format 1`] = `
====================================options=====================================
parsers: ["babel", "typescript"]
Expand All @@ -108,6 +97,68 @@ import * as baz from "baz.json" /* comment */;
================================================================================
`;

exports[`keyword-detect.js [acorn] format 1`] = `
"Unexpected token (1:35)
> 1 | import "./test.json" /* assert */ with /* assert */ { type: "json" }
| ^
2 | import {} from "./test.json" /* assert */ with /* assert */ { type: "json" }
3 | import "./test.json" /* with */ assert /* with */ { type: "json" }
4 | import {} from "./test.json" /* with */ assert /* with */ { type: "json" }
Cause: Unexpected token (1:34)"
`;

exports[`keyword-detect.js [espree] format 1`] = `
"Unexpected token with (1:35)
> 1 | import "./test.json" /* assert */ with /* assert */ { type: "json" }
| ^
2 | import {} from "./test.json" /* assert */ with /* assert */ { type: "json" }
3 | import "./test.json" /* with */ assert /* with */ { type: "json" }
4 | import {} from "./test.json" /* with */ assert /* with */ { type: "json" }
Cause: Unexpected token with"
`;

exports[`keyword-detect.js [meriyah] format 1`] = `
"Unexpected token: 'with' (1:38)
> 1 | import "./test.json" /* assert */ with /* assert */ { type: "json" }
| ^
2 | import {} from "./test.json" /* assert */ with /* assert */ { type: "json" }
3 | import "./test.json" /* with */ assert /* with */ { type: "json" }
4 | import {} from "./test.json" /* with */ assert /* with */ { type: "json" }
Cause: [1:38]: Unexpected token: 'with'"
`;

exports[`keyword-detect.js format 1`] = `
====================================options=====================================
parsers: ["babel", "typescript"]
printWidth: 80
| printWidth
=====================================input======================================
import "./test.json" /* assert */ with /* assert */ { type: "json" }
import {} from "./test.json" /* assert */ with /* assert */ { type: "json" }
import "./test.json" /* with */ assert /* with */ { type: "json" }
import {} from "./test.json" /* with */ assert /* with */ { type: "json" }

export {} from "./test.json" /* assert */ with /* assert */ { type: "json" }
export {} from "./test.json" /* with */ assert /* with */ { type: "json" }

export * from "./test.json" /* assert */ with /* assert */ { type: "json" }
export * from "./test.json" /* with */ assert /* with */ { type: "json" }

=====================================output=====================================
import "./test.json" /* assert */ /* assert */ with { type: "json" };
import {} from "./test.json" /* assert */ /* assert */ with { type: "json" };
import "./test.json" /* with */ /* with */ assert { type: "json" };
import {} from "./test.json" /* with */ /* with */ assert { type: "json" };

export {} from "./test.json" /* assert */ /* assert */ with { type: "json" };
export {} from "./test.json" /* with */ /* with */ assert { type: "json" };

export * from "./test.json" /* assert */ /* assert */ with { type: "json" };
export * from "./test.json" /* with */ /* with */ assert { type: "json" };

================================================================================
`;

exports[`multi-types.js [acorn] format 1`] = `
"Unexpected token (1:31)
> 1 | import json from "./foo.json" with { type: "json", type: "bar" };
Expand All @@ -132,14 +183,6 @@ exports[`multi-types.js [meriyah] format 1`] = `
Cause: [1:34]: Unexpected token: 'with'"
`;

exports[`multi-types.js [typescript] format 1`] = `
"';' expected. (1:31)
> 1 | import json from "./foo.json" with { type: "json", type: "bar" };
| ^
2 |
Cause: ';' expected."
`;

exports[`multi-types.js format 1`] = `
====================================options=====================================
parsers: ["babel", "typescript"]
Expand Down Expand Up @@ -178,14 +221,6 @@ exports[`non-type.js [meriyah] format 1`] = `
Cause: [1:31]: Unexpected token: 'with'"
`;

exports[`non-type.js [typescript] format 1`] = `
"';' expected. (1:28)
> 1 | import foo from "foo.json" with { lazy: "true" };
| ^
2 |
Cause: ';' expected."
`;

exports[`non-type.js format 1`] = `
====================================options=====================================
parsers: ["babel", "typescript"]
Expand Down Expand Up @@ -230,16 +265,6 @@ exports[`re-export.js [meriyah] format 1`] = `
Cause: [1:47]: Unexpected token: 'with'"
`;

exports[`re-export.js [typescript] format 1`] = `
"';' expected. (1:44)
> 1 | export { default as foo2 } from "foo.json" with { type: "json" };
| ^
2 | export * from "foo.json" with { type: "json" };
3 | export * as foo3 from "foo.json" with { type: "json" };
4 |
Cause: ';' expected."
`;

exports[`re-export.js format 1`] = `
====================================options=====================================
parsers: ["babel", "typescript"]
Expand Down Expand Up @@ -282,14 +307,6 @@ exports[`static-import.js [meriyah] format 1`] = `
Cause: [1:34]: Unexpected token: 'with'"
`;

exports[`static-import.js [typescript] format 1`] = `
"';' expected. (1:31)
> 1 | import json from "./foo.json" with { type: "json" };
| ^
2 |
Cause: ';' expected."
`;

exports[`static-import.js format 1`] = `
====================================options=====================================
parsers: ["babel", "typescript"]
Expand Down Expand Up @@ -328,14 +345,6 @@ exports[`without-from.js [meriyah] format 1`] = `
Cause: [1:17]: Unexpected token: 'with'"
`;

exports[`without-from.js [typescript] format 1`] = `
"';' expected. (1:14)
> 1 | import "foo" with { type: "json" }
| ^
2 |
Cause: ';' expected."
`;

exports[`without-from.js format 1`] = `
====================================options=====================================
parsers: ["babel", "typescript"]
Expand Down
Loading