Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moves @types/prettier to this repository #14212

Merged
merged 55 commits into from
Feb 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
c214a2d
Rename `types` to `thirs-party-types`
sosukesuzuki Jan 2, 2023
6dd2f61
Copy `index.d.ts` from `@types/prettier`
sosukesuzuki Jan 2, 2023
8ebe6f9
Make public APIs async
sosukesuzuki Jan 2, 2023
78143d1
Add missing `formatAST`
sosukesuzuki Jan 2, 2023
6b0ad37
Fix `CustomParser`
sosukesuzuki Jan 2, 2023
9f47737
Fix `Parser.parse` signature
sosukesuzuki Jan 2, 2023
c4ae339
Add comment
sosukesuzuki Jan 2, 2023
02252ce
Add `standalonw.d.ts`
sosukesuzuki Jan 2, 2023
ba22cdd
first implement
sosukesuzuki Jan 6, 2023
1ba3e37
Fix path
sosukesuzuki Jan 7, 2023
ef57115
Add missing parser `.d.ts`
sosukesuzuki Jan 7, 2023
1a51066
Fix
sosukesuzuki Jan 9, 2023
f484d0c
Fix path
sosukesuzuki Jan 20, 2023
3c3307c
Introduce valid tests
sosukesuzuki Jan 20, 2023
e6d03a7
Add unit tests for dts files
sosukesuzuki Jan 21, 2023
f65ae80
Add unit tests
sosukesuzuki Jan 21, 2023
e8fe765
Simplify unit testing
sosukesuzuki Jan 21, 2023
3a67a9e
Tweaks structures
sosukesuzuki Jan 21, 2023
1796fbb
Revert needless change
sosukesuzuki Jan 21, 2023
83b3496
Fix types path
sosukesuzuki Jan 21, 2023
61151eb
Introduce `ts-expect`
sosukesuzuki Jan 21, 2023
1bad2f2
Fix doc utils api
sosukesuzuki Jan 21, 2023
95c5865
Add AstPath API
sosukesuzuki Jan 21, 2023
b92f929
Fix API
sosukesuzuki Jan 21, 2023
d9e7714
Fix tests
sosukesuzuki Jan 21, 2023
7064f46
Fix changelog
sosukesuzuki Jan 21, 2023
73a1a2a
Move dts
sosukesuzuki Feb 5, 2023
228c001
Update module specifiers
sosukesuzuki Feb 5, 2023
8eb2a99
Remove generate dts config file
sosukesuzuki Feb 5, 2023
cc5586f
Update build scripts
sosukesuzuki Feb 5, 2023
f72c25e
Fix type errors
sosukesuzuki Feb 5, 2023
b73e3e8
Refactor with namespace imports
sosukesuzuki Feb 5, 2023
887e862
Update comments
sosukesuzuki Feb 5, 2023
d732f31
Fix build script
sosukesuzuki Feb 5, 2023
baba6de
Improve build script
fisker Feb 14, 2023
62c997b
`typing` -> `types`
fisker Feb 14, 2023
11d1c82
Clean up
fisker Feb 14, 2023
18d8014
Minor tweak
fisker Feb 14, 2023
7af13d0
Minor tweak
fisker Feb 14, 2023
1d60730
Modify `exports` field
fisker Feb 14, 2023
f58384c
Fix paths in builder
sosukesuzuki Feb 20, 2023
2c680ad
Refactor
sosukesuzuki Feb 20, 2023
53b7907
Refactor
sosukesuzuki Feb 20, 2023
99db4bd
Address review
sosukesuzuki Feb 20, 2023
d1ab837
Update changelog
sosukesuzuki Feb 20, 2023
7b4e39f
Address reviews
sosukesuzuki Feb 20, 2023
4e91996
Remove `since` from tests
sosukesuzuki Feb 20, 2023
173cb5e
Improve utils type def
sosukesuzuki Feb 20, 2023
ff9e431
Mark deprecated methods
sosukesuzuki Feb 20, 2023
7b351ce
Address review
sosukesuzuki Feb 20, 2023
aa519c9
Move docs types
sosukesuzuki Feb 20, 2023
9465ed1
Address review
sosukesuzuki Feb 20, 2023
59e9e88
Update c8 config
sosukesuzuki Feb 20, 2023
1743161
Minor tweak
fisker Feb 21, 2023
d1cea96
Merge branch 'next' into dts
sosukesuzuki Feb 23, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ module.exports = {
"tests/format/**/jsfmt.spec.js",
"tests/integration/**/*.js",
"tests/unit/**/*.js",
"tests/dts/unit/**/*.js",
"scripts/release/__tests__/**/*.spec.js",
],
env: {
Expand Down
3 changes: 3 additions & 0 deletions changelog_unreleased/api/14212.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#### Add `.d.ts` files (#14212 by @sosukesuzuki, @fisker)

Add type definition files required to use Prettier's JavaScript API from TypeScript. This eliminates the need for users to install `@types/prettier`.
1 change: 1 addition & 0 deletions jest.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const config = {
"<rootDir>/tests/format/**/jsfmt.spec.js",
"<rootDir>/tests/integration/__tests__/**/*.js",
"<rootDir>/tests/unit/**/*.js",
"<rootDir>/tests/dts/unit/**/*.js",
],
testPathIgnorePatterns,
// collectCoverage: ENABLE_CODE_COVERAGE,
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"exports": {
".": {
"require": "./src/index.cjs",
"types": "./src/index.d.ts",
"default": "./src/index.js"
},
"./*": "./*"
Expand Down Expand Up @@ -147,7 +148,8 @@
"semver": "7.3.8",
"serialize-javascript": "6.0.1",
"snapshot-diff": "0.10.0",
"tempy": "3.0.0"
"tempy": "3.0.0",
"ts-expect": "1.3.0"
},
"resolutions": {
"trim": "1.0.1"
Expand Down Expand Up @@ -192,7 +194,8 @@
"src/index.cjs",
"src/document/debug.js",
"src/utils/unexpected-node-error.js",
"src/language-js/types/estree.d.ts"
"src/language-js/types/estree.d.ts",
"src/**/**/*.d.ts"
]
},
"browserslist": [
Expand Down
2 changes: 1 addition & 1 deletion scripts/build/build-license.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ async function buildLicense({ file, files, shouldCollectLicenses }) {
return;
}

const javascriptFiles = files.filter((file) => !file.isMetaFile);
const javascriptFiles = files.filter((file) => file.kind === "javascript");
if (javascriptFiles.some((file) => !Array.isArray(file.dependencies))) {
return { skipped: true };
}
Expand Down
8 changes: 5 additions & 3 deletions scripts/build/build-package-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
writeJson,
} from "../utils/index.mjs";

