Sha256: 0220048f26cd2b060ec78b9e3968098c3f60adec166b306ff1b00b4ed6e397e9

Contents?: true

Size: 1.59 KB

Versions: 3

Compression:

Stored size: 1.59 KB

Contents

/**
 @module ember
*/
/**
  A Glimmer2 AST transformation that replaces all instances of

  ```handlebars
 {{#each-in iterableThing as |key value|}}
  ```

  with

  ```handlebars
 {{#each (-each-in iterableThing) as |value key|}}
  ```

  @private
  @class TransformHasBlockSyntax
*/
export default function transformEachInIntoEach(env) {
    let { builders: b } = env.syntax;
    return {
        name: 'transform-each-in-into-each',
        visitor: {
            BlockStatement(node) {
                if (node.path.original === 'each-in') {
                    node.params[0] = b.sexpr(b.path('-each-in'), [node.params[0]]);
                    let blockParams = node.program.blockParams;
                    if (!blockParams || blockParams.length === 0) {
                        // who uses {{#each-in}} without block params?!
                    }
                    else if (blockParams.length === 1) {
                        // insert a dummy variable for the first slot
                        // pick a name that won't parse so it won't shadow any real variables
                        blockParams = ['( unused value )', blockParams[0]];
                    }
                    else {
                        let key = blockParams.shift();
                        let value = blockParams.shift();
                        blockParams = [value, key, ...blockParams];
                    }
                    node.program.blockParams = blockParams;
                    return b.block(b.path('each'), node.params, node.hash, node.program, node.inverse, node.loc);
                }
            },
        },
    };
}

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
discourse-ember-source-3.6.0.0 dist/es/ember-template-compiler/lib/plugins/transform-each-in-into-each.js
discourse-ember-source-3.5.1.1 dist/es/ember-template-compiler/lib/plugins/transform-each-in-into-each.js
discourse-ember-source-3.5.1.0 dist/dist/es/ember-template-compiler/lib/plugins/transform-each-in-into-each.js