fix(compiler): generate inputs with aliases properly (#26774)
PR Close #26774
This commit is contained in:

committed by
Matias Niemelä

parent
c048358cf9
commit
19fcfc3d00
@ -110,12 +110,14 @@ export function extractDirectiveMetadata(
|
||||
// fields.
|
||||
const inputsFromMeta = parseFieldToPropertyMapping(directive, 'inputs', reflector, checker);
|
||||
const inputsFromFields = parseDecoratedFields(
|
||||
filterToMembersWithDecorator(decoratedElements, 'Input', coreModule), reflector, checker);
|
||||
filterToMembersWithDecorator(decoratedElements, 'Input', coreModule), reflector, checker,
|
||||
resolveInput);
|
||||
|
||||
// And outputs.
|
||||
const outputsFromMeta = parseFieldToPropertyMapping(directive, 'outputs', reflector, checker);
|
||||
const outputsFromFields = parseDecoratedFields(
|
||||
filterToMembersWithDecorator(decoratedElements, 'Output', coreModule), reflector, checker);
|
||||
filterToMembersWithDecorator(decoratedElements, 'Output', coreModule), reflector, checker,
|
||||
resolveOutput) as{[field: string]: string};
|
||||
// Construct the list of queries.
|
||||
const contentChildFromFields = queriesFromFields(
|
||||
filterToMembersWithDecorator(decoratedElements, 'ContentChild', coreModule), reflector,
|
||||
@ -330,7 +332,8 @@ function parseFieldToPropertyMapping(
|
||||
*/
|
||||
function parseDecoratedFields(
|
||||
fields: {member: ClassMember, decorators: Decorator[]}[], reflector: ReflectionHost,
|
||||
checker: ts.TypeChecker): {[field: string]: string} {
|
||||
checker: ts.TypeChecker, mapValueResolver: (publicName: string, internalName: string) =>
|
||||
string | string[]): {[field: string]: string | string[]} {
|
||||
return fields.reduce(
|
||||
(results, field) => {
|
||||
const fieldName = field.member.name;
|
||||
@ -344,7 +347,7 @@ function parseDecoratedFields(
|
||||
if (typeof property !== 'string') {
|
||||
throw new Error(`Decorator argument must resolve to a string`);
|
||||
}
|
||||
results[fieldName] = property;
|
||||
results[fieldName] = mapValueResolver(property, fieldName);
|
||||
} else {
|
||||
// Too many arguments.
|
||||
throw new Error(
|
||||
@ -353,7 +356,15 @@ function parseDecoratedFields(
|
||||
});
|
||||
return results;
|
||||
},
|
||||
{} as{[field: string]: string});
|
||||
{} as{[field: string]: string | string[]});
|
||||
}
|
||||
|
||||
function resolveInput(publicName: string, internalName: string) {
|
||||
return [publicName, internalName];
|
||||
}
|
||||
|
||||
function resolveOutput(publicName: string, internalName: string) {
|
||||
return publicName;
|
||||
}
|
||||
|
||||
export function queriesFromFields(
|
||||
|
@ -518,7 +518,10 @@ function tcbGetInputBindingExpressions(
|
||||
// is desired. Invert `dir.inputs` into `propMatch` to create this map.
|
||||
const propMatch = new Map<string, string>();
|
||||
const inputs = dir.inputs;
|
||||
Object.keys(inputs).forEach(key => propMatch.set(inputs[key], key));
|
||||
Object.keys(inputs).forEach(key => {
|
||||
Array.isArray(inputs[key]) ? propMatch.set(inputs[key][0], key) :
|
||||
propMatch.set(inputs[key] as string, key);
|
||||
});
|
||||
|
||||
// Add a binding expression to the map for each input of the directive that has a
|
||||
// matching binding.
|
||||
|
@ -1887,7 +1887,7 @@ describe('compiler compliance', () => {
|
||||
type: LifecycleComp,
|
||||
selectors: [["lifecycle-comp"]],
|
||||
factory: function LifecycleComp_Factory(t) { return new (t || LifecycleComp)(); },
|
||||
inputs: {nameMin: "name"},
|
||||
inputs: {nameMin: ["name", "nameMin"]},
|
||||
features: [$r3$.ɵNgOnChangesFeature],
|
||||
consts: 0,
|
||||
vars: 0,
|
||||
@ -2301,7 +2301,7 @@ describe('compiler compliance', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('inherited bare classes', () => {
|
||||
describe('inherited base classes', () => {
|
||||
it('should add ngBaseDef if one or more @Input is present', () => {
|
||||
const files = {
|
||||
app: {
|
||||
|
@ -56,7 +56,7 @@ describe('compiler compliance: listen()', () => {
|
||||
…
|
||||
inputs:{
|
||||
componentInput: "componentInput",
|
||||
originalComponentInput: "renamedComponentInput"
|
||||
originalComponentInput: ["renamedComponentInput", "originalComponentInput"]
|
||||
},
|
||||
outputs: {
|
||||
componentOutput: "componentOutput",
|
||||
@ -70,7 +70,7 @@ describe('compiler compliance: listen()', () => {
|
||||
…
|
||||
inputs:{
|
||||
directiveInput: "directiveInput",
|
||||
originalDirectiveInput: "renamedDirectiveInput"
|
||||
originalDirectiveInput: ["renamedDirectiveInput", "originalDirectiveInput"]
|
||||
},
|
||||
outputs: {
|
||||
directiveOutput: "directiveOutput",
|
||||
@ -86,4 +86,4 @@ describe('compiler compliance: listen()', () => {
|
||||
expectEmit(result.source, directiveDef, 'Incorrect directive definition');
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
|
Reference in New Issue
Block a user