diff --git a/.eslintrc.cjs b/.eslintrc.cjs index abd521300..9e8b6442a 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -176,8 +176,6 @@ module.exports = { { files: [ "**/*.cjs", - "src/document/**/*.js", - "src/utils/**/*.js", "scripts/tools/eslint-plugin-prettier-internal-rules/**/*.js", "website/**/*.js", "tests/integration/plugins/automatic/prettier-plugin-bar.js", diff --git a/scripts/build/bundler.mjs b/scripts/build/bundler.mjs index 93b4a09ed..2b55634b8 100644 --- a/scripts/build/bundler.mjs +++ b/scripts/build/bundler.mjs @@ -99,6 +99,8 @@ function* getEsbuildOptions(bundle, buildOptions) { "src/language-js/parse/parsers.js", "src/language-markdown/parsers.js", "src/language-yaml/parsers.js", + // This module requires file access, should not include in universal bundle + "src/utils/get-interpreter.js", ]) { replaceModule[path.join(PROJECT_ROOT, file)] = EXPORT_UNDEFINED_MODULE_REPLACEMENT; diff --git a/scripts/build/config.mjs b/scripts/build/config.mjs index 2bb07e4c1..8a9babe72 100644 --- a/scripts/build/config.mjs +++ b/scripts/build/config.mjs @@ -194,6 +194,7 @@ const coreBundles = [ target: "universal", format: "umd", minify: false, + isEsm: true, }, { input: "src/standalone.js", diff --git a/src/cli/package.json b/src/cli/package.json deleted file mode 100644 index b4cec2846..000000000 --- a/src/cli/package.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "//TODO": "Remove this file when we close #10157", - "type": "module", - "description": "A package.json file to use `.js` files in this directory as ES module." -} diff --git a/src/common/end-of-line.cjs b/src/common/end-of-line.cjs deleted file mode 100644 index 0c6365a28..000000000 --- a/src/common/end-of-line.cjs +++ /dev/null @@ -1,54 +0,0 @@ -// cjs version for ../doc to use -"use strict"; - -function guessEndOfLine(text) { - const index = text.indexOf("\r"); - if (index >= 0) { - return text.charAt(index + 1) === "\n" ? "crlf" : "cr"; - } - return "lf"; -} - -function convertEndOfLineToChars(value) { - switch (value) { - case "cr": - return "\r"; - case "crlf": - return "\r\n"; - default: - return "\n"; - } -} - -function countEndOfLineChars(text, eol) { - let regex; - - switch (eol) { - case "\n": - regex = /\n/g; - break; - case "\r": - regex = /\r/g; - break; - case "\r\n": - regex = /\r\n/g; - break; - default: - /* istanbul ignore next */ - throw new Error(`Unexpected "eol" ${JSON.stringify(eol)}.`); - } - - const endOfLines = text.match(regex); - return endOfLines ? endOfLines.length : 0; -} - -function normalizeEndOfLine(text) { - return text.replace(/\r\n?/g, "\n"); -} - -module.exports = { - guessEndOfLine, - convertEndOfLineToChars, - countEndOfLineChars, - normalizeEndOfLine, -}; diff --git a/src/document/doc-builders.js b/src/document/doc-builders.js index c119695be..16cc292e2 100644 --- a/src/document/doc-builders.js +++ b/src/document/doc-builders.js @@ -1,5 +1,3 @@ -"use strict"; - /** * TBD properly tagged union for Doc object type is needed here. * @@ -263,7 +261,7 @@ function label(label, contents) { return { type: "label", label, contents }; } -module.exports = { +export { concat, join, line, diff --git a/src/document/doc-debug.js b/src/document/doc-debug.js index 02a65e436..b85f714f8 100644 --- a/src/document/doc-debug.js +++ b/src/document/doc-debug.js @@ -1,6 +1,4 @@ -"use strict"; - -const { isConcat, getDocParts } = require("./doc-utils.js"); +import { isConcat, getDocParts } from "./doc-utils.js"; function flattenDoc(doc) { if (!doc) { @@ -210,4 +208,4 @@ function printDocToDebug(doc) { } } -module.exports = { printDocToDebug }; +export { printDocToDebug }; diff --git a/src/document/doc-printer.js b/src/document/doc-printer.js index a8e422178..7cdecf5ab 100644 --- a/src/document/doc-printer.js +++ b/src/document/doc-printer.js @@ -1,10 +1,8 @@ -"use strict"; - -const { convertEndOfLineToChars } = require("../common/end-of-line.cjs"); -const getLast = require("../utils/get-last.js"); -const getStringWidth = require("../utils/get-string-width.js"); -const { fill, cursor, indent } = require("./doc-builders.js"); -const { isConcat, getDocParts } = require("./doc-utils.js"); +import { convertEndOfLineToChars } from "../common/end-of-line.js"; +import getLast from "../utils/get-last.js"; +import getStringWidth from "../utils/get-string-width.js"; +import { fill, cursor, indent } from "./doc-builders.js"; +import { isConcat, getDocParts } from "./doc-utils.js"; /** @type {Record} */ let groupModeMap; @@ -603,4 +601,4 @@ function printDocToString(doc, options) { return { formatted: out.join("") }; } -module.exports = { printDocToString }; +export { printDocToString }; diff --git a/src/document/doc-utils.js b/src/document/doc-utils.js index 7392976b1..d97e59ef1 100644 --- a/src/document/doc-utils.js +++ b/src/document/doc-utils.js @@ -1,7 +1,5 @@ -"use strict"; - -const getLast = require("../utils/get-last.js"); -const { literalline, join } = require("./doc-builders.js"); +import getLast from "../utils/get-last.js"; +import { literalline, join } from "./doc-builders.js"; const isConcat = (doc) => Array.isArray(doc) || (doc && doc.type === "concat"); const getDocParts = (doc) => { @@ -412,7 +410,7 @@ function canBreak(doc) { return findInDoc(doc, canBreakFn, false); } -module.exports = { +export { isConcat, getDocParts, willBreak, diff --git a/src/document/index.js b/src/document/index.js index 03eebf216..8653e5c64 100644 --- a/src/document/index.js +++ b/src/document/index.js @@ -1,12 +1,17 @@ -"use strict"; +import * as builders from "./doc-builders.js"; +import * as printer from "./doc-printer.js"; +import * as utils from "./doc-utils.js"; +import * as debug from "./doc-debug.js"; /** * @typedef {import("./doc-builders").Doc} Doc */ -module.exports = { - builders: require("./doc-builders.js"), - printer: require("./doc-printer.js"), - utils: require("./doc-utils.js"), - debug: require("./doc-debug.js"), +const doc = { + builders, + printer, + utils, + debug, }; + +export default doc; diff --git a/src/document/package.json b/src/document/package.json deleted file mode 100644 index 54d221724..000000000 --- a/src/document/package.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "//TODO": "Remove this file when we close #10157", - "type": "commonjs", - "description": "A package.json file to use `.js` files in this directory as CommonJS module." -} diff --git a/src/main/options.js b/src/main/options.js index 25f213ce4..d118ff94c 100644 --- a/src/main/options.js +++ b/src/main/options.js @@ -1,7 +1,7 @@ -import { createRequire } from "module"; import path from "path"; import { UndefinedParserError } from "../common/errors.js"; import { getSupportInfo } from "../main/support.js"; +import getInterpreter from "../utils/get-interpreter.js"; import { normalizeApiOptions } from "./options-normalizer.js"; import { resolveParser } from "./parser.js"; @@ -139,10 +139,6 @@ function inferParser(filepath, plugins) { !language && !filename.includes(".") ) { - const require = createRequire(import.meta.url); - // `getInterpreter` requires file access, put `require()` in the `if` block, - // So we can easily remove this part during build - const getInterpreter = require("../utils/get-interpreter.js"); const interpreter = getInterpreter(filepath); language = languages.find( (language) => diff --git a/src/utils/arrayify.js b/src/utils/arrayify.js index 6cd24da84..45e776357 100644 --- a/src/utils/arrayify.js +++ b/src/utils/arrayify.js @@ -1,7 +1,7 @@ -"use strict"; - -module.exports = (object, keyName) => +const arrayify = (object, keyName) => Object.entries(object).map(([key, value]) => ({ [keyName]: key, ...value, })); + +export default arrayify; diff --git a/src/utils/create-language.js b/src/utils/create-language.js index 37ed075aa..5b384cc15 100644 --- a/src/utils/create-language.js +++ b/src/utils/create-language.js @@ -1,10 +1,10 @@ -"use strict"; - -module.exports = function (linguistData, override) { +function createLanguage(linguistData, override) { const { languageId, ...rest } = linguistData; return { linguistLanguageId: languageId, ...rest, ...override(linguistData), }; -}; +} + +export default createLanguage; diff --git a/src/utils/front-matter/parse.js b/src/utils/front-matter/parse.js index 0efc79add..04ba0d38a 100644 --- a/src/utils/front-matter/parse.js +++ b/src/utils/front-matter/parse.js @@ -1,5 +1,3 @@ -"use strict"; - const frontMatterRegex = new RegExp( "^(?-{3}|\\+{3})" + // trailing spaces after delimiters are allowed @@ -47,4 +45,4 @@ function parse(text) { }; } -module.exports = parse; +export default parse; diff --git a/src/utils/front-matter/print.js b/src/utils/front-matter/print.js index 5de6ee784..2e69868a4 100644 --- a/src/utils/front-matter/print.js +++ b/src/utils/front-matter/print.js @@ -1,8 +1,8 @@ -"use strict"; +import doc from "../../document/index.js"; const { builders: { hardline, markAsRoot }, -} = require("../../document/index.js"); +} = doc; function print(node, textToDoc) { if (node.lang === "yaml") { @@ -20,4 +20,4 @@ function print(node, textToDoc) { } } -module.exports = print; +export default print; diff --git a/src/utils/get-file-content-or-null.js b/src/utils/get-file-content-or-null.js index 69d3eaef6..67c412c79 100644 --- a/src/utils/get-file-content-or-null.js +++ b/src/utils/get-file-content-or-null.js @@ -1,6 +1,4 @@ -"use strict"; - -const fs = require("fs"); +import fs from "fs"; const fsAsync = fs.promises; /** @@ -35,4 +33,4 @@ function handleError(filename, error) { throw new Error(`Unable to read ${filename}: ${error.message}`); } -module.exports = getFileContentOrNull; +export default getFileContentOrNull; diff --git a/src/utils/get-interpreter.js b/src/utils/get-interpreter.js index c67fe6324..61a5d71dc 100644 --- a/src/utils/get-interpreter.js +++ b/src/utils/get-interpreter.js @@ -1,7 +1,5 @@ -"use strict"; - -const fs = require("fs"); -const readlines = require("n-readlines"); +import fs from "fs"; +import readlines from "n-readlines"; function getInterpreter(filepath) { /* istanbul ignore next */ @@ -49,4 +47,4 @@ function getInterpreter(filepath) { } } -module.exports = getInterpreter; +export default getInterpreter; diff --git a/src/utils/get-last.js b/src/utils/get-last.js index 371abf867..170e21fe8 100644 --- a/src/utils/get-last.js +++ b/src/utils/get-last.js @@ -1,5 +1,3 @@ -"use strict"; - const getLast = (arr) => arr[arr.length - 1]; -module.exports = getLast; +export default getLast; diff --git a/src/utils/get-string-width.js b/src/utils/get-string-width.js index d96612d3d..864df308a 100644 --- a/src/utils/get-string-width.js +++ b/src/utils/get-string-width.js @@ -1,6 +1,4 @@ -"use strict"; - -const stringWidth = require("../../vendors/string-width.js").default; +import stringWidth from "string-width"; const notAsciiRegex = /[^\x20-\x7F]/; @@ -21,4 +19,4 @@ function getStringWidth(text) { return stringWidth(text); } -module.exports = getStringWidth; +export default getStringWidth; diff --git a/src/utils/is-non-empty-array.js b/src/utils/is-non-empty-array.js index 0456053bd..7006cd529 100644 --- a/src/utils/is-non-empty-array.js +++ b/src/utils/is-non-empty-array.js @@ -1,5 +1,3 @@ -"use strict"; - /** * @param {any} object * @returns {object is Array} @@ -8,4 +6,4 @@ function isNonEmptyArray(object) { return Array.isArray(object) && object.length > 0; } -module.exports = isNonEmptyArray; +export default isNonEmptyArray; diff --git a/src/utils/line-column-to-index.js b/src/utils/line-column-to-index.js index 09911a5b2..7cf626e6d 100644 --- a/src/utils/line-column-to-index.js +++ b/src/utils/line-column-to-index.js @@ -1,10 +1,10 @@ -"use strict"; - // Super inefficient, needs to be cached. -module.exports = function (lineColumn, text) { +function lineColumnToIndex(lineColumn, text) { let index = 0; for (let i = 0; i < lineColumn.line - 1; ++i) { index = text.indexOf("\n", index) + 1; } return index + lineColumn.column; -}; +} + +export default lineColumnToIndex; diff --git a/src/utils/load-json5.js b/src/utils/load-json5.js index 126aaf64c..1232c6560 100644 --- a/src/utils/load-json5.js +++ b/src/utils/load-json5.js @@ -1,12 +1,12 @@ -"use strict"; +import json5 from "json5"; -const { parse } = require("json5"); - -module.exports = function (filePath, content) { +function loadJson5(filePath, content) { try { - return parse(content); + return json5.parse(content); } catch (error) { error.message = `JSON5 Error in ${filePath}:\n${error.message}`; throw error; } -}; +} + +export default loadJson5; diff --git a/src/utils/load-toml.js b/src/utils/load-toml.js index 59c475456..a3ba1da41 100644 --- a/src/utils/load-toml.js +++ b/src/utils/load-toml.js @@ -1,12 +1,12 @@ -"use strict"; +import parse from "@iarna/toml/parse-string.js"; -const parse = require("@iarna/toml/parse-string"); - -module.exports = function (filePath, content) { +function loadToml(filePath, content) { try { return parse(content); } catch (error) { error.message = `TOML Error in ${filePath}:\n${error.message}`; throw error; } -}; +} + +export default loadToml; diff --git a/src/utils/package.json b/src/utils/package.json deleted file mode 100644 index 54d221724..000000000 --- a/src/utils/package.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "//TODO": "Remove this file when we close #10157", - "type": "commonjs", - "description": "A package.json file to use `.js` files in this directory as CommonJS module." -} diff --git a/src/utils/text/get-next-non-space-non-comment-character-index-with-start-index.js b/src/utils/text/get-next-non-space-non-comment-character-index-with-start-index.js index 237b629a9..5d65fbcb6 100644 --- a/src/utils/text/get-next-non-space-non-comment-character-index-with-start-index.js +++ b/src/utils/text/get-next-non-space-non-comment-character-index-with-start-index.js @@ -1,9 +1,7 @@ -"use strict"; - -const skipInlineComment = require("./skip-inline-comment.js"); -const skipNewline = require("./skip-newline.js"); -const skipTrailingComment = require("./skip-trailing-comment.js"); -const { skipSpaces } = require("./skip.js"); +import skipInlineComment from "./skip-inline-comment.js"; +import skipNewline from "./skip-newline.js"; +import skipTrailingComment from "./skip-trailing-comment.js"; +import { skipSpaces } from "./skip.js"; /** * @param {string} text @@ -25,4 +23,4 @@ function getNextNonSpaceNonCommentCharacterIndexWithStartIndex(text, idx) { return nextIdx; } -module.exports = getNextNonSpaceNonCommentCharacterIndexWithStartIndex; +export default getNextNonSpaceNonCommentCharacterIndexWithStartIndex; diff --git a/src/utils/text/skip-inline-comment.js b/src/utils/text/skip-inline-comment.js index 1b47494d0..5ee01689b 100644 --- a/src/utils/text/skip-inline-comment.js +++ b/src/utils/text/skip-inline-comment.js @@ -1,5 +1,3 @@ -"use strict"; - /** * @param {string} text * @param {number | false} index @@ -21,4 +19,4 @@ function skipInlineComment(text, index) { return index; } -module.exports = skipInlineComment; +export default skipInlineComment; diff --git a/src/utils/text/skip-newline.js b/src/utils/text/skip-newline.js index d1d674086..6dfee8108 100644 --- a/src/utils/text/skip-newline.js +++ b/src/utils/text/skip-newline.js @@ -1,5 +1,3 @@ -"use strict"; - /** @typedef {import("./skip").SkipOptions} SkipOptions */ // This one doesn't use the above helper function because it wants to @@ -51,4 +49,4 @@ function skipNewline(text, index, opts) { return index; } -module.exports = skipNewline; +export default skipNewline; diff --git a/src/utils/text/skip-trailing-comment.js b/src/utils/text/skip-trailing-comment.js index 13c74d057..00a711a93 100644 --- a/src/utils/text/skip-trailing-comment.js +++ b/src/utils/text/skip-trailing-comment.js @@ -1,6 +1,4 @@ -"use strict"; - -const { skipEverythingButNewLine } = require("./skip.js"); +import { skipEverythingButNewLine } from "./skip.js"; /** * @param {string} text @@ -19,4 +17,4 @@ function skipTrailingComment(text, index) { return index; } -module.exports = skipTrailingComment; +export default skipTrailingComment; diff --git a/src/utils/text/skip.js b/src/utils/text/skip.js index 0e37e474e..c5ee42ce7 100644 --- a/src/utils/text/skip.js +++ b/src/utils/text/skip.js @@ -1,5 +1,3 @@ -"use strict"; - /** * @typedef {{backwards?: boolean}} SkipOptions */ @@ -62,9 +60,4 @@ const skipToLineEnd = skip(",; \t"); */ const skipEverythingButNewLine = skip(/[^\n\r]/); -module.exports = { - skipWhitespace, - skipSpaces, - skipToLineEnd, - skipEverythingButNewLine, -}; +export { skipWhitespace, skipSpaces, skipToLineEnd, skipEverythingButNewLine }; diff --git a/src/utils/try-combinations.js b/src/utils/try-combinations.js index f7edd9e48..4e9714549 100644 --- a/src/utils/try-combinations.js +++ b/src/utils/try-combinations.js @@ -1,5 +1,3 @@ -"use strict"; - function tryCombinations(...combinations) { let firstError; for (const [index, fn] of combinations.entries()) { @@ -14,4 +12,4 @@ function tryCombinations(...combinations) { return { error: firstError }; } -module.exports = tryCombinations; +export default tryCombinations;