Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: export collection of built-in directives #1524

Conversation

pkozlowski-opensource
Copy link
Member

This PR makes it slightly easier to list built-in directives in @View and addresses part of #1293 based on this comment

IMO this change is backward-compatible, non-controversial and makes it easier to implement other boilerplate-busters discussed in #1293.

@pkozlowski-opensource
Copy link
Member Author

Hmm, it looks like Dart is not happy about export {CSSClass, For, If, NonBindable, Switch, SwitchWhen, SwitchDefault};...

Any ideas on rewriting it so Dart is not complaining? Or is it a bug in the Dart transpiler that should be fixed?

@pkozlowski-opensource pkozlowski-opensource force-pushed the def_directives_collection branch 5 times, most recently from e668ce1 to d69324b Compare April 24, 2015 14:10
export * from './src/directives/class';
export * from './src/directives/for';
export * from './src/directives/if';
export * from './src/directives/non_bindable';
export * from './src/directives/switch';

export var BUILT_IN_DIRECTIVES = [For, If, NonBindable, Switch, SwitchWhen, SwitchDefault];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should not be all caps. How about coreDirectives?

@View({
  directives: [coreDirecitives, MyDirective]
})

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also it needs a doc (with an example on how to use it) so that it will show up in the documentation.

@pkozlowski-opensource
Copy link
Member Author

@mhevery changed the name to coreDirecitives and added docs.

