Stop manipulate options in parsers (#14463)

This commit is contained in:
fisker Cheung authored and GitHub committed 2023-03-07 17:43:22 +08:00
1 parent 134f6078e1
commit 88b67b57e4
12 files changed
+56 -61

No files matched your search

+1 -2
View File
@@ -79,8 +79,7 @@ function parse(text, options = {}) {
throw createParseError(error);
}
options.originalText = text;
return postprocess(ast, options);
return postprocess(ast, { text });
}
export default createParser(parse);
+8 -10
View File
@@ -104,17 +104,17 @@ function parseWithOptions(parse, text, options) {
}
function createParse({ isExpression = false, optionsCombinations }) {
return (text, opts = {}) => {
return (text, options = {}) => {
if (
(opts.parser === "babel" || opts.parser === "__babel_estree") &&
isFlowFile(text, opts)
(options.parser === "babel" || options.parser === "__babel_estree") &&
isFlowFile(text, options)
) {
opts.parser = "babel-flow";
return babelFlow.parse(text, opts);
options.parser = "babel-flow";
return babelFlow.parse(text, options);
}
let combinations = optionsCombinations;
const sourceType = opts.__babelSourceType ?? getSourceType(opts);
const sourceType = options.__babelSourceType ?? getSourceType(options);
if (sourceType === "script") {
combinations = combinations.map((options) => ({
...options,
@@ -158,13 +158,11 @@ function createParse({ isExpression = false, optionsCombinations }) {
throw createBabelParseError(error);
}
opts.originalText = text;
if (isExpression) {
ast = wrapBabelExpression(ast, opts);
ast = wrapBabelExpression(ast, { text, rootMarker: options.rootMarker });
}
return postprocess(ast, opts);
return postprocess(ast, { parser: "babel", text });
};
}
+1 -2
View File
@@ -52,8 +52,7 @@ function parse(text, options = {}) {
throw createParseError(error);
}
options.originalText = text;
return postprocess(ast, options);
return postprocess(ast, { text });
}
export default createParser(parse);
+2 -3
View File
@@ -40,15 +40,14 @@ function createParseError(error) {
});
}
function parse(text, options = {}) {
function parse(text) {
const ast = flowParser.parse(replaceHashbang(text), parseOptions);
const [error] = ast.errors;
if (error) {
throw createParseError(error);
}
options.originalText = text;
return postprocess(ast, options);
return postprocess(ast, { text });
}
// Export as a plugin so we can reuse the same bundle for UMD loading
+1 -2
View File
@@ -99,8 +99,7 @@ function parse(text, options = {}) {
throw createParseError(error);
}
options.originalText = text;
return postprocess(ast, options);
return postprocess(ast, { parser: "meriyah", text });
}
const parser = {
+17 -17
View File
@@ -6,7 +6,15 @@ import isIndentableBlockComment from "../../utils/is-indentable-block-comment.js
import visitNode from "./visit-node.js";
import throwSyntaxError from "./throw-ts-syntax-error.js";
/**
* @param {{
* text: string,
* parser?: string,
* }} options
*/
function postprocess(ast, options) {
const { parser, text } = options;
// `InterpreterDirective` from babel parser
// Other parsers parse it as comment, babel treat it as comment too
// https://github.com/babel/babel/issues/15116
@@ -20,13 +28,7 @@ function postprocess(ast, options) {
}
// Keep Babel's non-standard ParenthesizedExpression nodes only if they have Closure-style type cast comments.
if (
options.parser !== "typescript" &&
options.parser !== "flow" &&
options.parser !== "acorn" &&
options.parser !== "espree" &&
options.parser !== "meriyah"
) {
if (parser === "babel") {
const startOffsetsOfTypeCastedNodes = new Set();
// Comments might be attached not directly to ParenthesizedExpression but to its ancestor.
@@ -92,7 +94,7 @@ function postprocess(ast, options) {
break;
case "ObjectExpression":
// #12963
if (options.parser === "typescript") {
if (parser === "typescript") {
const invalidProperty = node.properties.find(
(property) =>
property.type === "Property" &&
@@ -131,16 +133,14 @@ function postprocess(ast, options) {
// For hack-style pipeline
case "TopicReference":
options.__isUsingHackPipeline = true;
ast.extra = { ...ast.extra, __isUsingHackPipeline: true };
break;
// TODO: Remove this when https://github.com/meriyah/meriyah/issues/200 get fixed
case "ExportAllDeclaration": {
const { exported } = node;
if (options.parser === "meriyah" && exported?.type === "Identifier") {
const raw = options.originalText.slice(
locStart(exported),
locEnd(exported)
);
if (parser === "meriyah" && exported?.type === "Identifier") {
const raw = text.slice(locStart(exported), locEnd(exported));
if (raw.startsWith('"') || raw.startsWith("'")) {
node.exported = {
...node.exported,
@@ -155,7 +155,7 @@ function postprocess(ast, options) {
// TODO: Remove this when https://github.com/meriyah/meriyah/issues/231 get fixed
case "PropertyDefinition":
if (
options.parser === "meriyah" &&
parser === "meriyah" &&
node.static &&
!node.computed &&
!node.key
@@ -197,7 +197,7 @@ function postprocess(ast, options) {
// In `typescript`/`espree`/`flow`, `Program` doesn't count whitespace and comments
// See https://github.com/eslint/espree/issues/488
if (ast.type === "Program") {
ast.range = [0, options.originalText.length];
ast.range = [0, text.length];
}
return ast;
@@ -206,7 +206,7 @@ function postprocess(ast, options) {
* - do nothing if there's a semicolon on `toOverrideNode.end` (no need to fix)
*/
function overrideLocEnd(toBeOverriddenNode, toOverrideNode) {
if (options.originalText[locEnd(toOverrideNode)] === ";") {
if (text[locEnd(toOverrideNode)] === ";") {
return;
}
toBeOverriddenNode.range = [
@@ -291,8 +291,8 @@ const decoratorOrModifierRegExp = new RegExp(
["@", ...POSSIBLE_MODIFIERS].join("|")
);
function throwErrorForInvalidNodes(tsParseResult, options) {
if (!decoratorOrModifierRegExp.test(options.originalText)) {
function throwErrorForInvalidNodes(tsParseResult, text) {
if (!decoratorOrModifierRegExp.test(text)) {
return;
}
+3 -5
View File
@@ -37,7 +37,7 @@ function createParseError(error) {
});
}
function parse(text, options = {}) {
function parse(text) {
const textToParse = replaceHashbang(text);
const jsx = isProbablyJsx(text);
@@ -58,11 +58,9 @@ function parse(text, options = {}) {
throw createParseError(error);
}
options.originalText = text;
throwErrorForInvalidNodes(result, text);
throwErrorForInvalidNodes(result, options);
return postprocess(result.ast, options);
return postprocess(result.ast, { parser: "typescript", text });
}
/**
@@ -1,15 +1,24 @@
function wrapBabelExpression(node, options, type = "JsExpressionRoot") {
const { tokens, comments } = node;
delete node.tokens;
delete node.comments;
/**
* @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,
range: [0, options.originalText.length],
rootMarker: options.rootMarker,
node: expression,
range: [0, text.length],
rootMarker,
};
}
+3 -3
View File
@@ -20,7 +20,6 @@ import {
isCallExpression,
isMemberExpression,
isObjectProperty,
isEnabledHackPipeline,
isArrayOrTupleExpression,
isObjectOrRecordExpression,
} from "../utils/index.js";
@@ -37,7 +36,7 @@ function printBinaryishExpression(path, options, print) {
parent.type === "SwitchStatement" ||
parent.type === "DoWhileStatement");
const isHackPipeline =
isEnabledHackPipeline(options) && node.operator === "|>";
node.operator === "|>" && path.root.extra?.__isUsingHackPipeline;
const parts = printBinaryishExpressions(
path,
@@ -253,7 +252,8 @@ function printBinaryishExpressions(
if (shouldInline) {
right = [operator, " ", print("right"), rightSuffix];
} else {
const isHackPipeline = isEnabledHackPipeline(options) && operator === "|>";
const isHackPipeline =
operator === "|>" && path.root.extra?.__isUsingHackPipeline;
const rightContent = isHackPipeline
? path.call(
(left) =>
-5
View File
@@ -1184,10 +1184,6 @@ function isObjectProperty(node) {
);
}
function isEnabledHackPipeline(options) {
return Boolean(options.__isUsingHackPipeline);
}
/**
* This is used as a marker for dangling comments.
*/
@@ -1217,7 +1213,6 @@ export {
identity,
isBinaryish,
isCallLikeExpression,
isEnabledHackPipeline,
isLineComment,
isPrettierIgnoreComment,
isCallExpression,
+2 -3
View File
@@ -8,7 +8,7 @@ import wrapBabelExpression from "../language-js/parse/utils/wrap-babel-expressio
function createJsonParse(options = {}) {
const { allowComments = true } = options;
return function parse(text, options = {}) {
return function parse(text) {
let ast;
try {
ast = parseExpression(text, {
@@ -37,8 +37,7 @@ function createJsonParse(options = {}) {
assertJsonNode(ast);
options.originalText = text;
return wrapBabelExpression(ast, options, "JsonRoot");
return wrapBabelExpression(ast, { type: "JsonRoot", text });
};
}