Commit graph

2 commits

Author SHA1 Message Date
Nathan Shively-Sanders 6419240ab2
Declaration emit includes function properties (#26499)
* Declaration emit includes function properties

It does this by printing the type as an object literal type:

```ts
function f() { }
f.p = 1
```

Appears in a d.ts as

```ts
declare var f: {
  (): void;
  p: number;
}
```

It would also be possible to represent it as a namespace merge. I'm not
sure which is better.

```ts
declare function f(): void;
declare namespace f {
  export var p: number;
}
```

In order to avoid a private-name-used error (though I think it was
actually *unused*), I also had to change the nodeBuilder code to match.
This is arguably harder to read. So it's possible that I should instead
keep the nodeBuilder version as `typeof f` and make an exception for
private name use.

* Emit namespace merge instead of object type

This makes the change smaller, overall.

* Fix isJSContainerFunctionDeclaration+namespace merges

Also improve emit style to match other namespace emit.

* Add isPrivate + test case from PR comments
2018-08-27 10:29:53 -07:00
Nathan Shively-Sanders cc67ce1141
Property assignments in Typescript (#26368)
* Allow special property assignments in TS

But only for functions and constant variable declarations initialised with
functions.

This specifically excludes class declarations and class expressions,
which differs from Javascript. That's because Typescript supports
`static` properties, which are equivalent to property assignments to a
class.

* Improve contextual typing predicate

Don't think it's right yet, but probably closer?

* More fixes.

The code is still fantastically ugly, but everything works the way it
should.

Also update baselines, even where it is ill-advised.

* Cleanup

* Remove extra whitespace

* Some kind of fix to isAnyDeclarationName

It's not done yet.

Specifically, in TS:
Special property assignments are supposed to be declaration sites (but not all
top-level assignments), and I think I
got them to be. (But not sure).

In JS:
Special property assignments are supposed to be declaration sites (but not all
top-level assignments), and I'm pretty sure ALL top-level assignments
have been declaration sites for some time. This is incorrect, and
probably means the predicate needs to be the same for both dialects.

* Add fourslash and improve isAnyDeclarationName

Now JS behaves the same as TS.

* Cleanup from PR comments
2018-08-15 15:25:25 -07:00