feat(core): support properties and events in addition to inputs and outputs to make transition easier

Closes #4482
This commit is contained in:
vsavkin 2015-10-02 16:21:49 -07:00 committed by Victor Savkin
parent 5ea6dc844c
commit c9901c5fe0
8 changed files with 93 additions and 8 deletions

View File

@ -1,5 +1,5 @@
import {resolveForwardRef, Injectable} from 'angular2/src/core/di'; import {resolveForwardRef, Injectable} from 'angular2/src/core/di';
import {Type, isPresent, stringify} from 'angular2/src/core/facade/lang'; import {Type, isPresent, isBlank, stringify} from 'angular2/src/core/facade/lang';
import {BaseException} from 'angular2/src/core/facade/exceptions'; import {BaseException} from 'angular2/src/core/facade/exceptions';
import {ListWrapper, StringMap, StringMapWrapper} from 'angular2/src/core/facade/collection'; import {ListWrapper, StringMap, StringMapWrapper} from 'angular2/src/core/facade/collection';
import { import {
@ -110,6 +110,10 @@ export class DirectiveResolver {
var mergedQueries = var mergedQueries =
isPresent(dm.queries) ? StringMapWrapper.merge(dm.queries, queries) : queries; isPresent(dm.queries) ? StringMapWrapper.merge(dm.queries, queries) : queries;
// TODO: remove after migrating from properties to inputs
if (mergedInputs.length == 0 && isPresent(dm.properties)) mergedInputs = dm.properties;
if (mergedOutputs.length == 0 && isPresent(dm.events)) mergedOutputs = dm.events;
if (dm instanceof ComponentMetadata) { if (dm instanceof ComponentMetadata) {
return new ComponentMetadata({ return new ComponentMetadata({
selector: dm.selector, selector: dm.selector,

View File

@ -15,13 +15,18 @@ export './metadata/view.dart';
*/ */
class Directive extends DirectiveMetadata { class Directive extends DirectiveMetadata {
const Directive({String selector, List<String> inputs, const Directive({String selector, List<String> inputs,
List<String> outputs, Map<String, String> host, List<String> outputs,
@deprecated List<String> properties,
@deprecated List<String> events,
Map<String, String> host,
List bindings, String exportAs, String moduleId, List bindings, String exportAs, String moduleId,
Map<String, dynamic> queries}) Map<String, dynamic> queries})
: super( : super(
selector: selector, selector: selector,
inputs: inputs, inputs: inputs,
outputs: outputs, outputs: outputs,
properties: properties,
events: events,
host: host, host: host,
bindings: bindings, bindings: bindings,
exportAs: exportAs, exportAs: exportAs,
@ -34,7 +39,10 @@ class Directive extends DirectiveMetadata {
*/ */
class Component extends ComponentMetadata { class Component extends ComponentMetadata {
const Component({String selector, List<String> inputs, const Component({String selector, List<String> inputs,
List<String> outputs, Map<String, String> host, List<String> outputs,
@deprecated List<String> properties,
@deprecated List<String> events,
Map<String, String> host,
List bindings, String exportAs, String moduleId, List bindings, String exportAs, String moduleId,
Map<String, dynamic> queries, Map<String, dynamic> queries,
List viewBindings, ChangeDetectionStrategy changeDetection}) List viewBindings, ChangeDetectionStrategy changeDetection})
@ -42,6 +50,8 @@ class Component extends ComponentMetadata {
selector: selector, selector: selector,
inputs: inputs, inputs: inputs,
outputs: outputs, outputs: outputs,
properties: properties,
events: events,
host: host, host: host,
bindings: bindings, bindings: bindings,
exportAs: exportAs, exportAs: exportAs,

View File

@ -149,6 +149,8 @@ export interface DirectiveFactory {
selector?: string, selector?: string,
inputs?: string[], inputs?: string[],
outputs?: string[], outputs?: string[],
properties?: string[],
events?: string[],
host?: StringMap<string, string>, host?: StringMap<string, string>,
bindings?: any[], bindings?: any[],
exportAs?: string, exportAs?: string,
@ -159,6 +161,8 @@ export interface DirectiveFactory {
selector?: string, selector?: string,
inputs?: string[], inputs?: string[],
outputs?: string[], outputs?: string[],
properties?: string[],
events?: string[],
host?: StringMap<string, string>, host?: StringMap<string, string>,
bindings?: any[], bindings?: any[],
exportAs?: string, exportAs?: string,
@ -215,6 +219,8 @@ export interface ComponentFactory {
selector?: string, selector?: string,
inputs?: string[], inputs?: string[],
outputs?: string[], outputs?: string[],
properties?: string[],
events?: string[],
host?: StringMap<string, string>, host?: StringMap<string, string>,
bindings?: any[], bindings?: any[],
exportAs?: string, exportAs?: string,
@ -227,6 +233,8 @@ export interface ComponentFactory {
selector?: string, selector?: string,
inputs?: string[], inputs?: string[],
outputs?: string[], outputs?: string[],
properties?: string[],
events?: string[],
host?: StringMap<string, string>, host?: StringMap<string, string>,
bindings?: any[], bindings?: any[],
exportAs?: string, exportAs?: string,

View File

@ -467,6 +467,12 @@ export class DirectiveMetadata extends InjectableMetadata {
*/ */
inputs: string[]; inputs: string[];
/**
* @deprecated
* Same as `inputs`. This is to enable easier migration.
*/
properties: string[];
/** /**
* Enumerates the set of event-bound output properties. * Enumerates the set of event-bound output properties.
* *
@ -514,6 +520,12 @@ export class DirectiveMetadata extends InjectableMetadata {
*/ */
outputs: string[]; outputs: string[];
/**
* @deprecated
* Same as `outputs`. This is to enable easier migration.
*/
events: string[];
/** /**
* Specify the events, actions, properties and attributes related to the host element. * Specify the events, actions, properties and attributes related to the host element.
* *
@ -738,10 +750,13 @@ export class DirectiveMetadata extends InjectableMetadata {
*/ */
queries: StringMap<string, any>; queries: StringMap<string, any>;
constructor({selector, inputs, outputs, host, bindings, exportAs, moduleId, queries}: { constructor({selector, inputs, outputs, properties, events, host, bindings, exportAs, moduleId,
queries}: {
selector?: string, selector?: string,
inputs?: string[], inputs?: string[],
outputs?: string[], outputs?: string[],
properties?: string[],
events?: string[],
host?: StringMap<string, string>, host?: StringMap<string, string>,
bindings?: any[], bindings?: any[],
exportAs?: string, exportAs?: string,
@ -753,6 +768,11 @@ export class DirectiveMetadata extends InjectableMetadata {
this.inputs = inputs; this.inputs = inputs;
this.outputs = outputs; this.outputs = outputs;
this.host = host; this.host = host;
// TODO: remove this once properties and events are removed.
this.properties = properties;
this.events = events;
this.exportAs = exportAs; this.exportAs = exportAs;
this.moduleId = moduleId; this.moduleId = moduleId;
this.queries = queries; this.queries = queries;
@ -856,11 +876,13 @@ export class ComponentMetadata extends DirectiveMetadata {
*/ */
viewBindings: any[]; viewBindings: any[];
constructor({selector, inputs, outputs, host, exportAs, moduleId, bindings, viewBindings, constructor({selector, inputs, outputs, properties, events, host, exportAs, moduleId, bindings,
changeDetection = ChangeDetectionStrategy.Default, queries}: { viewBindings, changeDetection = ChangeDetectionStrategy.Default, queries}: {
selector?: string, selector?: string,
inputs?: string[], inputs?: string[],
outputs?: string[], outputs?: string[],
properties?: string[],
events?: string[],
host?: StringMap<string, string>, host?: StringMap<string, string>,
bindings?: any[], bindings?: any[],
exportAs?: string, exportAs?: string,
@ -873,6 +895,8 @@ export class ComponentMetadata extends DirectiveMetadata {
selector: selector, selector: selector,
inputs: inputs, inputs: inputs,
outputs: outputs, outputs: outputs,
properties: properties,
events: events,
host: host, host: host,
exportAs: exportAs, exportAs: exportAs,
moduleId: moduleId, moduleId: moduleId,

View File

@ -26,7 +26,7 @@ class SomeChildDirective extends SomeDirective {
} }
@Directive({selector: 'someDirective', inputs: ['c']}) @Directive({selector: 'someDirective', inputs: ['c']})
class SomeDirectiveWithProperties { class SomeDirectiveWithInputs {
@Input() a; @Input() a;
@Input("renamed") b; @Input("renamed") b;
c; c;
@ -39,6 +39,15 @@ class SomeDirectiveWithOutputs {
c; c;
} }
@Directive({selector: 'someDirective', properties: ['a']})
class SomeDirectiveWithProperties {
}
@Directive({selector: 'someDirective', events: ['a']})
class SomeDirectiveWithEvents {
}
@Directive({selector: 'someDirective'}) @Directive({selector: 'someDirective'})
class SomeDirectiveWithSetterProps { class SomeDirectiveWithSetterProps {
@ -125,7 +134,7 @@ export function main() {
describe('inputs', () => { describe('inputs', () => {
it('should append directive inputs', () => { it('should append directive inputs', () => {
var directiveMetadata = resolver.resolve(SomeDirectiveWithProperties); var directiveMetadata = resolver.resolve(SomeDirectiveWithInputs);
expect(directiveMetadata.inputs).toEqual(['c', 'a', 'b: renamed']); expect(directiveMetadata.inputs).toEqual(['c', 'a', 'b: renamed']);
}); });
@ -133,6 +142,12 @@ export function main() {
var directiveMetadata = resolver.resolve(SomeDirectiveWithSetterProps); var directiveMetadata = resolver.resolve(SomeDirectiveWithSetterProps);
expect(directiveMetadata.inputs).toEqual(['a: renamed']); expect(directiveMetadata.inputs).toEqual(['a: renamed']);
}); });
it('should use properties as inputs', () => {
var directiveMetadata = resolver.resolve(SomeDirectiveWithProperties);
expect(directiveMetadata.inputs).toEqual(['a']);
});
}); });
describe('outputs', () => { describe('outputs', () => {
@ -145,6 +160,11 @@ export function main() {
var directiveMetadata = resolver.resolve(SomeDirectiveWithGetterOutputs); var directiveMetadata = resolver.resolve(SomeDirectiveWithGetterOutputs);
expect(directiveMetadata.outputs).toEqual(['a: renamed']); expect(directiveMetadata.outputs).toEqual(['a: renamed']);
}); });
it('should use events as outputs', () => {
var directiveMetadata = resolver.resolve(SomeDirectiveWithEvents);
expect(directiveMetadata.outputs).toEqual(['a']);
});
}); });
describe('host', () => { describe('host', () => {

View File

@ -157,10 +157,12 @@ var NG_API = [
'Component.bindings', 'Component.bindings',
'Component.changeDetection', 'Component.changeDetection',
'Component.outputs', 'Component.outputs',
'Component.events',
'Component.exportAs', 'Component.exportAs',
'Component.host', 'Component.host',
'Component.moduleId', 'Component.moduleId',
'Component.inputs', 'Component.inputs',
'Component.properties',
'Component.queries', 'Component.queries',
'Component.selector', 'Component.selector',
'Component.viewBindings', 'Component.viewBindings',
@ -168,10 +170,12 @@ var NG_API = [
'ComponentMetadata.bindings', 'ComponentMetadata.bindings',
'ComponentMetadata.changeDetection', 'ComponentMetadata.changeDetection',
'ComponentMetadata.outputs', 'ComponentMetadata.outputs',
'ComponentMetadata.events',
'ComponentMetadata.exportAs', 'ComponentMetadata.exportAs',
'ComponentMetadata.host', 'ComponentMetadata.host',
'ComponentMetadata.moduleId', 'ComponentMetadata.moduleId',
'ComponentMetadata.inputs', 'ComponentMetadata.inputs',
'ComponentMetadata.properties',
'ComponentMetadata.queries', 'ComponentMetadata.queries',
'ComponentMetadata.selector', 'ComponentMetadata.selector',
'ComponentMetadata.viewBindings', 'ComponentMetadata.viewBindings',
@ -370,19 +374,23 @@ var NG_API = [
'Directive', 'Directive',
'Directive.bindings', 'Directive.bindings',
'Directive.outputs', 'Directive.outputs',
'Directive.events',
'Directive.exportAs', 'Directive.exportAs',
'Directive.host', 'Directive.host',
'Directive.moduleId', 'Directive.moduleId',
'Directive.inputs', 'Directive.inputs',
'Directive.properties',
'Directive.queries', 'Directive.queries',
'Directive.selector', 'Directive.selector',
'DirectiveMetadata', 'DirectiveMetadata',
'DirectiveMetadata.bindings', 'DirectiveMetadata.bindings',
'DirectiveMetadata.outputs', 'DirectiveMetadata.outputs',
'DirectiveMetadata.events',
'DirectiveMetadata.exportAs', 'DirectiveMetadata.exportAs',
'DirectiveMetadata.host', 'DirectiveMetadata.host',
'DirectiveMetadata.moduleId', 'DirectiveMetadata.moduleId',
'DirectiveMetadata.inputs', 'DirectiveMetadata.inputs',
'DirectiveMetadata.properties',
'DirectiveMetadata.queries', 'DirectiveMetadata.queries',
'DirectiveMetadata.selector', 'DirectiveMetadata.selector',
'DirectiveResolver', 'DirectiveResolver',

View File

@ -235,6 +235,9 @@ class _DirectiveMetadataVisitor extends Object
case 'inputs': case 'inputs':
_populateProperties(node.expression); _populateProperties(node.expression);
break; break;
case 'properties':
_populateProperties(node.expression);
break;
case 'host': case 'host':
_populateHost(node.expression); _populateHost(node.expression);
break; break;
@ -247,6 +250,9 @@ class _DirectiveMetadataVisitor extends Object
case 'outputs': case 'outputs':
_populateEvents(node.expression); _populateEvents(node.expression);
break; break;
case 'events':
_populateEvents(node.expression);
break;
} }
return null; return null;
} }

View File

@ -175,6 +175,11 @@ _AnalyzerOutputLine.prototype = {
return true; return true;
} }
} }
if (this.errorCode.match(/DEPRECATED_MEMBER_USE/i)) {
return true;
}
// TODO: https://github.com/angular/ts2dart/issues/168 // TODO: https://github.com/angular/ts2dart/issues/168
if (this.errorCode.match(/UNUSED_CATCH_STACK/i)) { if (this.errorCode.match(/UNUSED_CATCH_STACK/i)) {
return true; return true;