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

RFC: Add a move method, or tweak insert #14

Closed
Victorystick opened this issue Aug 31, 2015 · 4 comments
Closed

RFC: Add a move method, or tweak insert #14

Victorystick opened this issue Aug 31, 2015 · 4 comments

Comments

@Victorystick
Copy link

Now that there exists a Rollup issue for SystemJS, we'll need to be able to hoist function declarations, while retaining SourceMaps.

I haven't been able to find any move method, and the method combination that could have replicated the behavior (snip, remove and insert) requires insert to work on MagicStrings in addition to strings.

I suggest extending MagicStrings API with either a move method, or an insert that supports MagicStrings.

@Victorystick
Copy link
Author

The fundamental operation I'm looking for is a way to turn this:

function add( x, y ) {
  return x + y;
}

function sub( x, y ) {
  return x - y;
}

into this:

function sub( x, y ) {
  return x - y;
}

function add( x, y ) {
  return x + y;
}

while preserving source maps.

@Rich-Harris
Copy link
Owner

This seems doable, though it'd require a bit of a rethink about how mappings are stored. Hmm... I don't quite understand the use case – why would you need to move functions around?

@Victorystick
Copy link
Author

Ok, the example doesn't really make it clear.

If you take a look at SystemJS, what I propose should make more sense. In SystemJS, declarations are hoisted to the top of the wrapper, while all other statements are kept at the "bottom", inside another function.

For example:

import { a } from 'foo';

export var x = a( data, 17 );

export function data(...) { ... }

becomes something like:

System.register(['foo'], function($__export) {
    var a, x;
    function data(...) { ... }
    $__export('data', data);
    return {
      setters: [
      // every time a dependency updates an export, 
      // this function is called to update the local binding
      // the setter array matches up with the dependency array above
      function(foo) {
        a = foo.a;
      }
      ],
      execute: function() {
        // use the export function to update the exports of this module
        x = a( data, 17 );
        $__export( 'x', x );
      }
    };
  });

So you see, I don't want to change the order of function declarations by moving them around, no. Depending on what you consider being moved where, I want to move Statements down below all declarations.

@Rich-Harris Rich-Harris mentioned this issue Dec 24, 2015
4 tasks
@Rich-Harris
Copy link
Owner

This is implemented in 0.11! Bit experimental – no sourcemap tests yet, for example – but it seems to work. Tests have examples of usage – basically goes like this:

var s = new MagicString( 'abcdefghijkl' );
s.move( 0, 3, 6 ); // start, end, newIndex

assert.equal( s.toString(), 'defabcghijkl' );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants