29 Commits

Author SHA1 Message Date
Justin Ridgewell
4daf11528c Return inserted/replaced paths (#5710)
* Return inserted/replaced paths

This gives `Path`’s replacement and insertion methods a consistent
return value: the inserted/replaced paths.

Before, they could return `undefined`, a `node`, or a the current path
inside an array. It was kinda pointless.  But now they always return an
array of paths, which is useful for solving
https://github.com/babel/babel/pull/4935#discussion_r96151368.

* Return inserted nodes and not BlockStatement

Addded test for bug #4363

* Cleanups

- `#replaceWith` will now return the current path if it's the same node
- `#insertAfter` and `#insertBefore` use public Path APIs now
- Makes container insertion faster (single splice call)
- Use public APIs in container insertion
- Replacing a statement with an expression returns the expression's path
- Replacing an expression with multiple statements returns the inserted
  closure's body's paths.
2017-09-11 16:07:04 -04:00
Justin Ridgewell
1797ac5015 Update babylon and remove bad label tests (#6188) 2017-09-01 12:29:48 -04:00
Artem Yavorsky
b2b3d7944a Spec compatibility for iteratorClose condition. (#6094)
* for-of: IteratorClose spec compatibility.

See #3:
https://tc39.github.io/ecma262/#sec-iteratorclose

* Update spec fixtures for for-of.

* Fix IteratorClose case for remap-async-to-generator.

* Fix IteratorClose case for async-generator-function test output.

* Modify few tests according to iteratorClose fix.

* Fix iteratorClose for helpers.slicedToArray also.

* Update iteratorClose fixture for commonjs.
2017-08-28 13:39:02 -06:00
Henry Zhu
827d84536a Merge pull request #6156 from jridgewell/pr/5502
Fix overshadowing local binding
2017-08-28 13:38:21 -06:00
Justin Ridgewell
3e487f89ab Don't merge test options. (#6157)
* Don't merge test options.

Particularly, I don't want `lodash/merge` to merge my specific plugins
with the general test plugins. It led to odd behavior where I could
enable a loose transform in my specific test, just to have it overridden
by the test fixture's general options.

* Need options
2017-08-28 13:36:03 -06:00
Moti Zilberman
68786c4f0f Add test for issue #4946 2017-08-25 19:21:38 -04:00
Boopathi Rajaa
ab76cb6b53 Fix scope of catch block (#5980)
* Fix scope of catch block

* Throw error on Duplicate variable declaration

* Update test
2017-08-08 16:26:29 -04:00
Henry Zhu
0f823beeb1 Newlines in fixtures (#6044)
* write newlines for fixtures

* rerun fixtures
2017-08-02 15:35:29 -04:00
Daniel Tschinder
b3372a572d Remove whitespace generation (#5833)
* Remove whitespace generation and rely on default printing

Changes to printing:
* Add newline after last empty SwitchCase
* Add newlines around block comments if they are non-flow comments or contain newlines

* Fix a few more fixtures
2017-06-27 21:57:02 -05:00
Henry Zhu
123dce5fcd Hardcode to double quotes, indent to 2 spaces (#5824) 2017-06-05 22:14:37 -05:00
Peeyush Kushwaha
72d83acc07 Properly convert continue statements in switch
Ignore just non-label break statements in a switch, and allow continue
statments and lablled break statements.

Fixes #5725
2017-05-25 13:49:29 +05:30
Logan Smyth
14584c218c Kill the "shadow-functions.js" internal plugin in favor of an explicit helper (#5677)
* Handle arrow function processing via shared API rather than default plugin.

* Fix a few small PR comments.

* Preserve existing spec arrow 'this' rewrites, and support spec in subclass constructors.
2017-05-05 13:27:18 -07:00
Ben Alpert
14d3c2e256 Avoid adding unnecessary closure for block scoping (#5246)
When you write

```
for (const x of l) {
  setTimeout(() => x);
}
```

we need to add a closure because the variable is meant to be block-scoped and recreated each time the block runs. We do this.

However, we also add the closure when no loop is present. This isn't necessary, because if no loop is present then each piece of code runs at most once. I changed the transform to only add a closure if a variable is referenced from within a loop.
2017-02-13 13:46:00 -08:00
Ben Alpert
ff8a10e52f Add option to block-scoping to slow on throw code (#5236)
The let/const plugin can add closures where you don't expect them. This is undesirable in some perf-sensitive projects (ex: React). I added an option that throws whenever the plugin adds a function (as opposed to simply renaming variables when converting to var).
2017-02-06 13:50:56 -05:00
Sergey Rubanov
292c3ca206 Refactor test packages to use ES modules instead of CJS (#5138) 2017-01-16 11:25:04 -05:00
Ben Newman
26b4e0909e Add (and fix) failing test of function parameter bindings in a catch block (#4880)
* Add failing test of function parameter bindings in a catch block.

This test can be run in isolation via the following command:

  TEST_GREP='block-scoping.*function in catch' make test-only

This test fails because BlockScoping#getLetReferences accidentally
considers the parameters of the function declaration as let bindings in
the catch scope. When the name of the catch parameter is the same as one
of the function's parameter names, the function declaration will be
unnecessarily wrapped to isolate its parameters from the outer scope.

While the extra wrapping may not seem harmful in this case, this behavior
is a symptom of a deeper problem that causes very subtle bugs in transform
code involving catch parameters and function declarations. This test case
was just the simplest example I could find to demonstrate the problem.

I have a proposed fix for this problem that I will push as soon as the
tests fail for this commit.

* Make BlockScoping#getLetReferences ignore function parameters.
2016-12-08 10:16:48 -05:00
Moti Zilberman
7a7704fea0 Fix block scoping transform for declarations in labeled statements (#4669)
* Fix block scoping transform for declarations in labeled statements (#4122)

* DRY block-scoping
2016-10-05 21:47:21 +02:00
Ryan Biwer
23ea626241 Fix block scope remapping (fixes T7525) (#3662) 2016-09-01 10:48:32 -04:00
Adam Leventhal
0d1e1ee10c incorrect handling of returns nested in switch cases (#3618) 2016-08-18 10:14:07 -04:00
Logan Smyth
5ddce1372b Fix an issue with the switch handing from PR #3490. 2016-06-11 00:17:36 -07:00
Jay Phelps
4166e5148e create new lexical env inside switch statement blocks, fixes #T7324 2016-04-29 23:54:41 -07:00
Amjad Masad
8b4b02a5fb [hotfix T7197] Use scope.moveBindingTo
I had deleted the binding and created a new one. I naively thought that
the analysis will automatically run again. But now discovered the method
I actually want to use: `scope.moveBindingTo` which moves the binding
and all the correct analysis. The only thing that was left to do is to
update `binding.kind` which I did manually.
2016-03-09 13:24:20 -08:00
Henry Zhu
937b8a6767 use flow instead of flow-comments 2016-03-04 15:50:05 -05:00
Amjad Masad
30bb38c4bb Update scope binding info after transforming block-scoped bindings
When convert a const, let or any other block-bound binding to a var we
forget to update the scope info. This confuses other transforms that may
come after this as to which scope does the binding belongs to.

This also uncovered an issue where duplicate block-scoped bindings were allowed
to co-exist.
2016-03-01 17:03:06 -08:00
Amjad Masad
0d8e5a9e86 Fix over-parenthesizing of function expressions 2015-12-24 14:55:51 -08:00
Amjad Masad
a17d62ca2b Convert the constants transform plugin to a validation plugin
A follow up from https://github.com/babel/babel/pull/2969 were we added support for consts in the block-scoping plugin.
2015-11-11 10:45:31 -08:00
Amjad Masad
aa8b96bf38 Support consts in block-scoping transform 2015-11-11 10:42:55 -08:00
Amjad Masad
4cca0abd2f move them to the correct place 2015-11-11 10:42:54 -08:00
Sebastian McKenzie
15969a0904 move all plugin tests out of babel-core and into their appropriate folders 2015-11-08 23:04:10 -08:00