Migrate GraphQL language to ES Module (#12321)
This commit is contained in:
1 parent
15781c91d4
commit
0040204e2c
12 files changed
+56
-51
No files matched your search
@@ -177,7 +177,6 @@ module.exports = {
|
||||
"src/config/**/*.js",
|
||||
"src/document/**/*.js",
|
||||
"src/language-css/**/*.js",
|
||||
"src/language-graphql/**/*.js",
|
||||
"src/language-handlebars/**/*.js",
|
||||
"src/language-html/**/*.js",
|
||||
"src/language-js/**/*.js",
|
||||
|
||||
@@ -88,16 +88,22 @@ function* getEsbuildOptions(bundle, buildOptions) {
|
||||
// Replace parser getters with `undefined`
|
||||
for (const file of [
|
||||
"src/language-css/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",
|
||||
]) {
|
||||
replaceModule[path.join(PROJECT_ROOT, file)] = EMPTY_MODULE_REPLACEMENT;
|
||||
}
|
||||
for (const file of ["src/language-markdown/parsers.js"]) {
|
||||
for (const file of [
|
||||
// "src/language-css/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",
|
||||
]) {
|
||||
replaceModule[path.join(PROJECT_ROOT, file)] =
|
||||
EXPORT_UNDEFINED_MODULE_REPLACEMENT;
|
||||
}
|
||||
|
||||
@@ -124,6 +124,7 @@ const parsers = [
|
||||
},
|
||||
{
|
||||
input: "src/language-graphql/parser-graphql.js",
|
||||
isEsm: true,
|
||||
},
|
||||
{
|
||||
input: "src/language-markdown/parser-markdown.js",
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
"use strict";
|
||||
import { createRequire } from "module";
|
||||
import createLanguage from "../utils/create-language.js";
|
||||
import printer from "./printer-graphql.js";
|
||||
import options from "./options.js";
|
||||
import parsers from "./parsers.js";
|
||||
|
||||
const createLanguage = require("../utils/create-language.js");
|
||||
const printer = require("./printer-graphql.js");
|
||||
const options = require("./options.js");
|
||||
const parsers = require("./parsers.js");
|
||||
const require = createRequire(import.meta.url);
|
||||
|
||||
const languages = [
|
||||
createLanguage(require("linguist-languages/data/GraphQL.json"), () => ({
|
||||
@@ -17,9 +18,11 @@ const printers = {
|
||||
graphql: printer,
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
const language = {
|
||||
languages,
|
||||
options,
|
||||
printers,
|
||||
parsers,
|
||||
};
|
||||
|
||||
export default language;
|
||||
@@ -1,5 +1,3 @@
|
||||
"use strict";
|
||||
|
||||
function locStart(node) {
|
||||
if (typeof node.start === "number") {
|
||||
return node.start;
|
||||
@@ -14,4 +12,4 @@ function locEnd(node) {
|
||||
return node.loc && node.loc.end;
|
||||
}
|
||||
|
||||
module.exports = { locStart, locEnd };
|
||||
export { locStart, locEnd };
|
||||
@@ -1,8 +1,8 @@
|
||||
"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,
|
||||
};
|
||||
|
||||
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,9 +1,11 @@
|
||||
"use strict";
|
||||
|
||||
const createError = require("../common/parser-create-error.js");
|
||||
const tryCombinations = require("../utils/try-combinations.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 parseGraphql } from "graphql/language/parser.mjs";
|
||||
import { GraphQLError } from "graphql/error/GraphQLError.mjs";
|
||||
import createError from "../common/parser-create-error.js";
|
||||
import tryCombinations from "../utils/try-combinations.js";
|
||||
import { hasPragma } from "./pragma.js";
|
||||
import { locStart, locEnd } from "./loc.js";
|
||||
|
||||
function parseComments(ast) {
|
||||
const comments = [];
|
||||
@@ -43,7 +45,6 @@ const parseOptions = {
|
||||
};
|
||||
|
||||
function createParseError(error) {
|
||||
const { GraphQLError } = require("graphql/error/GraphQLError");
|
||||
if (error instanceof GraphQLError) {
|
||||
const {
|
||||
message,
|
||||
@@ -57,12 +58,13 @@ function createParseError(error) {
|
||||
}
|
||||
|
||||
function parse(text /*, parsers, opts*/) {
|
||||
// Inline the require to avoid loading all the JS if we don't use it
|
||||
const { parse } = require("graphql/language/parser");
|
||||
const { result: ast, error } = tryCombinations(
|
||||
() => parse(text, { ...parseOptions }),
|
||||
() => parseGraphql(text, { ...parseOptions }),
|
||||
() =>
|
||||
parse(text, { ...parseOptions, allowLegacySDLImplementsInterfaces: true })
|
||||
parseGraphql(text, {
|
||||
...parseOptions,
|
||||
allowLegacySDLImplementsInterfaces: true,
|
||||
})
|
||||
);
|
||||
|
||||
if (!ast) {
|
||||
@@ -74,7 +76,7 @@ function parse(text /*, parsers, opts*/) {
|
||||
return ast;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
const graphql = {
|
||||
parsers: {
|
||||
graphql: {
|
||||
parse,
|
||||
@@ -85,3 +87,5 @@ module.exports = {
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
export default graphql;
|
||||
@@ -1,7 +1,9 @@
|
||||
"use strict";
|
||||
import graphqlParser from "./parser-graphql.js";
|
||||
|
||||
module.exports = {
|
||||
const parsers = {
|
||||
get graphql() {
|
||||
return require("./parser-graphql.js").parsers.graphql;
|
||||
return graphqlParser.parsers.graphql;
|
||||
},
|
||||
};
|
||||
|
||||
export default parsers;
|
||||
@@ -1,5 +1,3 @@
|
||||
"use strict";
|
||||
|
||||
function hasPragma(text) {
|
||||
return /^\s*#[^\S\n]*@(?:format|prettier)\s*(?:\n|$)/.test(text);
|
||||
}
|
||||
@@ -8,7 +6,4 @@ function insertPragma(text) {
|
||||
return "# @format\n\n" + text;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
hasPragma,
|
||||
insertPragma,
|
||||
};
|
||||
export { hasPragma, insertPragma };
|
||||
@@ -1,11 +1,11 @@
|
||||
"use strict";
|
||||
import doc from "../document/index.js";
|
||||
import { isNextLineEmpty, isNonEmptyArray } from "../common/util.js";
|
||||
import { insertPragma } from "./pragma.js";
|
||||
import { locStart, locEnd } from "./loc.js";
|
||||
|
||||
const {
|
||||
builders: { join, hardline, line, softline, group, indent, ifBreak },
|
||||
} = require("../document/index.js");
|
||||
const { isNextLineEmpty, isNonEmptyArray } = require("../common/util.js");
|
||||
const { insertPragma } = require("./pragma.js");
|
||||
const { locStart, locEnd } = require("./loc.js");
|
||||
} = doc;
|
||||
|
||||
function genericPrint(path, options, print) {
|
||||
const node = path.getValue();
|
||||
@@ -611,7 +611,7 @@ function hasPrettierIgnore(path) {
|
||||
);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
const printer = {
|
||||
print: genericPrint,
|
||||
massageAstNode: clean,
|
||||
hasPrettierIgnore,
|
||||
@@ -619,3 +619,5 @@ module.exports = {
|
||||
printComment,
|
||||
canAttachComment,
|
||||
};
|
||||
|
||||
export default printer;
|
||||
+2
-2
@@ -1,5 +1,5 @@
|
||||
import { createRequire } from "module";
|
||||
|
||||
import graphql from "./language-graphql/index.js";
|
||||
import markdown from "./language-markdown/index.js";
|
||||
|
||||
const require = createRequire(import.meta.url);
|
||||
@@ -13,7 +13,7 @@ const languages = [
|
||||
// Handlebars
|
||||
require("./language-handlebars/index.js"),
|
||||
// GraphQL
|
||||
require("./language-graphql/index.js"),
|
||||
graphql,
|
||||
// Markdown
|
||||
markdown,
|
||||
// HTML
|
||||
|
||||
Reference in new issue
Block a user