async function buildPackageJson({ files }) {
const packageJson = await readJson(path.join(PROJECT_ROOT, "package.json"));
async function buildPackageJson({ file, files }) {
const packageJson = await readJson(path.join(PROJECT_ROOT, file.input));

const bin = files.find(
(file) =>
Expand All @@ -20,6 +20,7 @@ async function buildPackageJson({ files }) {
packageJson.exports = {
".": {
require: "./index.cjs",
types: "./index.d.ts",
default: "./index.mjs",
},
"./*": "./*",
Expand All @@ -32,6 +33,7 @@ async function buildPackageJson({ files }) {
file.isPlugin ? `./plugins/${basename}` : `./${basename}`,
{
require: `./${file.output.file}`,
types: `./${file.output.file.replace(/\.js$/, ".d.ts")}`,
default: `./${file.output.file.replace(/\.js$/, ".mjs")}`,
},
];
Expand Down Expand Up @@ -74,7 +76,7 @@ async function buildPackageJson({ files }) {
};
packageJson.files = files.map(({ output: { file } }) => file).sort();

await writeJson(path.join(DIST_DIR, "package.json"), packageJson);
await writeJson(path.join(DIST_DIR, file.output.file), packageJson);
}

export default buildPackageJson;
3 changes: 2 additions & 1 deletion scripts/build/build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ async function buildFile({ file, files, shouldCollectLicenses, cliOptions }) {
let displayName = file.output.file;
if (
(file.platform === "universal" && file.output.format !== "esm") ||
(file.output.file.startsWith("index.") && file.output.format !== "esm")
(file.output.file.startsWith("index.") && file.output.format !== "esm") ||
file.kind === "types"
) {
displayName = ` ${displayName}`;
}
Expand Down
160 changes: 107 additions & 53 deletions scripts/build/config.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import path from "node:path";
import url from "node:url";
import fs from "node:fs";
import { createRequire } from "node:module";
import createEsmUtils from "esm-utils";
import { PROJECT_ROOT, DIST_DIR, copyFile } from "../utils/index.mjs";
import {
PROJECT_ROOT,
DIST_DIR,
copyFile,
writeFile,
} from "../utils/index.mjs";
import buildJavascriptModule from "./build-javascript-module.js";
import buildPackageJson from "./build-package-json.js";
import buildLicense from "./build-license.js";
Expand All @@ -16,6 +22,58 @@ const {
} = createEsmUtils(import.meta);
const resolveEsmModulePath = async (specifier) =>
url.fileURLToPath(await importMetaResolve(specifier));
const copyFileBuilder = ({ file }) =>
copyFile(
path.join(PROJECT_ROOT, file.input),
path.join(DIST_DIR, file.output.file)
);

async function typesFileBuilder({ file }) {
/**
* @typedef {{ from: string, to: string }} ImportPathReplacement
* @typedef {{ [input: string]: Array<ImportPathReplacement> }} ReplacementMap
*/

/** @type {Array<ImportPathReplacement>} */
const jsParsersImportReplacement = [
{ from: "../../index.js", to: "../index.js" },
];
/** @type {ReplacementMap} */
const pathReplacementMap = {
"src/index.d.ts": [{ from: "./document/index.js", to: "./doc.js" }],
"src/language-js/parse/acorn-and-espree.d.ts": jsParsersImportReplacement,
"src/language-js/parse/angular.d.ts": jsParsersImportReplacement,
"src/language-js/parse/babel.d.ts": jsParsersImportReplacement,
"src/language-js/parse/flow.d.ts": jsParsersImportReplacement,
"src/language-js/parse/meriyah.d.ts": jsParsersImportReplacement,
"src/language-js/parse/typescript.d.ts": jsParsersImportReplacement,
};
const replacements = pathReplacementMap[file.input] ?? [];
let data = await fs.promises.readFile(file.input, "utf8");
for (const { from, to } of replacements) {
data = data.replaceAll(
new RegExp(` from "${from}";`, "g"),
` from "${to}";`
);
}
await writeFile(path.join(DIST_DIR, file.output.file), data);
}

function getTypesFileConfig({ input: jsFileInput, outputBaseName }) {
const input = jsFileInput.replace(/\.[cm]?js$/, ".d.ts");
if (!fs.existsSync(path.join(PROJECT_ROOT, input))) {
return;
}

return {
input,
output: {
file: outputBaseName + ".d.ts",
},
kind: "types",
build: typesFileBuilder,
};
}

/**
* @typedef {Object} BuildOptions
Expand All @@ -25,18 +83,18 @@ const resolveEsmModulePath = async (specifier) =>
* @property {boolean?} minify - disable code minification
*
* @typedef {Object} Output
* @property {'esm' | 'umd' | 'cjs' | 'text' | 'json'} format - File format
* @property {"esm" | "umd" | "cjs" | "text" | "json"} format - File format
* @property {string} file - path of the output file in the `dist/` folder
* @property {string?} umdVariableName - name for the UMD file (for plugins, it'll be `prettierPlugins.${name}`)
*
* @typedef {Object} File
* @property {string?} input - input of the file
* @property {string} input - input of the file
* @property {Output} output - output of the file
* @property {"javascript" | "types" | "meta"} kind - file kind
* @property {function} build - file generate function
* @property {'node' | 'universal'} platform - ESBuild platform
* @property {"node" | "universal"} platform - ESBuild platform
* @property {BuildOptions} buildOptions - ESBuild options
* @property {boolean?} isPlugin - file is a plugin
* @property {boolean?} isMeta - file is a meta file (package.json, LICENSE README.md)
*/

/*
Expand Down Expand Up @@ -400,22 +458,27 @@ const universalFiles = [...nonPluginUniversalFiles, ...pluginFiles].flatMap(
outputBaseName ??= path.basename(input);

return [
{
format: "esm",
file: `${outputBaseName}${extensions.esm}`,
},
{
format: "umd",
file: `${outputBaseName}${extensions.umd}`,
umdVariableName,
},
].map((output) => ({
input,
output,
platform: "universal",
buildOptions,
isPlugin,
}));
...[
{
format: "esm",
file: `${outputBaseName}${extensions.esm}`,
},
{
format: "umd",
file: `${outputBaseName}${extensions.umd}`,
umdVariableName,
},
].map((output) => ({
input,
output,
platform: "universal",
buildOptions,
isPlugin,
build: buildJavascriptModule,
kind: "javascript",
})),
getTypesFileConfig({ input, outputBaseName }),
];
}
);

Expand Down Expand Up @@ -481,59 +544,50 @@ const nodejsFiles = [
},
],
},
].map((file) => {
].flatMap((file) => {
let { input, output, outputBaseName, ...buildOptions } = file;

const format = input.endsWith(".cjs") ? "cjs" : "esm";
outputBaseName ??= path.basename(input, path.extname(input));

return {
input,
output: {
format,
file: `${outputBaseName}${extensions[format]}`,
return [
{
input,
output: {
format,
file: `${outputBaseName}${extensions[format]}`,
},
platform: "node",
buildOptions,
build: buildJavascriptModule,
kind: "javascript",
},
platform: "node",
buildOptions,
};
getTypesFileConfig({ input, outputBaseName }),
];
});

const metaFiles = [
{
input: "package.json",
output: {
format: "json",
file: "package.json",
},
build: buildPackageJson,
},
{
output: {
format: "text",
file: "README.md",
},
async build() {
await copyFile(
path.join(PROJECT_ROOT, "README.md"),
path.join(DIST_DIR, "README.md")
);
},
input: "README.md",
build: copyFileBuilder,
},
{
output: {
format: "text",
file: "LICENSE",
},
input: "LICENSE",
build: buildLicense,
},
].map((file) => ({ ...file, isMetaFile: true }));
].map((file) => ({
...file,
output: { file: file.input, ...file.output },
kind: "meta",
}));

/** @type {Files[]} */
const files = [
...[...nodejsFiles, ...universalFiles].map((file) => ({
...file,
build: buildJavascriptModule,
})),
...metaFiles,
];
const files = [...nodejsFiles, ...universalFiles, ...metaFiles].filter(Boolean);
export default files;
1 change: 1 addition & 0 deletions src/cli/prettier-internal.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-expect-error
import { __internal as sharedWithCli } from "../index.js";

export const {
Expand Down
1 change: 1 addition & 0 deletions src/cli/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from "node:fs/promises";
import path from "node:path";
import sdbm from "sdbm";
// @ts-expect-error
import { __internal as sharedWithCli } from "../index.js";

const { arrayify, isNonEmptyArray, partition } = sharedWithCli.utils;
Expand Down