feat(directive): validate input transforms like ngtsc - #494
Open
ashley-hunter wants to merge 1 commit into
Open
ashley-hunter wants to merge 1 commit into
ashley-hunter wants to merge 1 commit into
Conversation
ngtsc rejects an input `transform` it can't prove is a usable function
(`parseDecoratorInputTransformFunction`); oxc accepted anything, so code that
fails to build with ngc compiled here. Both `@Input({ transform })` and
`inputs: [{ transform }]` are now checked, with ngtsc's diagnostics word for
word and in its order (`inputs:` entries, then members):
- not a function: literals, classes, calls, and function expressions reached
through a variable, property access or parentheses (only one written directly
as the property value is analyzable)
- generic, or overloaded (more than one call signature)
- first parameter untyped or a spread
- first parameter typed with a same-file type that isn't exported
- a `static ngAcceptInputType_<name>` member on the class
The evaluator learns functions to do this: same-file function declarations and
static methods resolve to their definitions. A transform reached through such a
reference (`T.f` where `const T = { f }`) is emitted as the function's own name,
as ngtsc does; a static method keeps the written expression, since ngtsc's bare
method name isn't in scope and would bind an unrelated same-named function.
`@Input` members are also found through a namespace import (`@core.Input`),
like `@core.Component` already was. Imported transforms can't be inspected from
one file and are assumed to be functions.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stack (2/4): #493 inputs/outputs → #494 transform validation → #495
queries:→ #496.d.tstransform types. This is #494.Checks input
transforms the way ngtsc does, for both@Input({ transform })andinputs: [{ transform }].The bug
ngtsc rejects a transform it can't prove is a usable function (
parseDecoratorInputTransformFunction). oxc accepted anything, so code that fails to build with ngc compiled here, e.g.The fix
Each rule matches ngtsc, with its diagnostic word for word and in its order (
inputs:entries, then members):static ngAcceptInputType_<name>member already on the classThe evaluator learns functions to do this: same-file function declarations and static methods resolve to their definitions. A transform reached through such a reference (
T.fwhereconst T = { f }) is emitted as the function's own name, as ngtsc does.One deliberate difference: for a static method (
transform: Utils.coerce) ngtsc emits the bare method name. That name isn't in scope, so it either throws or binds an unrelated same-named function, and oxc keeps the written expression instead.@Inputmembers are also recognised through a namespace import (@core.Input), like@core.Componentalready was. Imported transforms can't be inspected from one file and are assumed to be functions.Tests
Added to the ngtsc snapshot: Angular's input transform specs from
ngtsc_spec(non-function values, generic, overloaded, spread, untyped, non-exported type,ngAcceptInputTypeclash), plus probes of each rule and of how ngtsc describes values in these errors. The threengtsc_speccases that need types from another file are listed as skipped with that reason, as is an aliased@Inputimport (member decorators are matched by name throughout oxc). A separate test covers the static-method case.