Refactor angular control flow block (#15614)
This commit is contained in:
1 parent
cd7931f479
commit
22c423933c
9 files changed
+75
-41
No files matched your search
@@ -38,8 +38,8 @@ function clean(ast, newNode) {
|
|||||||
delete newNode.value;
|
delete newNode.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ast.type === "block") {
|
if (ast.type === "angularControlFlowBlock" && newNode.parameters?.children) {
|
||||||
for (const parameter of newNode.parameters) {
|
for (const parameter of newNode.parameters.children) {
|
||||||
if (embeddedAngularControlFlowBlocks.has(ast.name)) {
|
if (embeddedAngularControlFlowBlocks.has(ast.name)) {
|
||||||
delete parameter.expression;
|
delete parameter.expression;
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -145,12 +145,8 @@ function embed(path, options) {
|
|||||||
case "front-matter":
|
case "front-matter":
|
||||||
return (textToDoc) => printFrontMatter(node, textToDoc);
|
return (textToDoc) => printFrontMatter(node, textToDoc);
|
||||||
|
|
||||||
// TODO: This should work on `blockParameter`, but currently we don't have a parser for it.
|
case "angularControlFlowBlockParameters":
|
||||||
case "block":
|
if (!embeddedAngularControlFlowBlocks.has(path.parent.name)) {
|
||||||
if (
|
|
||||||
!embeddedAngularControlFlowBlocks.has(node.name) ||
|
|
||||||
node.parameters.length === 0
|
|
||||||
) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,16 +1,25 @@
|
|||||||
import { formatAttributeValue, shouldHugJsExpression } from "./utils.js";
|
import { formatAttributeValue, shouldHugJsExpression } from "./utils.js";
|
||||||
|
|
||||||
async function printAngularControlFlowBlockParameters(
|
function printAngularControlFlowBlockParameters(
|
||||||
textToDoc,
|
textToDoc,
|
||||||
print,
|
print,
|
||||||
path,
|
path,
|
||||||
/* options,*/
|
options,
|
||||||
) {
|
) {
|
||||||
const { node } = path;
|
const { node } = path;
|
||||||
const code = node.parameters.map(({ expression }) => expression).join("; ");
|
|
||||||
|
|
||||||
const doc = await formatAttributeValue(
|
const content = options.originalText.slice(
|
||||||
code,
|
node.sourceSpan.start.offset,
|
||||||
|
node.sourceSpan.end.offset,
|
||||||
|
);
|
||||||
|
const isEmpty = /^\s*$/.test(content);
|
||||||
|
|
||||||
|
if (isEmpty) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
return formatAttributeValue(
|
||||||
|
content,
|
||||||
textToDoc,
|
textToDoc,
|
||||||
{
|
{
|
||||||
parser: "__ng_directive",
|
parser: "__ng_directive",
|
||||||
@@ -19,9 +28,6 @@ async function printAngularControlFlowBlockParameters(
|
|||||||
},
|
},
|
||||||
shouldHugJsExpression,
|
shouldHugJsExpression,
|
||||||
);
|
);
|
||||||
|
|
||||||
// We have to put the doc on the node, since `node.parameters` is not a valid "node"
|
|
||||||
node.__embed_parameters_doc = doc;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default printAngularControlFlowBlockParameters;
|
export default printAngularControlFlowBlockParameters;
|
||||||
@@ -11,6 +11,7 @@ import {
|
|||||||
import parseFrontMatter from "../utils/front-matter/parse.js";
|
import parseFrontMatter from "../utils/front-matter/parse.js";
|
||||||
import inferParser from "../utils/infer-parser.js";
|
import inferParser from "../utils/infer-parser.js";
|
||||||
import createError from "../common/parser-create-error.js";
|
import createError from "../common/parser-create-error.js";
|
||||||
|
import isNonEmptyArray from "../utils/is-non-empty-array.js";
|
||||||
import HTML_TAGS from "./utils/html-tag-names.evaluate.js";
|
import HTML_TAGS from "./utils/html-tag-names.evaluate.js";
|
||||||
import HTML_ELEMENT_ATTRIBUTES from "./utils/html-elements-attributes.evaluate.js";
|
import HTML_ELEMENT_ATTRIBUTES from "./utils/html-elements-attributes.evaluate.js";
|
||||||
import isUnknownNamespace from "./utils/is-unknown-namespace.js";
|
import isUnknownNamespace from "./utils/is-unknown-namespace.js";
|
||||||
@@ -39,10 +40,31 @@ import { locStart, locEnd } from "./loc.js";
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
// `@else if`
|
// `@else if`
|
||||||
function normalizeAngularControlFlowBlockName(node) {
|
function normalizeAngularControlFlowBlock(node) {
|
||||||
if (node.type === "block") {
|
if (node.type !== "block") {
|
||||||
node.name = node.name.toLowerCase().replaceAll(/\s+/g, " ").trim();
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
node.name = node.name.toLowerCase().replaceAll(/\s+/g, " ").trim();
|
||||||
|
node.type = "angularControlFlowBlock";
|
||||||
|
|
||||||
|
if (!isNonEmptyArray(node.parameters)) {
|
||||||
|
delete node.parameters;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const parameter of node.parameters) {
|
||||||
|
parameter.type = "angularControlFlowBlockParameter";
|
||||||
|
}
|
||||||
|
|
||||||
|
node.parameters = {
|
||||||
|
type: "angularControlFlowBlockParameters",
|
||||||
|
children: node.parameters,
|
||||||
|
sourceSpan: new ParseSourceSpan(
|
||||||
|
node.parameters[0].sourceSpan.start,
|
||||||
|
node.parameters.at(-1).sourceSpan.end,
|
||||||
|
),
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -244,7 +266,6 @@ function ngHtmlParser(input, parseOptions, options) {
|
|||||||
addTagDefinition(node);
|
addTagDefinition(node);
|
||||||
normalizeName(node);
|
normalizeName(node);
|
||||||
fixSourceSpan(node);
|
fixSourceSpan(node);
|
||||||
normalizeAngularControlFlowBlockName(node);
|
|
||||||
}
|
}
|
||||||
})(),
|
})(),
|
||||||
rootNodes,
|
rootNodes,
|
||||||
@@ -351,6 +372,8 @@ function parse(
|
|||||||
node.parent.replaceChild(node, ieConditionalComment);
|
node.parent.replaceChild(node, ieConditionalComment);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
normalizeAngularControlFlowBlock(node);
|
||||||
});
|
});
|
||||||
|
|
||||||
return ast;
|
return ast;
|
||||||
|
|||||||
+12
-12
@@ -29,15 +29,8 @@ function printAngularControlFlowBlock(path, options, print) {
|
|||||||
|
|
||||||
docs.push("@", node.name);
|
docs.push("@", node.name);
|
||||||
|
|
||||||
if (node.parameters.length > 0) {
|
if (node.parameters) {
|
||||||
const parametersDoc = group(
|
docs.push(" (", group(print("parameters")), ")");
|
||||||
node.__embed_parameters_doc ?? [
|
|
||||||
indent([softline, join([";", line], path.map(print, "parameters"))]),
|
|
||||||
softline,
|
|
||||||
],
|
|
||||||
);
|
|
||||||
|
|
||||||
docs.push(" (", parametersDoc, ")");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
docs.push(" {");
|
docs.push(" {");
|
||||||
@@ -67,7 +60,7 @@ function printAngularControlFlowBlock(path, options, print) {
|
|||||||
function shouldCloseBlock(node, names) {
|
function shouldCloseBlock(node, names) {
|
||||||
return !(
|
return !(
|
||||||
names.size > 0 &&
|
names.size > 0 &&
|
||||||
node.next?.type === "block" &&
|
node.next?.type === "angularControlFlowBlock" &&
|
||||||
names.has(node.next.name)
|
names.has(node.next.name)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -78,10 +71,17 @@ function findPreviousBlock(path) {
|
|||||||
for (let i = index - 1; i >= 0; i--) {
|
for (let i = index - 1; i >= 0; i--) {
|
||||||
const sibling = siblings[i];
|
const sibling = siblings[i];
|
||||||
|
|
||||||
if (sibling.type === "block") {
|
if (sibling.type === "angularControlFlowBlock") {
|
||||||
return sibling;
|
return sibling;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export { printAngularControlFlowBlock };
|
function printAngularControlFlowBlockParameters(path, options, print) {
|
||||||
|
return [
|
||||||
|
indent([softline, join([";", line], path.map(print, "children"))]),
|
||||||
|
softline,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
export { printAngularControlFlowBlock, printAngularControlFlowBlockParameters };
|
||||||
@@ -128,7 +128,7 @@ function needsToBorrowPrevClosingTagEndMarker(node) {
|
|||||||
return (
|
return (
|
||||||
node.prev &&
|
node.prev &&
|
||||||
node.prev.type !== "docType" &&
|
node.prev.type !== "docType" &&
|
||||||
node.type !== "block" &&
|
node.type !== "angularControlFlowBlock" &&
|
||||||
!isTextLikeNode(node.prev) &&
|
!isTextLikeNode(node.prev) &&
|
||||||
node.isLeadingSpaceSensitive &&
|
node.isLeadingSpaceSensitive &&
|
||||||
!node.hasLeadingSpaces
|
!node.hasLeadingSpaces
|
||||||
|
|||||||
@@ -21,7 +21,10 @@ import {
|
|||||||
} from "./print/tag.js";
|
} from "./print/tag.js";
|
||||||
import { printElement } from "./print/element.js";
|
import { printElement } from "./print/element.js";
|
||||||
import { printChildren } from "./print/children.js";
|
import { printChildren } from "./print/children.js";
|
||||||
import { printAngularControlFlowBlock } from "./print/angular-control-flow-block.js";
|
import {
|
||||||
|
printAngularControlFlowBlock,
|
||||||
|
printAngularControlFlowBlockParameters,
|
||||||
|
} from "./print/angular-control-flow-block.js";
|
||||||
import getVisitorKeys from "./get-visitor-keys.js";
|
import getVisitorKeys from "./get-visitor-keys.js";
|
||||||
|
|
||||||
function genericPrint(path, options, print) {
|
function genericPrint(path, options, print) {
|
||||||
@@ -39,9 +42,11 @@ function genericPrint(path, options, print) {
|
|||||||
case "ieConditionalComment":
|
case "ieConditionalComment":
|
||||||
return printElement(path, options, print);
|
return printElement(path, options, print);
|
||||||
|
|
||||||
case "block":
|
case "angularControlFlowBlock":
|
||||||
return printAngularControlFlowBlock(path, options, print);
|
return printAngularControlFlowBlock(path, options, print);
|
||||||
case "blockParameter":
|
case "angularControlFlowBlockParameters":
|
||||||
|
return printAngularControlFlowBlockParameters(path, options, print);
|
||||||
|
case "angularControlFlowBlockParameter":
|
||||||
return htmlWhitespaceUtils.trim(node.expression);
|
return htmlWhitespaceUtils.trim(node.expression);
|
||||||
|
|
||||||
case "ieConditionalStartComment":
|
case "ieConditionalStartComment":
|
||||||
|
|||||||
@@ -11,8 +11,9 @@ const visitorKeys = {
|
|||||||
comment: [],
|
comment: [],
|
||||||
attribute: [],
|
attribute: [],
|
||||||
cdata: [],
|
cdata: [],
|
||||||
block: ["children", "parameters"],
|
angularControlFlowBlock: ["children", "parameters"],
|
||||||
blockParameter: [],
|
angularControlFlowBlockParameters: ["children"],
|
||||||
|
angularControlFlowBlockParameter: [],
|
||||||
};
|
};
|
||||||
|
|
||||||
export default visitorKeys;
|
export default visitorKeys;
|
||||||
@@ -1389,12 +1389,15 @@ exports[`visitor keys graphql 1`] = `
|
|||||||
|
|
||||||
exports[`visitor keys html 1`] = `
|
exports[`visitor keys html 1`] = `
|
||||||
{
|
{
|
||||||
"attribute": [],
|
"angularControlFlowBlock": [
|
||||||
"block": [
|
|
||||||
"children",
|
"children",
|
||||||
"parameters",
|
"parameters",
|
||||||
],
|
],
|
||||||
"blockParameter": [],
|
"angularControlFlowBlockParameter": [],
|
||||||
|
"angularControlFlowBlockParameters": [
|
||||||
|
"children",
|
||||||
|
],
|
||||||
|
"attribute": [],
|
||||||
"cdata": [],
|
"cdata": [],
|
||||||
"comment": [],
|
"comment": [],
|
||||||
"docType": [],
|
"docType": [],
|
||||||
|
|||||||
Reference in new issue
Block a user