Commit graph

1936 commits

Author SHA1 Message Date
Matthieu Riegler
c73520bb74 refactor(compiler): remove empty empty string suffix from interpolation instructions (#60066)
With this change, interpolations that don't have a suffix will miss the last argument which was previously an empty string.

PR Close #60066
2025-03-10 16:31:34 -07:00
Miles Malerba
00938699b8 refactor(compiler-cli): handle parentheses in the template pipeline (#60169)
Now that the expression AST contains parenthesized expressions, this
refactors the template pipeline to strip out the ones we don't need.

PR Close #60169
2025-03-10 09:53:10 -07:00
Miles Malerba
d4cfeb0a86 refactor(compiler): add parenthesized expressions to experssion ast (#60169)
Following up on #60127 which added the concept of a parenthesized
expression to the output AST, this does the same for the expression AST.

PR Close #60169
2025-03-10 09:53:10 -07:00
Kristiyan Kostadinov
9aef786b4a refactor(compiler): allow binder to apply to more than one set of nodes (#60191)
Currently `R3TargetBinder.bind` gets a set of data back from `DirectiveBinder.apply` and `TemplateBinder.applyWithScope`. This will be annoying  if we have multiple sources of data, because we'd have to do merge them at the end.

These changes switch to constructing the various data structures ahead of time and passing them into the binders to populate them instead.

I also extracted some of the less trivial types into type aliases so we don't have to repeat them.

PR Close #60191
2025-03-06 10:39:31 -08:00
Paul Gschwendtner
1f1039475c feat(bazel): support shared chunks in ng_package (#60241)
Historically we've had to be VERY cautious about the way we import
things between entry-points. That is because the `ng_package` rule
bundling is subject to silently introducing code duplication, breaking
singletons etc. We've had this surface a couple of times already, and
dev-infra tried to help detect such cases by adding safety analysis into
`ng_package`.

Long-term we want to get to an approach where it's easy to simply share
code between chunks. Precisely, with the upcoming `rules_js` migration,
this will be necessary as we will have different import "guidelines"
that would currently, before this commit, result in code duplication, or
trigger our "safety check/lint".

This commit prepares `ng_package` to support relative imports between
entry-points, so that we only need the safety check for cross-package
imports/exports. The result is that `ng_package`/APF is now smartly able
to generate shared chunks for things that are needed between multiple
entry-points. Yay!

Note that those shared chunks still remain private, and are guarded by
our `package.json` "exports"; so no new public API surface is
exposed.

PR Close #60241
2025-03-06 10:29:05 -08:00
Miles Malerba
ffb19e64f1 fix(compiler-cli): preserve required parens for nullish coalescing (#60060)
Fixes outputted nullish coalescing expressions to not drop parentheses
when it would change the meaning of the expression.

PR Close #60060
2025-03-04 17:44:54 +00:00
Miles Malerba
2a23209b08 refactor(compiler): stop down-leveling nullish coalescing (#60060)
Stop down-leveling the nullish coalescing (`??`) operator in templates.
Depending on the ES version typescript may still down-level it.

PR Close #60060
2025-03-04 17:44:53 +00:00
Miles Malerba
51b8ff23ce feat(compiler): support tagged template literals in expressions (#59947)
Adds support for using tagged template literals in Angular templates.

Ex:
```
@Component({
  template: '{{ greet`Hello, ${name()}` }}'
})
export class MyComp {
  name = input();

  greet(strings: TemplateStringsArray, name: string) {
    return strings[0] + name + strings[1] + '!';
  }
}
```

PR Close #59947
2025-02-28 19:53:33 +00:00
Miles Malerba
b70ad3c4e6 fix(compiler): proper handling of typeof, void in RecursiveAstVisitor (#60101)
Handle typeof and void expressions the same way as other unary operator
expressions.

PR Close #60101
2025-02-28 16:28:10 +00:00
Miles Malerba
7c9b4892e9 fix(compiler-cli): preserve required parens in exponentiation expressions (#60101)
Parentheses are required around a unary operator used in the base of an
exponentiation expression. For example: `(-1) ** 3`

PR Close #60101
2025-02-28 16:28:10 +00:00
Miles Malerba
4fe489f1b4 fix(compiler): exponentiation should be right-to-left associative (#60101)
For example, `a ** b ** c` should be equivalent to `a ** (b ** c)`,
not `(a ** b) ** c`

PR Close #60101
2025-02-28 16:28:10 +00:00
Miles Malerba
a75566a9be refactor(compiler): rework how parens are emitted (#60127)
Instead of using a property on BinaryOperatorExpr / UnaryOperatorExpr,
introduce a ParenthesizedExpr which can be used to parenthesize any
expression.

PR Close #60127
2025-02-27 17:41:05 +00:00
Miles Malerba
f2d5cf7edd feat(compiler): support exponentiation operator in templates (#59894)
Adds support for the exponentiation (`**`) operator in templates

Ex:
```
@Component {
  template: '{{2 ** 3}}'
}
```

PR Close #59894
2025-02-25 11:03:37 -05:00
Miles Malerba
0361c2d81f feat(compiler): support void operator in templates (#59894)
Add support for the `void` operator in templates and host bindings.

This is useful when binding a listener that may return `false` and
unintentionally prevent the default event behavior.

Ex:
```
@Directive({
  host: { '(mousedown)': 'void handleMousedown()' }
})
```

BREAKING CHANGE: `void` in an expression now refers to the operator

Previously an expression in the template like `{{void}}` referred to a
property on the component class. After this change it now refers to the
`void` operator, which would make the above example invalid. If you have
existing expressions that need to refer to a property named `void`,
change the expression to use `this.void` instead: `{{this.void}}`.

PR Close #59894
2025-02-25 11:03:37 -05:00
Kristiyan Kostadinov
db530856a8 refactor(compiler): remove input transforms feature (#59980)
An earlier refactor made the `InputTransformsFeature` a no-op so these changes remove the code that was generating it.

PR Close #59980
2025-02-18 19:27:59 +00:00
Kristiyan Kostadinov
9e847fc60d fix(compiler): handle tracking expressions requiring temporary variables (#58520)
Currently when we generate the tracking expression for a `@for` block, we process its expression in the context of the creation block. This is incorrect, because the expression may require ops of its own for cases like nullish coalescing or safe reads. The result is that while we do generate the correct variable, they're added to the creation block rather than the tracking function which causes an error at runtime.

These changes address the issue by keeping track of a separate set of ops for the `track` expression that are prepended to the generated function, similarly to how we handle event listeners.

Fixes #56256.

PR Close #58520
2025-02-12 09:56:08 -08:00
Kristiyan Kostadinov
e47c1e5abe refactor(compiler): pass more information to HMR replacement function (#59854)
Adjusts the code we generate for HMR so that it passes in the HMR ID and `import.meta` to the `replaceMetadata` call. This is necessary so we can do better logging of errors.

PR Close #59854
2025-02-12 09:05:30 -08:00
Kristiyan Kostadinov
bae94b82fd fix(compiler-cli): handle const enums used inside HMR data (#59815)
When we generate an HMR replacement function, we determine which locals from the file are used and we pass them by reference. This works fine in most cases, but breaks down for const enums which don't have a runtime representation.

These changes work around the issue by passing in all the values as an object literal.

Fixes #59800.

PR Close #59815
2025-02-03 13:58:31 -08:00
Kristiyan Kostadinov
fe8a68329b feat(compiler): support untagged template literals in expressions (#59230)
Updates the compiler to support untagged template literals inside of the expression syntax (e.g. ``hello ${world}``).

PR Close #59230
2025-01-21 12:04:53 -08:00
Kristiyan Kostadinov
4016aa3229 refactor(compiler): clean up tagged templates in output AST (#59230)
Makes the following cleanups in the output AST:
* The `TemplateLiteral` and `TemplateLiteralElement` nodes have been renamed to `TemplateLiteralExpr` and `TemplateLiteralElementExpr` respectively for consistency and to avoid overlaps with the expression AST nodes.
* The `TemplateLiteralExpr` and `TemplateLiteralElementExpr` have been refactored to be `Expression`s for correctness. This involves updating some existing code.
* The `TaggedTemplateExpr` has been renamed to `TaggedTemplateLiteralExpr` for consistency.

PR Close #59230
2025-01-21 12:04:52 -08:00
Kristiyan Kostadinov
eb7e765e2f refactor(compiler): produce AST for template literals (#59230)
Updates the compiler to parse the template literal tokens into the new `TemplateLiteral` and `TemplateLiteralElement` AST nodes.

PR Close #59230
2025-01-21 12:04:52 -08:00
Kristiyan Kostadinov
353005b97f refactor(compiler): tokenize template literals (#59230)
Reworks the lexer to produce tokens for template literal expressions.

PR Close #59230
2025-01-21 12:04:52 -08:00
Kristiyan Kostadinov
98998bbd42 refactor(compiler): allow lexer to produce multiple tokens (#59230)
Reworks the lexer's scanner to produce more than one token at a time. This can be useful for the cases where one token means the end of another one.

Also cleans up the scanner by making all non-essential methods private and using strict equality everywhere.

PR Close #59230
2025-01-21 12:04:52 -08:00
Kristiyan Kostadinov
98f820737b fix(compiler): handle :host-context with comma-separated child selector (#59276)
Both `:host` and `:host-context` work by looking for a specific character sequence that is terminated by `,` or `{` and replacing selectors inside of it with scoped versions. This is implemented as a regex which isn't aware of things like nested selectors. Normally this is fine for `:host`, because each `:host` produces one scoped selector which doesn't affect any child selectors, however it breaks down with `:host-context` which replaces each instance with two selectors. For example, if we have a selector in the form of `:host-context(.foo) a:not(.a, .b)`, the compiler ends up determining that `.a,` is the end selector and produces `.foo[a-host] a[contenta]:not(.a, .foo [a-host] a[contenta]:not(.a, .b) {}`.

These changes resolve the issue by splitting the CSS alogn top-level commas, processing the `:host-context` in them individually, and stiching the CSS back together.

PR Close #59276
2025-01-21 09:11:16 -08:00
Alan Agius
30c4404752 fix(compiler): update @ng/component URL to be relative (#59620)
This change is required to resolve https://github.com/angular/angular-cli/pull/29248 and works in conjunction with https://github.com/angular/angular-cli/pull/29386. It ensures that HMR (Hot Module Replacement) functions correctly with `base href`, proxies, and other advanced development setups.

PR Close #59620
2025-01-20 09:10:13 +01:00
hawkgs
b9155b5121 docs: set syntax highlighting to the remaining Markdown code examples blocks (#59088)
There are some code blocks that slipped through the initial Regex-es.

Related to #59026

PR Close #59088
2025-01-14 15:14:02 -05:00
Matthieu Riegler
52334e02e7 refactor(compiler): remove unused AstVisitors (#59297)
Those 2 clases weren't imported anywhere.

PR Close #59297
2025-01-14 11:08:25 -05:00
Kristiyan Kostadinov
915593ad25 refactor(compiler): fix typo in method name (#59479)
Fixes a typo in the `AstVisitor.visitTypeofExpresion` method's name.

PR Close #59479
2025-01-13 10:57:51 -05:00
Charles Lyding
a4164141a3 fix(compiler): use chunk origin in template HMR request URL (#59459)
The URL that is dynamically imported to fetch a potential component update
for HMR is now based on the value of `import.meta.url`. This ensures that
the request is sent to the same location that was used to retrieve the
application code. For some development server setups the HTML base HREF
may not be the location of the Angular development server. By using the
application code location which was generated by the development server,
HMR requests can continue to work as expected in these scenarios. In
most common cases, this change will not have any effect as the HTML base
HREF aligns with the location of the application code files.

PR Close #59459
2025-01-09 18:21:48 -05:00
Kristiyan Kostadinov
ceadd28ea1 fix(compiler): allow $any in two-way bindings (#59362)
Some time ago we narrowed down the expressions we support in two-way bindings, because in most cases any apart from property reads doesn't make sense. This ended up preventing users from using `$any` in the binding since it's considered a function call.

These changes update the validation logic to allow `$any`.

Fixes #51165.

PR Close #59362
2025-01-06 17:25:25 +00:00
Sandor Drieënhuizen
e5866eed2e refactor(compiler): incorrect spelling in for loop parse error message (#59289)
'parameter' was spelled as 'paramater'.

Fix spelling error in Update r3_control_flow.ts

'parameter' was spelled as 'paramater'.

Fix spelling error in r3_template_transform_spec.ts

'parameter' was spelled as 'paramater'.

PR Close #59289
2025-01-06 16:18:15 +00:00
Kristiyan Kostadinov
4559e125f0 refactor(compiler): generate debug location instruction (#58982)
Adds the logic that will generate the `ɵɵattachSourceLocations` instruction.

Fixes #42530.

PR Close #58982
2024-12-05 16:09:55 -08:00
Kristiyan Kostadinov
90896b858b refactor(core): add runtime logic for attaching source locations (#58982)
Adds the implementation of the `ɵɵattachSourceLocations` instruction that will add the `data-ng-source-location` attribute to nodes to indicate where they were defined.

PR Close #58982
2024-12-05 16:09:55 -08:00
Kristiyan Kostadinov
c9b3bd8409 refactor(compiler): capture template path (#58982)
Captures the template path in the component's metadata so it can be used later on.

PR Close #58982
2024-12-05 16:09:55 -08:00
Alex Rickabaugh
8b02d11fbb refactor(compiler): remove circular dep in the output ast (#59083)
Use `import type` to break a phantom circular import in the output AST in
the compiler.

PR Close #59083
2024-12-05 16:01:14 -08:00
hawkgs
0513fbc9fc docs: set syntax highlighting of code examples MD code blocks (#59026)
Set the syntax highlighting based on the code examples' language.

PR Close #59026
2024-12-04 17:30:28 +01:00
Kristiyan Kostadinov
21b993c8df refactor(compiler): remove allowInvalidAssignmentEvents flag (#58988)
Deletes the `allowInvalidAssignmentEvents` which was added to facilitate a migration away from invalid two-way bindings. Since the migration doesn't exist anymore, we don't need the flag either.

PR Close #58988
2024-12-02 08:55:42 +01:00
Kristiyan Kostadinov
f280467398 fix(compiler-cli): account for multiple generated namespace imports in HMR (#58924)
The current HMR compiler assumes that there will only be one namespace import in the generated code (`@angular/core`). This is incorrect, because the compiler may need to generate additional imports in some cases (e.g. importing directives through a module). These changes adjust the compiler to capture all the namespaces in an array and pass them along.

Fixes #58915.

PR Close #58924
2024-11-28 10:00:56 +01:00
Jessica Janiuk
30891d8dec refactor(core): Consolidates shouldTrigger* methods down to one (#58833)
This cleans up the triggering code base and consolidates it down to one
function that outlines the logic. This also resolves the `hydrate when`
behavior issue.

fixes: #58709

PR Close #58833
2024-11-27 17:00:06 +01:00
Jessica Janiuk
38fe180d34 refactor(compiler): Adds ingest and flags for defer details (#58833)
This adds TDeferDetailsFlags to indicate the presence of hydration triggers, and any future flags we add to defer.

PR Close #58833
2024-11-27 17:00:05 +01:00
Utku Gultopu
0f1c71869e docs: capitalize webpack with a lowercase W (#56812)
PR Close #56812
2024-11-26 22:24:11 +00:00
Kristiyan Kostadinov
4e9244990a fix(compiler-cli): more accurate diagnostics for host binding parser errors (#58870)
Currently host bindings are in a bit of a weird state, because their source spans all point to the root object literal, rather than the individual expression. This is tricky to handle at the moment, because the object is being passed around as a `Record<string, string>` since the compiler needs to support both JIT and non-JIT environments, and because the AOT compiler evaluates the entire literal rather than doing it expression-by-expression. As a result, when we report errors in one of the host bindings, we end up highlighting the entire expression which can be very noisy in an IDE.

These changes aim to report a more accurate error for the most common case where the `host` object is initialized to a `string -> string` object literal by matching the failing expression to one of the property initializers. Note that this isn't 100% reliable, because we can't map cases like `host: SOME_CONST`, but it's still better than the current setup.

PR Close #58870
2024-11-25 15:25:48 +00:00
Georgy Serga
549a00d18b fix(compiler): fix multiline selectors (#58681)
multiline selectors where not correctly recognized by the regexp, fix it to fetch newlines as well

Fixes #58399

PR Close #58681
2024-11-15 11:30:27 +01:00
Georgy Serga
58a84b2f79 fix(compiler): resolve :host:host-context(.foo) (#58681)
fix results which had to parse several `-shadowcsshost-no-combinator` occurrences in a single selector

PR Close #58681
2024-11-15 11:30:27 +01:00
Georgy Serga
9587437e60 fix(compiler): transform chained pseudo-selectors (#58681)
fix transformation logic for `:where` and `:is` pseudo-selectors
when these selectors were used in a chain. results were often broken,
the last letter of the selector was incorrectly trimmed.
see tests for examples

Fixes #58226

PR Close #58681
2024-11-15 11:30:27 +01:00
Georgy Serga
70e800ecec fix(compiler): fix :host parsing in pseudo-selectors (#58681)
fix several use-cases where `:host` was used in or around pseudo-selectors
- `:host` followed by a comma inside pseudo-selectors
- `:host` outside of pseudo-selectors when another `:host` is present within
see tests for examples

PR Close #58681
2024-11-15 11:30:27 +01:00
Kristiyan Kostadinov
c421ffdbfb fix(compiler): control flow nodes with root at the end projected incorrectly (#58607)
Fixes an edge case where a control flow node that has non-projectable nodes followed by an element node at the end would cause the entire control flow node to be project. For example if we have a projection target of `Main: <ng-content/> Slot: <ng-content select="[foo]"/>`, inserting a node of `@if (true) {Hello <span foo>world</span>}` would project the entire `Hello world` into the `[foo]` slot.

In the process of working on the issue, I also found that `@let` declarations at the root of the control flow node would prevent content projection as well.

PR Close #58607
2024-11-12 18:05:00 +00:00
Kristiyan Kostadinov
7d0ba0cac8 refactor(compiler): trigger hmr load on initialization (#58465)
Adjusts the HMR initialization to avoid the edge case where a developer makes change to a non-rendered component that exists in a lazy loaded chunk that has not been loaded yet. The changes include:
* Moving the `import` statement out into a separate function.
* Adding a null check for `d.default` before calling `replaceMEtadata`.
* Triggering the `import` callback eagerly on initialization.

Example of the new generated code:

```js
(() => {
  function Cmp_HmrLoad(t) {
    import(
      /* @vite-ignore */ "/@ng/component?c=test.ts%40Cmp&t=" + encodeURIComponent(t)
    ).then((m) => m.default && i0.ɵɵreplaceMetadata(Cmp, m.default, [/* Dependencies go here */]));
  }
  (typeof ngDevMode === "undefined" || ngDevMode) && Cmp_HmrLoad(Date.now());
  (typeof ngDevMode === "undefined" || ngDevMode) &&
    import.meta.hot &&
    import.meta.hot.on("angular:component-update", (d) => {
      if (d.id === "test.ts%40Cmp") {
        Cmp_HmrLoad(d.timestamp);
      }
    });
})();
```

PR Close #58465
2024-11-01 19:14:16 +00:00
Kristiyan Kostadinov
5ca48e802b refactor(compiler): remove unused field from output AST (#58444)
The `ExternalReference.runtime` field wasn't being used anywhere.

PR Close #58444
2024-11-01 14:32:57 +00:00
Kristiyan Kostadinov
21adbba784 fix(compiler): avoid having to duplicate core environment (#58444)
Switches to referencing the core environment directly in the generated code, instead of having to duplicate it.

PR Close #58444
2024-11-01 14:32:57 +00:00