Migrate YAML language to ES Module (#12339)
This commit is contained in:
1 parent
0040204e2c
commit
b05812737d
19 files changed
+105
-119
No files matched your search
@@ -180,7 +180,6 @@ module.exports = {
|
||||
"src/language-handlebars/**/*.js",
|
||||
"src/language-html/**/*.js",
|
||||
"src/language-js/**/*.js",
|
||||
"src/language-yaml/**/*.js",
|
||||
"src/main/**/*.js",
|
||||
"src/utils/**/*.js",
|
||||
"scripts/tools/eslint-plugin-prettier-internal-rules/**/*.js",
|
||||
|
||||
@@ -91,18 +91,18 @@ function* getEsbuildOptions(bundle, buildOptions) {
|
||||
"src/language-handlebars/parsers.js",
|
||||
"src/language-html/parsers.js",
|
||||
"src/language-js/parse/parsers.js",
|
||||
"src/language-yaml/parsers.js",
|
||||
]) {
|
||||
replaceModule[path.join(PROJECT_ROOT, file)] = EMPTY_MODULE_REPLACEMENT;
|
||||
}
|
||||
for (const file of [
|
||||
// "src/language-css/parsers.js",
|
||||
// "src/language-graphql/parsers.js",
|
||||
"src/language-graphql/parsers.js",
|
||||
// "src/language-handlebars/parsers.js",
|
||||
// "src/language-html/parsers.js",
|
||||
// "src/language-js/parse/parsers.js",
|
||||
"src/language-markdown/parsers.js",
|
||||
// "src/language-yaml/parsers.js",
|
||||
"src/language-yaml/parsers.js",
|
||||
]) {
|
||||
replaceModule[path.join(PROJECT_ROOT, file)] =
|
||||
EXPORT_UNDEFINED_MODULE_REPLACEMENT;
|
||||
|
||||
@@ -152,6 +152,7 @@ const parsers = [
|
||||
.resolve("tslib")
|
||||
.replace(/tslib\.js$/, "tslib.es6.js"),
|
||||
},
|
||||
isEsm: true,
|
||||
},
|
||||
].map((bundle) => {
|
||||
const { name } = bundle.input.match(
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
"use strict";
|
||||
|
||||
function embed(path, print, textToDoc, options) {
|
||||
const node = path.getValue();
|
||||
|
||||
@@ -13,4 +11,4 @@ function embed(path, print, textToDoc, options) {
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = embed;
|
||||
export default embed;
|
||||
@@ -1,9 +1,10 @@
|
||||
"use strict";
|
||||
import { createRequire } from "module";
|
||||
import createLanguage from "../utils/create-language.js";
|
||||
import printer from "./printer-yaml.js";
|
||||
import options from "./options.js";
|
||||
import parsers from "./parsers.js";
|
||||
|
||||
const createLanguage = require("../utils/create-language.js");
|
||||
const printer = require("./printer-yaml.js");
|
||||
const options = require("./options.js");
|
||||
const parsers = require("./parsers.js");
|
||||
const require = createRequire(import.meta.url);
|
||||
|
||||
const languages = [
|
||||
createLanguage(require("linguist-languages/data/YAML.json"), (data) => ({
|
||||
@@ -19,9 +20,11 @@ const languages = [
|
||||
})),
|
||||
];
|
||||
|
||||
module.exports = {
|
||||
const language = {
|
||||
languages,
|
||||
printers: { yaml: printer },
|
||||
options,
|
||||
parsers,
|
||||
};
|
||||
|
||||
export default language;
|
||||
@@ -1,5 +1,3 @@
|
||||
"use strict";
|
||||
|
||||
function locStart(node) {
|
||||
return node.position.start.offset;
|
||||
}
|
||||
@@ -8,4 +6,4 @@ function locEnd(node) {
|
||||
return node.position.end.offset;
|
||||
}
|
||||
|
||||
module.exports = { locStart, locEnd };
|
||||
export { locStart, locEnd };
|
||||
@@ -1,10 +1,10 @@
|
||||
"use strict";
|
||||
|
||||
const commonOptions = require("../common/common-options.js");
|
||||
import commonOptions from "../common/common-options.js";
|
||||
|
||||
// format based on https://github.com/prettier/prettier/blob/main/src/main/core-options.js
|
||||
module.exports = {
|
||||
const options = {
|
||||
bracketSpacing: commonOptions.bracketSpacing,
|
||||
singleQuote: commonOptions.singleQuote,
|
||||
proseWrap: commonOptions.proseWrap,
|
||||
};
|
||||
|
||||
export default options;
|
||||
@@ -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,14 +1,13 @@
|
||||
"use strict";
|
||||
|
||||
const createError = require("../common/parser-create-error.js");
|
||||
const { hasPragma } = require("./pragma.js");
|
||||
const { locStart, locEnd } = require("./loc.js");
|
||||
// TODO[@fisker]: try inline import this module
|
||||
// Inline the require to avoid loading all the JS if we don't use it
|
||||
import { parse as parseYaml } from "yaml-unist-parser";
|
||||
import createError from "../common/parser-create-error.js";
|
||||
import { hasPragma } from "./pragma.js";
|
||||
import { locStart, locEnd } from "./loc.js";
|
||||
|
||||
function parse(text) {
|
||||
const { parse } = require("yaml-unist-parser");
|
||||
|
||||
try {
|
||||
const root = parse(text);
|
||||
const root = parseYaml(text);
|
||||
|
||||
/**
|
||||
* suppress `comment not printed` error
|
||||
@@ -37,8 +36,10 @@ const parser = {
|
||||
locEnd,
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
const yaml = {
|
||||
parsers: {
|
||||
yaml: parser,
|
||||
},
|
||||
};
|
||||
|
||||
export default yaml;
|
||||
@@ -1,7 +1,9 @@
|
||||
"use strict";
|
||||
import yamlParser from "./parser-yaml.js";
|
||||
|
||||
module.exports = {
|
||||
const parsers = {
|
||||
get yaml() {
|
||||
return require("./parser-yaml.js").parsers.yaml;
|
||||
return yamlParser.parsers.yaml;
|
||||
},
|
||||
};
|
||||
|
||||
export default parsers;
|
||||
@@ -1,5 +1,3 @@
|
||||
"use strict";
|
||||
|
||||
function isPragma(text) {
|
||||
return /^\s*@(?:prettier|format)\s*$/.test(text);
|
||||
}
|
||||
@@ -12,8 +10,4 @@ function insertPragma(text) {
|
||||
return `# @format\n\n${text}`;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
isPragma,
|
||||
hasPragma,
|
||||
insertPragma,
|
||||
};
|
||||
export { isPragma, hasPragma, insertPragma };
|
||||
@@ -1,5 +1,4 @@
|
||||
"use strict";
|
||||
const { defineShortcut, mapNode } = require("./utils.js");
|
||||
import { defineShortcut, mapNode } from "./utils.js";
|
||||
|
||||
function preprocess(ast) {
|
||||
return mapNode(ast, defineShortcuts);
|
||||
@@ -27,4 +26,4 @@ function defineShortcuts(node) {
|
||||
return node;
|
||||
}
|
||||
|
||||
module.exports = preprocess;
|
||||
export default preprocess;
|
||||
@@ -1,7 +1,15 @@
|
||||
"use strict";
|
||||
|
||||
/** @typedef {import("../../document").Doc} Doc */
|
||||
|
||||
import doc from "../../document/index.js";
|
||||
import {
|
||||
getAncestorCount,
|
||||
getBlockValueLineContents,
|
||||
hasIndicatorComment,
|
||||
isLastDescendantNode,
|
||||
isNode,
|
||||
} from "../utils.js";
|
||||
import { alignWithSpaces } from "./misc.js";
|
||||
|
||||
const {
|
||||
builders: {
|
||||
dedent,
|
||||
@@ -14,15 +22,7 @@ const {
|
||||
markAsRoot,
|
||||
},
|
||||
utils: { getDocParts },
|
||||
} = require("../../document/index.js");
|
||||
const {
|
||||
getAncestorCount,
|
||||
getBlockValueLineContents,
|
||||
hasIndicatorComment,
|
||||
isLastDescendantNode,
|
||||
isNode,
|
||||
} = require("../utils.js");
|
||||
const { alignWithSpaces } = require("./misc.js");
|
||||
} = doc;
|
||||
|
||||
function printBlock(path, print, options) {
|
||||
const node = path.getValue();
|
||||
@@ -79,4 +79,4 @@ function printBlock(path, print, options) {
|
||||
return parts;
|
||||
}
|
||||
|
||||
module.exports = printBlock;
|
||||
export default printBlock;
|
||||
@@ -1,10 +1,10 @@
|
||||
"use strict";
|
||||
import doc from "../../document/index.js";
|
||||
import { isEmptyNode, getLast, hasEndComments } from "../utils.js";
|
||||
import { printNextEmptyLine, alignWithSpaces } from "./misc.js";
|
||||
|
||||
const {
|
||||
builders: { ifBreak, line, softline, hardline, join },
|
||||
} = require("../../document/index.js");
|
||||
const { isEmptyNode, getLast, hasEndComments } = require("../utils.js");
|
||||
const { printNextEmptyLine, alignWithSpaces } = require("./misc.js");
|
||||
} = doc;
|
||||
|
||||
function printFlowMapping(path, print, options) {
|
||||
const node = path.getValue();
|
||||
@@ -60,8 +60,8 @@ function printChildren(path, print, options) {
|
||||
return parts;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
export {
|
||||
printFlowMapping,
|
||||
// Alias
|
||||
printFlowSequence: printFlowMapping,
|
||||
printFlowMapping as printFlowSequence,
|
||||
};
|
||||
@@ -1,11 +1,7 @@
|
||||
"use strict";
|
||||
|
||||
/** @typedef {import("../../document").Doc} Doc */
|
||||
|
||||
const {
|
||||
builders: { conditionalGroup, group, hardline, ifBreak, join, line },
|
||||
} = require("../../document/index.js");
|
||||
const {
|
||||
import doc from "../../document/index.js";
|
||||
import {
|
||||
hasLeadingComments,
|
||||
hasMiddleComments,
|
||||
hasTrailingComment,
|
||||
@@ -13,8 +9,12 @@ const {
|
||||
isNode,
|
||||
isEmptyNode,
|
||||
isInlineNode,
|
||||
} = require("../utils.js");
|
||||
const { alignWithSpaces } = require("./misc.js");
|
||||
} from "../utils.js";
|
||||
import { alignWithSpaces } from "./misc.js";
|
||||
|
||||
const {
|
||||
builders: { conditionalGroup, group, hardline, ifBreak, join, line },
|
||||
} = doc;
|
||||
|
||||
function printMappingItem(node, parentNode, path, print, options) {
|
||||
const { key, value } = node;
|
||||
@@ -208,4 +208,4 @@ function isSingleLineNode(node) {
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = printMappingItem;
|
||||
export default printMappingItem;
|
||||
@@ -1,10 +1,9 @@
|
||||
"use strict";
|
||||
import doc from "../../document/index.js";
|
||||
import { hasEndComments, isNextLineEmpty, isNode } from "../utils.js";
|
||||
|
||||
const {
|
||||
builders: { softline, align },
|
||||
} = require("../../document/index.js");
|
||||
|
||||
const { hasEndComments, isNextLineEmpty, isNode } = require("../utils.js");
|
||||
} = doc;
|
||||
|
||||
const printedEmptyLineCache = new WeakMap();
|
||||
function printNextEmptyLine(path, originalText) {
|
||||
@@ -48,8 +47,4 @@ function alignWithSpaces(width, doc) {
|
||||
return align(" ".repeat(width), doc);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
alignWithSpaces,
|
||||
shouldPrintEndComments,
|
||||
printNextEmptyLine,
|
||||
};
|
||||
export { alignWithSpaces, shouldPrintEndComments, printNextEmptyLine };
|
||||
@@ -1,7 +1,35 @@
|
||||
"use strict";
|
||||
|
||||
/** @typedef {import("../document").Doc} Doc */
|
||||
|
||||
import doc from "../document/index.js";
|
||||
import { isPreviousLineEmpty } from "../common/util.js";
|
||||
import { insertPragma, isPragma } from "./pragma.js";
|
||||
import { locStart } from "./loc.js";
|
||||
import embed from "./embed.js";
|
||||
import {
|
||||
getFlowScalarLineContents,
|
||||
getLastDescendantNode,
|
||||
hasLeadingComments,
|
||||
hasMiddleComments,
|
||||
hasTrailingComment,
|
||||
hasEndComments,
|
||||
hasPrettierIgnore,
|
||||
isLastDescendantNode,
|
||||
isNode,
|
||||
isInlineNode,
|
||||
} from "./utils.js";
|
||||
import preprocess from "./print-preprocess.js";
|
||||
import {
|
||||
alignWithSpaces,
|
||||
printNextEmptyLine,
|
||||
shouldPrintEndComments,
|
||||
} from "./print/misc.js";
|
||||
import {
|
||||
printFlowMapping,
|
||||
printFlowSequence,
|
||||
} from "./print/flow-mapping-sequence.js";
|
||||
import printMappingItem from "./print/mapping-item.js";
|
||||
import printBlock from "./print/block.js";
|
||||
|
||||
const {
|
||||
builders: {
|
||||
breakParent,
|
||||
@@ -14,35 +42,7 @@ const {
|
||||
literalline,
|
||||
},
|
||||
utils: { getDocParts, replaceTextEndOfLine },
|
||||
} = require("../document/index.js");
|
||||
const { isPreviousLineEmpty } = require("../common/util.js");
|
||||
const { insertPragma, isPragma } = require("./pragma.js");
|
||||
const { locStart } = require("./loc.js");
|
||||
const embed = require("./embed.js");
|
||||
const {
|
||||
getFlowScalarLineContents,
|
||||
getLastDescendantNode,
|
||||
hasLeadingComments,
|
||||
hasMiddleComments,
|
||||
hasTrailingComment,
|
||||
hasEndComments,
|
||||
hasPrettierIgnore,
|
||||
isLastDescendantNode,
|
||||
isNode,
|
||||
isInlineNode,
|
||||
} = require("./utils.js");
|
||||
const preprocess = require("./print-preprocess.js");
|
||||
const {
|
||||
alignWithSpaces,
|
||||
printNextEmptyLine,
|
||||
shouldPrintEndComments,
|
||||
} = require("./print/misc.js");
|
||||
const {
|
||||
printFlowMapping,
|
||||
printFlowSequence,
|
||||
} = require("./print/flow-mapping-sequence.js");
|
||||
const printMappingItem = require("./print/mapping-item.js");
|
||||
const printBlock = require("./print/block.js");
|
||||
} = doc;
|
||||
|
||||
function genericPrint(path, options, print) {
|
||||
const node = path.getValue();
|
||||
@@ -443,10 +443,12 @@ function clean(node, newNode /*, parent */) {
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
const printer = {
|
||||
preprocess,
|
||||
embed,
|
||||
print: genericPrint,
|
||||
massageAstNode: clean,
|
||||
insertPragma,
|
||||
};
|
||||
|
||||
export default printer;
|
||||
@@ -1,6 +1,4 @@
|
||||
"use strict";
|
||||
|
||||
const { getLast, isNonEmptyArray } = require("../common/util.js");
|
||||
import { getLast, isNonEmptyArray } from "../common/util.js";
|
||||
|
||||
function getAncestorCount(path, filter) {
|
||||
let counter = 0;
|
||||
@@ -352,7 +350,7 @@ function isInlineNode(node) {
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
export {
|
||||
getLast,
|
||||
getAncestorCount,
|
||||
isNode,
|
||||
|
||||
+2
-1
@@ -1,6 +1,7 @@
|
||||
import { createRequire } from "module";
|
||||
import graphql from "./language-graphql/index.js";
|
||||
import markdown from "./language-markdown/index.js";
|
||||
import yaml from "./language-yaml/index.js";
|
||||
|
||||
const require = createRequire(import.meta.url);
|
||||
|
||||
@@ -19,7 +20,7 @@ const languages = [
|
||||
// HTML
|
||||
require("./language-html/index.js"),
|
||||
// YAML
|
||||
require("./language-yaml/index.js"),
|
||||
yaml,
|
||||
];
|
||||
|
||||
export default languages;
|
||||
Reference in new issue
Block a user