Migrate src/{document,utils}/** to ES Modules (#12345)
This commit is contained in:
1 parent
05aa5e16d6
commit
af103ae766
31 files changed
+73
-177
No files matched your search
@@ -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",
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -194,6 +194,7 @@ const coreBundles = [
|
||||
target: "universal",
|
||||
format: "umd",
|
||||
minify: false,
|
||||
isEsm: true,
|
||||
},
|
||||
{
|
||||
input: "src/standalone.js",
|
||||
|
||||
@@ -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."
|
||||
}
|
||||
@@ -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,
|
||||
};
|
||||
@@ -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,
|
||||
|
||||
@@ -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 };
|
||||
@@ -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<symbol, typeof MODE_BREAK | typeof MODE_FLAT>} */
|
||||
let groupModeMap;
|
||||
@@ -603,4 +601,4 @@ function printDocToString(doc, options) {
|
||||
return { formatted: out.join("") };
|
||||
}
|
||||
|
||||
module.exports = { printDocToString };
|
||||
export { printDocToString };
|
||||
@@ -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,
|
||||
|
||||
+11
-6
@@ -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;
|
||||
@@ -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."
|
||||
}
|
||||
+1
-5
@@ -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) =>
|
||||
|
||||
@@ -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;
|
||||
@@ -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;
|
||||
@@ -1,5 +1,3 @@
|
||||
"use strict";
|
||||
|
||||
const frontMatterRegex = new RegExp(
|
||||
"^(?<startDelimiter>-{3}|\\+{3})" +
|
||||
// trailing spaces after delimiters are allowed
|
||||
@@ -47,4 +45,4 @@ function parse(text) {
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = parse;
|
||||
export default parse;
|
||||
@@ -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;
|
||||
@@ -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;
|
||||
@@ -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;
|
||||
@@ -1,5 +1,3 @@
|
||||
"use strict";
|
||||
|
||||
const getLast = (arr) => arr[arr.length - 1];
|
||||
|
||||
module.exports = getLast;
|
||||
export default getLast;
|
||||
@@ -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;
|
||||
@@ -1,5 +1,3 @@
|
||||
"use strict";
|
||||
|
||||
/**
|
||||
* @param {any} object
|
||||
* @returns {object is Array<any>}
|
||||
@@ -8,4 +6,4 @@ function isNonEmptyArray(object) {
|
||||
return Array.isArray(object) && object.length > 0;
|
||||
}
|
||||
|
||||
module.exports = isNonEmptyArray;
|
||||
export default isNonEmptyArray;
|
||||
@@ -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;
|
||||
@@ -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;
|
||||
@@ -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;
|
||||
@@ -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."
|
||||
}
|
||||
@@ -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;
|
||||
@@ -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;
|
||||
@@ -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;
|
||||
@@ -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;
|
||||
@@ -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 };
|
||||
@@ -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;
|
||||
Reference in new issue
Block a user