prettier 3.0.3 → 3.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/internal/cli.mjs CHANGED
@@ -1371,23 +1371,19 @@ var require_src = __commonJS({
1371
1371
  return void 0;
1372
1372
  }
1373
1373
  if (isArray) {
1374
- const result = [];
1375
- for (let row of data) {
1374
+ return data.map((row, index) => {
1376
1375
  if (typeof row === "string") {
1377
1376
  row = this.opts.deserialize(row);
1378
1377
  }
1379
1378
  if (row === void 0 || row === null) {
1380
- result.push(void 0);
1381
- continue;
1379
+ return void 0;
1382
1380
  }
1383
1381
  if (typeof row.expires === "number" && Date.now() > row.expires) {
1384
- this.delete(key).then(() => void 0);
1385
- result.push(void 0);
1386
- } else {
1387
- result.push(options && options.raw ? row : row.value);
1382
+ this.delete(key[index]).then(() => void 0);
1383
+ return void 0;
1388
1384
  }
1389
- }
1390
- return result;
1385
+ return options && options.raw ? row : row.value;
1386
+ });
1391
1387
  }
1392
1388
  if (typeof data.expires === "number" && Date.now() > data.expires) {
1393
1389
  return this.delete(key).then(() => void 0);
@@ -5408,7 +5404,8 @@ var {
5408
5404
  getSupportInfoWithoutPlugins,
5409
5405
  normalizeOptionSettings,
5410
5406
  vnopts,
5411
- fastGlob
5407
+ fastGlob,
5408
+ mockable
5412
5409
  } = sharedWithCli;
5413
5410
 
5414
5411
  // src/cli/cli-options.evaluate.js
@@ -6197,25 +6194,33 @@ import fs5 from "fs/promises";
6197
6194
  import path8 from "path";
6198
6195
  var import_diff = __toESM(require_create(), 1);
6199
6196
  import * as prettier from "../index.mjs";
6200
- import mockable2 from "./internal.mjs";
6201
6197
 
6202
6198
  // src/cli/expand-patterns.js
6203
6199
  import path2 from "path";
6204
6200
  async function* expandPatterns(context) {
6205
6201
  const seen = /* @__PURE__ */ new Set();
6206
6202
  let noResults = true;
6207
- for await (const pathOrError of expandPatternsInternal(context)) {
6203
+ for await (const {
6204
+ filePath,
6205
+ ignoreUnknown,
6206
+ error
6207
+ } of expandPatternsInternal(context)) {
6208
6208
  noResults = false;
6209
- if (typeof pathOrError !== "string") {
6210
- yield pathOrError;
6209
+ if (error) {
6210
+ yield {
6211
+ error
6212
+ };
6211
6213
  continue;
6212
6214
  }
6213
- const fileName = path2.resolve(pathOrError);
6214
- if (seen.has(fileName)) {
6215
+ const filename = path2.resolve(filePath);
6216
+ if (seen.has(filename)) {
6215
6217
  continue;
6216
6218
  }
6217
- seen.add(fileName);
6218
- yield fileName;
6219
+ seen.add(filename);
6220
+ yield {
6221
+ filename,
6222
+ ignoreUnknown
6223
+ };
6219
6224
  }
6220
6225
  if (noResults && context.argv.errorOnUnmatchedPattern !== false) {
6221
6226
  yield {
@@ -6233,7 +6238,6 @@ async function* expandPatternsInternal(context) {
6233
6238
  ignore: silentlyIgnoredDirs.map((dir) => "**/" + dir),
6234
6239
  followSymbolicLinks: false
6235
6240
  };
6236
- let supportedFilesGlob;
6237
6241
  const cwd2 = process.cwd();
6238
6242
  const entries = [];
6239
6243
  for (const pattern of context.filePatterns) {
@@ -6244,9 +6248,13 @@ async function* expandPatternsInternal(context) {
6244
6248
  const stat = await lstatSafe(absolutePath);
6245
6249
  if (stat) {
6246
6250
  if (stat.isSymbolicLink()) {
6247
- yield {
6248
- error: `Explicitly specified pattern "${pattern}" is a symbolic link.`
6249
- };
6251
+ if (context.argv.errorOnUnmatchedPattern !== false) {
6252
+ yield {
6253
+ error: `Explicitly specified pattern "${pattern}" is a symbolic link.`
6254
+ };
6255
+ } else {
6256
+ context.logger.debug(`Skipping pattern "${pattern}", as it is a symbolic link.`);
6257
+ }
6250
6258
  } else if (stat.isFile()) {
6251
6259
  entries.push({
6252
6260
  type: "file",
@@ -6258,8 +6266,9 @@ async function* expandPatternsInternal(context) {
6258
6266
  const prefix = escapePathForGlob(fixWindowsSlashes(relativePath));
6259
6267
  entries.push({
6260
6268
  type: "dir",
6261
- glob: getSupportedFilesGlob().map((pattern2) => `${prefix}/**/${pattern2}`),
6262
- input: pattern
6269
+ glob: `${prefix}/**/*`,
6270
+ input: pattern,
6271
+ ignoreUnknown: true
6263
6272
  });
6264
6273
  }
6265
6274
  } else if (pattern[0] === "!") {
@@ -6275,7 +6284,8 @@ async function* expandPatternsInternal(context) {
6275
6284
  for (const {
6276
6285
  type,
6277
6286
  glob,
6278
- input
6287
+ input,
6288
+ ignoreUnknown
6279
6289
  } of entries) {
6280
6290
  let result;
6281
6291
  try {
@@ -6296,22 +6306,10 @@ ${message}`
6296
6306
  };
6297
6307
  }
6298
6308
  } else {
6299
- yield* sortPaths(result);
6300
- }
6301
- }
6302
- function getSupportedFilesGlob() {
6303
- supportedFilesGlob ?? (supportedFilesGlob = [...getSupportedFilesGlobWithoutCache()]);
6304
- return supportedFilesGlob;
6305
- }
6306
- function* getSupportedFilesGlobWithoutCache() {
6307
- for (const {
6308
- extensions = [],
6309
- filenames = []
6310
- } of context.languages) {
6311
- yield* filenames;
6312
- for (const extension of extensions) {
6313
- yield `*${extension.startsWith(".") ? extension : `.${extension}`}`;
6314
- }
6309
+ yield* sortPaths(result).map((filePath) => ({
6310
+ filePath,
6311
+ ignoreUnknown
6312
+ }));
6315
6313
  }
6316
6314
  }
6317
6315
  }
@@ -6420,7 +6418,7 @@ async function getOptionsOrDie(context, filePath) {
6420
6418
  return options;
6421
6419
  } catch (error) {
6422
6420
  context.logger.error(
6423
- `Invalid configuration for file "${filePath}":
6421
+ `Invalid configuration${filePath ? ` for file "${filePath}"` : ""}:
6424
6422
  ` + error.message
6425
6423
  );
6426
6424
  process.exit(2);
@@ -6467,7 +6465,6 @@ async function getOptionsForFile(context, filepath) {
6467
6465
  var get_options_for_file_default = getOptionsForFile;
6468
6466
 
6469
6467
  // src/cli/is-tty.js
6470
- import mockable from "./internal.mjs";
6471
6468
  function isTTY() {
6472
6469
  return process.stdout.isTTY && !mockable.isCI();
6473
6470
  }
@@ -6649,9 +6646,6 @@ function useDirectory(directory, options) {
6649
6646
  if (options.create) {
6650
6647
  fs3.mkdirSync(directory, { recursive: true });
6651
6648
  }
6652
- if (options.thunk) {
6653
- return (...arguments_) => path6.join(directory, ...arguments_);
6654
- }
6655
6649
  return directory;
6656
6650
  }
6657
6651
  function getNodeModuleDirectory(directory) {
@@ -6665,9 +6659,12 @@ function findCacheDirectory(options = {}) {
6665
6659
  if (env2.CACHE_DIR && !["true", "false", "1", "0"].includes(env2.CACHE_DIR)) {
6666
6660
  return useDirectory(path6.join(env2.CACHE_DIR, options.name), options);
6667
6661
  }
6668
- let { cwd: directory = cwd() } = options;
6669
- if (options.files) {
6670
- directory = (0, import_common_path_prefix.default)(options.files.map((file) => path6.resolve(directory, file)));
6662
+ let { cwd: directory = cwd(), files } = options;
6663
+ if (files) {
6664
+ if (!Array.isArray(files)) {
6665
+ throw new TypeError(`Expected \`files\` option to be an array, got \`${typeof files}\`.`);
6666
+ }
6667
+ directory = (0, import_common_path_prefix.default)(files.map((file) => path6.resolve(directory, file)));
6671
6668
  }
6672
6669
  directory = packageDirectorySync({ cwd: directory });
6673
6670
  if (!directory) {
@@ -6786,24 +6783,25 @@ _fileEntryCache = new WeakMap();
6786
6783
  var format_results_cache_default = FormatResultsCache;
6787
6784
 
6788
6785
  // src/cli/format.js
6789
- var { getStdin, writeFormattedFile } = mockable2;
6786
+ var { getStdin, writeFormattedFile } = mockable;
6790
6787
  function diff(a, b) {
6791
6788
  return (0, import_diff.createTwoFilesPatch)("", "", a, b, "", "", { context: 2 });
6792
6789
  }
6793
6790
  var DebugError = class extends Error {
6794
6791
  name = "DebugError";
6795
6792
  };
6796
- function handleError(context, filename, error, printedFilename) {
6793
+ function handleError(context, filename, error, printedFilename, ignoreUnknown) {
6794
+ ignoreUnknown || (ignoreUnknown = context.argv.ignoreUnknown);
6797
6795
  const errorIsUndefinedParseError = error instanceof errors.UndefinedParserError;
6798
6796
  if (printedFilename) {
6799
- if ((context.argv.write || context.argv.ignoreUnknown) && errorIsUndefinedParseError) {
6797
+ if ((context.argv.write || ignoreUnknown) && errorIsUndefinedParseError) {
6800
6798
  printedFilename.clear();
6801
6799
  } else {
6802
6800
  process.stdout.write("\n");
6803
6801
  }
6804
6802
  }
6805
6803
  if (errorIsUndefinedParseError) {
6806
- if (context.argv.ignoreUnknown) {
6804
+ if (ignoreUnknown) {
6807
6805
  return;
6808
6806
  }
6809
6807
  if (!context.argv.check && !context.argv.listDifferent) {
@@ -6981,7 +6979,7 @@ async function formatStdin(context) {
6981
6979
  }
6982
6980
  const options = await get_options_for_file_default(
6983
6981
  context,
6984
- filepath ? path8.resolve(process.cwd(), filepath) : process.cwd()
6982
+ filepath ? path8.resolve(filepath) : void 0
6985
6983
  );
6986
6984
  if (await listDifferent(context, input, options, "(stdin)")) {
6987
6985
  return;
@@ -7014,27 +7012,20 @@ async function formatFiles(context) {
7014
7012
  cacheFilePath,
7015
7013
  context.argv.cacheStrategy || "content"
7016
7014
  );
7017
- } else {
7018
- if (context.argv.cacheStrategy) {
7019
- context.logger.error(
7020
- "`--cache-strategy` cannot be used without `--cache`."
7021
- );
7022
- process.exit(2);
7023
- }
7024
- if (!context.argv.cacheLocation) {
7025
- const stat = await statSafe(cacheFilePath);
7026
- if (stat) {
7027
- await fs5.unlink(cacheFilePath);
7028
- }
7015
+ } else if (!context.argv.cacheLocation) {
7016
+ const stat = await statSafe(cacheFilePath);
7017
+ if (stat) {
7018
+ await fs5.unlink(cacheFilePath);
7029
7019
  }
7030
7020
  }
7031
- for await (const pathOrError of expandPatterns(context)) {
7032
- if (typeof pathOrError === "object") {
7033
- context.logger.error(pathOrError.error);
7021
+ for await (const { error, filename, ignoreUnknown } of expandPatterns(
7022
+ context
7023
+ )) {
7024
+ if (error) {
7025
+ context.logger.error(error);
7034
7026
  process.exitCode = 2;
7035
7027
  continue;
7036
7028
  }
7037
- const filename = pathOrError;
7038
7029
  const isFileIgnored = isIgnored(filename);
7039
7030
  if (isFileIgnored && (context.argv.debugCheck || context.argv.write || context.argv.check || context.argv.listDifferent)) {
7040
7031
  continue;
@@ -7054,11 +7045,11 @@ async function formatFiles(context) {
7054
7045
  let input;
7055
7046
  try {
7056
7047
  input = await fs5.readFile(filename, "utf8");
7057
- } catch (error) {
7048
+ } catch (error2) {
7058
7049
  context.logger.log("");
7059
7050
  context.logger.error(
7060
7051
  `Unable to read file "${fileNameToDisplay}":
7061
- ${error.message}`
7052
+ ${error2.message}`
7062
7053
  );
7063
7054
  process.exitCode = 2;
7064
7055
  continue;
@@ -7082,8 +7073,14 @@ ${error.message}`
7082
7073
  result = await format2(context, input, options);
7083
7074
  }
7084
7075
  output = result.formatted;
7085
- } catch (error) {
7086
- handleError(context, fileNameToDisplay, error, printedFilename);
7076
+ } catch (error2) {
7077
+ handleError(
7078
+ context,
7079
+ fileNameToDisplay,
7080
+ error2,
7081
+ printedFilename,
7082
+ ignoreUnknown
7083
+ );
7087
7084
  continue;
7088
7085
  }
7089
7086
  const isDifferent = output !== input;
@@ -7103,15 +7100,15 @@ ${error.message}`
7103
7100
  try {
7104
7101
  await writeFormattedFile(filename, output);
7105
7102
  shouldSetCache = true;
7106
- } catch (error) {
7103
+ } catch (error2) {
7107
7104
  context.logger.error(
7108
7105
  `Unable to write file "${fileNameToDisplay}":
7109
- ${error.message}`
7106
+ ${error2.message}`
7110
7107
  );
7111
7108
  process.exitCode = 2;
7112
7109
  }
7113
7110
  } else if (!context.argv.check && !context.argv.listDifferent) {
7114
- const message = `${source_default.grey(fileNameToDisplay)} ${Date.now() - start}ms`;
7111
+ const message = `${source_default.grey(fileNameToDisplay)} ${Date.now() - start}ms (unchanged)`;
7115
7112
  if (isCacheExists) {
7116
7113
  context.logger.log(`${message} (cached)`);
7117
7114
  } else {
@@ -7209,7 +7206,7 @@ async function printSupportInfo() {
7209
7206
  var print_support_info_default = printSupportInfo;
7210
7207
 
7211
7208
  // src/cli/index.js
7212
- async function run(rawArguments) {
7209
+ async function run(rawArguments = process.argv.slice(2)) {
7213
7210
  let logger = logger_default();
7214
7211
  try {
7215
7212
  const { logLevel } = parseArgvWithoutPlugins(
@@ -7245,6 +7242,9 @@ async function main(context) {
7245
7242
  if (context.argv.fileInfo && context.filePatterns.length > 0) {
7246
7243
  throw new Error("Cannot use --file-info with multiple files");
7247
7244
  }
7245
+ if (!context.argv.cache && context.argv.cacheStrategy) {
7246
+ throw new Error("`--cache-strategy` cannot be used without `--cache`.");
7247
+ }
7248
7248
  if (context.argv.version) {
7249
7249
  printToScreen(prettier2.version);
7250
7250
  return;
@@ -7258,24 +7258,29 @@ async function main(context) {
7258
7258
  if (context.argv.supportInfo) {
7259
7259
  return print_support_info_default();
7260
7260
  }
7261
- const hasFilePatterns = context.filePatterns.length > 0;
7262
- const useStdin = !hasFilePatterns && (!process.stdin.isTTY || context.argv.filePath);
7263
7261
  if (context.argv.findConfigPath) {
7264
7262
  await find_config_path_default(context);
7265
- } else if (context.argv.fileInfo) {
7263
+ return;
7264
+ }
7265
+ if (context.argv.fileInfo) {
7266
7266
  await file_info_default(context);
7267
- } else if (useStdin) {
7267
+ return;
7268
+ }
7269
+ const hasFilePatterns = context.filePatterns.length > 0;
7270
+ const useStdin = !hasFilePatterns && (!process.stdin.isTTY || context.argv.filepath);
7271
+ if (useStdin) {
7268
7272
  if (context.argv.cache) {
7269
- context.logger.error("`--cache` cannot be used with stdin.");
7270
- process.exit(2);
7273
+ throw new Error("`--cache` cannot be used when formatting stdin.");
7271
7274
  }
7272
7275
  await formatStdin(context);
7273
- } else if (hasFilePatterns) {
7276
+ return;
7277
+ }
7278
+ if (hasFilePatterns) {
7274
7279
  await formatFiles(context);
7275
- } else {
7276
- process.exitCode = 1;
7277
- printToScreen(createUsage(context));
7280
+ return;
7278
7281
  }
7282
+ process.exitCode = 1;
7283
+ printToScreen(createUsage(context));
7279
7284
  }
7280
7285
  export {
7281
7286
  run
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prettier",
3
- "version": "3.0.3",
3
+ "version": "3.1.1",
4
4
  "description": "Prettier is an opinionated code formatter",
5
5
  "bin": "./bin/prettier.cjs",
6
6
  "repository": "prettier/prettier",
@@ -15,6 +15,10 @@
15
15
  ".": {
16
16
  "types": "./index.d.ts",
17
17
  "require": "./index.cjs",
18
+ "browser": {
19
+ "import": "./standalone.mjs",
20
+ "default": "./standalone.js"
21
+ },
18
22
  "default": "./index.mjs"
19
23
  },
20
24
  "./*": "./*",
@@ -146,7 +150,6 @@
146
150
  "index.d.ts",
147
151
  "index.mjs",
148
152
  "internal/cli.mjs",
149
- "internal/internal.mjs",
150
153
  "package.json",
151
154
  "plugins/acorn.d.ts",
152
155
  "plugins/acorn.js",