Refactor wrapExpression (#18607)
This commit is contained in:
1 parent
303391a94a
commit
d2d34ee619
6 files changed
+73
-45
No files matched your search
+1
-1
@@ -454,7 +454,7 @@ const configs = [
|
|||||||
"src/language-js/parse/json.js",
|
"src/language-js/parse/json.js",
|
||||||
"src/language-js/parse/acorn.js",
|
"src/language-js/parse/acorn.js",
|
||||||
"src/language-js/parse/oxc.js",
|
"src/language-js/parse/oxc.js",
|
||||||
"src/language-js/parse/utilities/wrap-babel-expression.js",
|
"src/language-js/parse/utilities/wrap-expression.js",
|
||||||
],
|
],
|
||||||
"prettier-internal-rules/prefer-create-type-check-function": [
|
"prettier-internal-rules/prefer-create-type-check-function": [
|
||||||
"error",
|
"error",
|
||||||
|
|||||||
Vendored
+16
-5
@@ -6,6 +6,11 @@ import {
|
|||||||
} from "angular-estree-parser";
|
} from "angular-estree-parser";
|
||||||
import createError from "../../common/parser-create-error.js";
|
import createError from "../../common/parser-create-error.js";
|
||||||
import { locEnd, locStart } from "../loc.js";
|
import { locEnd, locStart } from "../loc.js";
|
||||||
|
import wrapExpression from "./utilities/wrap-expression.js";
|
||||||
|
|
||||||
|
/**
|
||||||
|
@import {TupleTypeAnnotation} from "@babel/types"
|
||||||
|
*/
|
||||||
|
|
||||||
function createParseError(error) {
|
function createParseError(error) {
|
||||||
/* c8 ignore next 3 */
|
/* c8 ignore next 3 */
|
||||||
@@ -27,7 +32,12 @@ function createParseError(error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@typedef {parseAction | parseBinding | parseInterpolationExpression | parseTemplateBindings} Parsers
|
@typedef {
|
||||||
|
| parseAction
|
||||||
|
| parseBinding
|
||||||
|
| parseInterpolationExpression
|
||||||
|
| parseTemplateBindings
|
||||||
|
} Parsers
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -38,7 +48,7 @@ function createParser(parseMethod) {
|
|||||||
return {
|
return {
|
||||||
astFormat: "estree",
|
astFormat: "estree",
|
||||||
parse(text) {
|
parse(text) {
|
||||||
/** @type {ReturnType<ParseMethod>} */
|
/** @type {Exclude<ReturnType<ParseMethod>, TupleTypeAnnotation>} */
|
||||||
let node;
|
let node;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -57,11 +67,12 @@ function createParser(parseMethod) {
|
|||||||
node = { ...node, type: "NGChainedExpression", expressions: [node] };
|
node = { ...node, type: "NGChainedExpression", expressions: [node] };
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return wrapExpression({
|
||||||
type: "NGRoot",
|
type: "NGRoot",
|
||||||
node,
|
expression: node,
|
||||||
comments,
|
comments,
|
||||||
};
|
text,
|
||||||
|
});
|
||||||
},
|
},
|
||||||
locStart,
|
locStart,
|
||||||
locEnd,
|
locEnd,
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import {
|
|||||||
SOURCE_TYPE_COMMONJS,
|
SOURCE_TYPE_COMMONJS,
|
||||||
SOURCE_TYPE_MODULE,
|
SOURCE_TYPE_MODULE,
|
||||||
} from "./utilities/source-types.js";
|
} from "./utilities/source-types.js";
|
||||||
import wrapBabelExpression from "./utilities/wrap-babel-expression.js";
|
import wrapExpression from "./utilities/wrap-expression.js";
|
||||||
|
|
||||||
const createBabelParser = (options) => createParser(createParse(options));
|
const createBabelParser = (options) => createParser(createParse(options));
|
||||||
|
|
||||||
@@ -166,7 +166,11 @@ function createParse({ isExpression = false, optionsCombinations }) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (isExpression) {
|
if (isExpression) {
|
||||||
ast = wrapBabelExpression(ast, { text, rootMarker: options.rootMarker });
|
ast = wrapExpression({
|
||||||
|
expression: ast,
|
||||||
|
text,
|
||||||
|
rootMarker: options.rootMarker,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return postprocess(ast, { text });
|
return postprocess(ast, { text });
|
||||||
|
|||||||
@@ -1,25 +0,0 @@
|
|||||||
/**
|
|
||||||
* @param {{
|
|
||||||
* text: string,
|
|
||||||
* type?: string,
|
|
||||||
* rootMarker?: string,
|
|
||||||
* }} options
|
|
||||||
*/
|
|
||||||
function wrapBabelExpression(expression, options) {
|
|
||||||
const { type = "JsExpressionRoot", rootMarker, text } = options;
|
|
||||||
|
|
||||||
const { tokens, comments } = expression;
|
|
||||||
delete expression.tokens;
|
|
||||||
delete expression.comments;
|
|
||||||
|
|
||||||
return {
|
|
||||||
tokens,
|
|
||||||
comments,
|
|
||||||
type,
|
|
||||||
node: expression,
|
|
||||||
range: [0, text.length],
|
|
||||||
rootMarker,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export default wrapBabelExpression;
|
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
/**
|
||||||
|
@import {Comment, Node} from "../../types/estree.js"
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {{
|
||||||
|
* text: string,
|
||||||
|
* expression?: Node,
|
||||||
|
* type?: string,
|
||||||
|
* rootMarker?: string,
|
||||||
|
* comments?: Comment[],
|
||||||
|
* }} options
|
||||||
|
*/
|
||||||
|
function wrapExpression(options) {
|
||||||
|
const {
|
||||||
|
type = "JsExpressionRoot",
|
||||||
|
expression,
|
||||||
|
comments = expression?.comments ?? [],
|
||||||
|
text,
|
||||||
|
rootMarker,
|
||||||
|
} = options;
|
||||||
|
|
||||||
|
const root = {
|
||||||
|
type,
|
||||||
|
comments,
|
||||||
|
range: [0, text.length],
|
||||||
|
rootMarker,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (expression) {
|
||||||
|
delete expression.comments;
|
||||||
|
root.node = expression;
|
||||||
|
}
|
||||||
|
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default wrapExpression;
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
import { parse, parseExpression } from "@babel/parser";
|
import { parse, parseExpression } from "@babel/parser";
|
||||||
import createError from "../../common/parser-create-error.js";
|
import createError from "../../common/parser-create-error.js";
|
||||||
import createBabelParseError from "../../language-js/parse/utilities/create-babel-parse-error.js";
|
import createBabelParseError from "../../language-js/parse/utilities/create-babel-parse-error.js";
|
||||||
import wrapBabelExpression from "../../language-js/parse/utilities/wrap-babel-expression.js";
|
import wrapExpression from "../../language-js/parse/utilities/wrap-expression.js";
|
||||||
import isNonEmptyArray from "../../utilities/is-non-empty-array.js";
|
import isNonEmptyArray from "../../utilities/is-non-empty-array.js";
|
||||||
|
|
||||||
const babelParseOptions = {
|
const babelParseOptions = {
|
||||||
@@ -30,15 +30,17 @@ function parseEmptyJson(text) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
return file;
|
return { comments: file.comments };
|
||||||
}
|
}
|
||||||
|
|
||||||
function parseJson(text, options = {}) {
|
function parseJson(text, options = {}) {
|
||||||
const { allowComments = true, allowEmpty = false } = options;
|
const { allowComments = true, allowEmpty = false } = options;
|
||||||
|
|
||||||
let ast;
|
let ast;
|
||||||
|
let comments;
|
||||||
try {
|
try {
|
||||||
ast = parseExpression(text, babelParseOptions);
|
ast = parseExpression(text, babelParseOptions);
|
||||||
|
comments = ast.comments;
|
||||||
} catch (/** @type {any} */ error) {
|
} catch (/** @type {any} */ error) {
|
||||||
if (
|
if (
|
||||||
allowEmpty &&
|
allowEmpty &&
|
||||||
@@ -46,29 +48,27 @@ function parseJson(text, options = {}) {
|
|||||||
error.reasonCode === "ParseExpressionEmptyInput"
|
error.reasonCode === "ParseExpressionEmptyInput"
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
ast = parseEmptyJson(text);
|
({ comments } = parseEmptyJson(text));
|
||||||
} catch {
|
} catch {
|
||||||
// No op
|
// No op
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!ast) {
|
if (!comments) {
|
||||||
throw createBabelParseError(error);
|
throw createBabelParseError(error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!allowComments && isNonEmptyArray(ast.comments)) {
|
if (!allowComments && isNonEmptyArray(comments)) {
|
||||||
throw createJsonError(ast.comments[0], "Comment");
|
throw createJsonError(comments[0], "Comment");
|
||||||
}
|
}
|
||||||
|
|
||||||
ast = wrapBabelExpression(ast, { type: "JsonRoot", text });
|
if (!allowEmpty || ast) {
|
||||||
|
assertJsonNode(ast);
|
||||||
if (ast.node.type === "File") {
|
|
||||||
delete ast.node;
|
|
||||||
} else {
|
|
||||||
assertJsonNode(ast.node);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ast = wrapExpression({ type: "JsonRoot", expression: ast, comments, text });
|
||||||
|
|
||||||
return ast;
|
return ast;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user