Simplify interpolation handling in ngNonBindable (#20115)

This commit is contained in:
fisker Cheung authored and GitHub committed 2026-09-20 21:52:10 +08:00
1 parent 0292df6bf3
commit 51ed2cbbf3
9 files changed
+68 -41

No files matched your search

+1 -1
View File
@@ -1,4 +1,4 @@
#### Preserve interpolations inside elements with `ngNonBindable` directive (#20044 by @splincode) #### Preserve interpolations inside elements with `ngNonBindable` directive (#20044 by @splincode, #20115 by @fisker)
<!-- prettier-ignore --> <!-- prettier-ignore -->
```html ```html
+1 -5
View File
@@ -23,7 +23,6 @@ import {
isScriptLikeTag, isScriptLikeTag,
isVueNonHtmlBlock, isVueNonHtmlBlock,
} from "./utilities/index.js"; } from "./utilities/index.js";
import { isInNgNonBindable } from "./utilities/is-in-ng-non-bindable.js";
import isVueSfcWithTypescriptScript from "./utilities/is-vue-sfc-with-typescript-script.js"; import isVueSfcWithTypescriptScript from "./utilities/is-vue-sfc-with-typescript-script.js";
const embeddedAngularControlFlowBlocks = new Set([ const embeddedAngularControlFlowBlocks = new Set([
@@ -110,10 +109,7 @@ function embed(path, options) {
]; ];
}; };
} }
} else if ( } else if (node.parent.kind === "interpolation") {
node.parent.kind === "interpolation" &&
!isInNgNonBindable(path, options)
) {
return async (textToDoc) => { return async (textToDoc) => {
const textToDocOptions = { const textToDocOptions = {
__isInHtmlInterpolation: true, // to avoid unexpected `}}` __isInHtmlInterpolation: true, // to avoid unexpected `}}`
+10 -1
View File
@@ -18,6 +18,7 @@ import {
CSS_WHITE_SPACE_TAGS, CSS_WHITE_SPACE_TAGS,
} from "../constants.evaluate.js"; } from "../constants.evaluate.js";
import isUnknownNamespace from "./is-unknown-namespace.js"; import isUnknownNamespace from "./is-unknown-namespace.js";
import { hasOrInNgNonBindable } from "./ng-non-bindable.js";
const htmlTrimLeadingBlankLines = (string) => const htmlTrimLeadingBlankLines = (string) =>
string.replaceAll(/^[\t\f\r ]*\n/g, ""); string.replaceAll(/^[\t\f\r ]*\n/g, "");
@@ -121,7 +122,15 @@ function isScriptLikeTag(node, options) {
} }
function canHaveInterpolation(node, options) { function canHaveInterpolation(node, options) {
return node.children && !isScriptLikeTag(node, options); if (!node.children || isScriptLikeTag(node, options)) {
return false;
}
if (hasOrInNgNonBindable(node, options)) {
return false;
}
return true;
} }
function isWhitespaceSensitiveNode(node, options) { function isWhitespaceSensitiveNode(node, options) {
@@ -1,29 +0,0 @@
import { getOrInsertComputed } from "../../utilities/get-or-insert.js";
const angularNonBindableCache = new WeakMap();
function hasOrInNgNonBindable(node) {
return getOrInsertComputed(angularNonBindableCache, node, (node) => {
if (
node.kind === "element" &&
Object.hasOwn(node.attrMap, "ngNonBindable")
) {
return true;
}
if (!node.parent) {
return false;
}
return hasOrInNgNonBindable(node.parent);
});
}
function isInNgNonBindable(path, options) {
if (options.parser !== "angular") {
return false;
}
return hasOrInNgNonBindable(path.node.parent);
}
export { isInNgNonBindable };
@@ -0,0 +1,20 @@
import { getOrInsertComputed } from "../../utilities/get-or-insert.js";
const isElementWithNgNonBindable = (node) =>
node?.kind === "element" && Object.hasOwn(node.attrMap, "ngNonBindable");
const angularNonBindableCache = new WeakMap();
const hasOrInNgNonBindableInternal = (node) =>
Boolean(node) &&
getOrInsertComputed(
angularNonBindableCache,
node,
(node) =>
isElementWithNgNonBindable(node) ||
hasOrInNgNonBindableInternal(node.parent),
);
const hasOrInNgNonBindable = (node, options) =>
options.parser === "angular" && hasOrInNgNonBindableInternal(node);
export { hasOrInNgNonBindable };
@@ -78,24 +78,38 @@ parsers: ["angular"]
<pre ngNonBindable>{{foo +bar}}</pre> <pre ngNonBindable>{{foo +bar}}</pre>
<div ngNonBindable> {{ foo + bar }} </div>
<div ngNonBindable> [[ foo + bar ]] </div>
<span ngNonBindable> {{ foo + bar }} </span>
<span ngNonBindable> [[ foo + bar ]] </span>
<pre ngNonBindable> {{ foo + bar }} </pre>
<pre ngNonBindable> [[ foo + bar ]] </pre>
=====================================output===================================== =====================================output=====================================
<span ngNonBindable>{{foo +bar}}</span> <span ngNonBindable>{{foo +bar}}</span>
<div ngNonBindable> <div ngNonBindable>
{{foo +bar}} {{foo +bar}}
<span>{{baz +qux}}</span> <span>{{baz +qux}}</span>
</div> </div>
<span>{{ foo + bar }}</span> <span>{{ foo + bar }}</span>
<span ngNonBindable> <span ngNonBindable>
@if (A) { @if (A) {
<span>{{foo +bar}}</span> <span>{{foo +bar}}</span>
} }
</span> </span>
<pre ngNonBindable>{{foo +bar}}</pre> <pre ngNonBindable>{{foo +bar}}</pre>
<div ngNonBindable>{{ foo + bar }}</div>
<div ngNonBindable>[[ foo + bar ]]</div>
<span ngNonBindable> {{ foo + bar }} </span>
<span ngNonBindable> [[ foo + bar ]] </span>
<pre ngNonBindable> {{ foo + bar }} </pre>
<pre ngNonBindable> [[ foo + bar ]] </pre>
================================================================================ ================================================================================
`; `;
@@ -7,9 +7,15 @@ parsers: ["angular"]
printWidth: 80 (default) | printWidth: 80 (default) |
=====================================input====================================== =====================================input======================================
<span ngNonBindable>{{foo +bar}}</span> <span ngNonBindable>{{foo +bar}}</span>
<pre ngNonBindable>{{foo +bar}}</pre>
<span ngNonBindable>{{foo
.bar(
)}}</span>
=====================================output===================================== =====================================output=====================================
<span ngNonBindable>{{foo +bar}}</span> <span ngNonBindable>{{foo +bar}}</span>
<pre ngNonBindable>{{foo +bar}}</pre>
<span ngNonBindable>{{foo .bar( )}}</span>
================================================================================ ================================================================================
`; `;
@@ -1 +1,5 @@
<span ngNonBindable>{{foo +bar}}</span> <span ngNonBindable>{{foo +bar}}</span>
<pre ngNonBindable>{{foo +bar}}</pre>
<span ngNonBindable>{{foo
.bar(
)}}</span>
@@ -14,3 +14,10 @@
</span> </span>
<pre ngNonBindable>{{foo +bar}}</pre> <pre ngNonBindable>{{foo +bar}}</pre>
<div ngNonBindable> {{ foo + bar }} </div>
<div ngNonBindable> [[ foo + bar ]] </div>
<span ngNonBindable> {{ foo + bar }} </span>
<span ngNonBindable> [[ foo + bar ]] </span>
<pre ngNonBindable> {{ foo + bar }} </pre>
<pre ngNonBindable> [[ foo + bar ]] </pre>