* })
* @View({
* templateUrl: 'myComponent.html',
* directives: [coreDirecitives, OtherDirective]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo coreDirectives

@mhevery mhevery added @lgtm action: merge The PR is ready for merge by the caretaker action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews labels Apr 28, 2015
@mhevery
Copy link
Contributor

mhevery commented Apr 29, 2015

@alexeagle or @mprobst can you help @pkozlowski-opensource with the const issue above. See #1283

@alexeagle
Copy link
Contributor

handing to @mprobst

@mprobst
Copy link
Contributor

mprobst commented Apr 29, 2015

@pkozlowski-opensource so as discussed in offline, we don't have a way to mark an expression as const (in the Dart sense) from TypeScript source code. I could special case const $var = $expr; to translate as const $var = const $exp;, but I think that's wrong in the general case. const ~= final in Dart, and we might have const $var = ...; expressions that should really translate to final $var = ...;.

So two options:

  1. Use two files. Have export var coreDirectives = [For, If, ...]; in foo.ts, and const coreDirectives = const [For, If, ...]; in foo.dart.
  2. Invent some way in ts2dart to recognize expressions that should be const. We cannot put decorators (e.g. @CONST) onto expressions, so it'd have to be some uber ugly hack like const var x = /* ~~~const mode~~~ */ ( expr );. I'd rather not do that.

@tbosch
Copy link
Contributor

tbosch commented Apr 30, 2015

How about we use static fields on classes that have a @const annotation?

On Wed, Apr 29, 2015 at 3:18 PM Martin Probst notifications@github.com
wrote:

@pkozlowski-opensource https://github.com/pkozlowski-opensource so as
discussed in offline, we don't have a way to mark an expression as const
(in the Dart sense) from TypeScript source code. I could special case const
$var = $expr; to translate as const $var = const $exp;, but I think
that's wrong in the general case. const ~= final in Dart, and we might
have const $var = ...; expressions that should really translate to final
$var = ...;.

So two options:

  1. Use two files. Have export var coreDirectives = [For, If, ...]; in
    foo.ts, and const coreDirectives = const [For, If, ...]; in foo.dart.
  2. Invent some way in ts2dart to recognize expressions that should be
    const. We cannot put decorators (e.g. @const) onto expressions, so it'd
    have to be some uber ugly hack like const var x = /* ~~~const mode~~~ */
    ( expr );. I'd rather not do that.


Reply to this email directly or view it on GitHub
#1524 (comment).

@mprobst
Copy link
Contributor

mprobst commented Apr 30, 2015 via email

@tbosch
Copy link
Contributor

tbosch commented Apr 30, 2015

Yes, but having the outer class is actually a nice place to keep constants.

E.g.
@const
class AngularDefaultDirectives {
static BASICS = [For, If];
static FORMS = [...]
}

On Wed, Apr 29, 2015 at 6:24 PM Martin Probst notifications@github.com
wrote:

We could do that, but it might still be odd and hacky as the const part is
not the field, but rather the expression going into it.

The separate files approach, one for dart, one for ts, should make this
easy enough to solve, with not too much duplication (four lines of code or
so). It's IMHO also easier to understand/maintain.


Reply to this email directly or view it on GitHub
#1524 (comment).

@mhevery
Copy link
Contributor

mhevery commented May 1, 2015

I don't think const on static is a good idea. As you want to import foo not Bar.foo. I think @mprobst comment hack is reasonable.

var x = /*cost*/ [1,2];

@mprobst
Copy link
Contributor

mprobst commented May 1, 2015

I really don't like the comment hack, experience shows that's in general a terrible idea.

What about just putting that stuff into a foo.dart file for the const expressions, and foo.ts for the TypeScript variant?

@pkozlowski-opensource
Copy link
Member Author

@mprobst yeh, the comment trick doesn't sound super-clean. I will split things into separate Ts / Dart files for this PR. But I think that we will need some kind of solution for this use-case as I keep bumping into it and there is another issue (#1283) where I will need to play it again. I saw also number of other places in the source code where we don't use consts due to this very problem.... In short: it is recurring.

@alexeagle
Copy link
Contributor

Have we considered making a feature request for TypeScript to add the matching semantics with some available keyword or other syntax?

@mhevery
Copy link
Contributor

mhevery commented May 6, 2015

@alexeagle I don't think that you could ever implement dart-const semantics in JS. So this is for us to solve.

I am not a fan of comment, but duplicate code sounds even worse.

Any other suggestions?

@tbosch
Copy link
Contributor

tbosch commented May 6, 2015

We could add a magic function
On Tue, May 5, 2015 at 10:17 PM Miško Hevery notifications@github.com
wrote:

@alexeagle https://github.com/alexeagle I don't think that you could
ever implement dart-const semantics in JS. So this is for us to solve.

I am not a fan of comment, but duplicate code sounds even worse.

Any other suggestions?


Reply to this email directly or view it on GitHub
#1524 (comment).

@tbosch
Copy link
Contributor

tbosch commented May 6, 2015

Ups, didn't finish my last mail:

We could have a function function CONST(a) { return a; }. In JavaScript,
this is all there is to it.
However, the dart transpiler would recognize this and make all expressions
that are used in the argument const.

E.g.

export const BASICS = CONST([For, If]);

In Dart, this would transpile into:

const BASICS = CONST(const [For, If]);

On Wed, May 6, 2015 at 8:54 AM Tobias Bosch tbosch@google.com wrote:

We could add a magic function
On Tue, May 5, 2015 at 10:17 PM Miško Hevery notifications@github.com
wrote:

@alexeagle https://github.com/alexeagle I don't think that you could
ever implement dart-const semantics in JS. So this is for us to solve.

I am not a fan of comment, but duplicate code sounds even worse.

Any other suggestions?


Reply to this email directly or view it on GitHub
#1524 (comment).

@pkozlowski-opensource
Copy link
Member Author

We need to land dart-archive/ts2dart#151 and update deps on ts2dart before this PR can land

@pkozlowski-opensource pkozlowski-opensource force-pushed the def_directives_collection branch from a395702 to a5988b3 Compare May 11, 2015 14:05
@pkozlowski-opensource pkozlowski-opensource deleted the def_directives_collection branch July 20, 2015 12:55
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 6, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews action: merge The PR is ready for merge by the caretaker cla: yes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants