Sha256: 9b402342739b4e836dbfe8e0ce62b2a0f1248a36c53ba9e94b5d7611e62a49fc

Contents?: true

Size: 1.95 KB

Versions: 4

Compression:

Stored size: 1.95 KB

Contents

import LoopStatement from './shared/LoopStatement.js';
import destructure from '../../utils/destructure.js';
import extractNames from '../extractNames.js';

export default class ForInStatement extends LoopStatement {
	findScope(functionScope) {
		return functionScope || !this.createdScope
			? this.parent.findScope(functionScope)
			: this.body.scope;
	}

	transpile(code, transforms) {
		const hasDeclaration = this.left.type === 'VariableDeclaration';

		if (this.shouldRewriteAsFunction) {
			// which variables are declared in the init statement?
			const names =
				hasDeclaration
					? [].concat.apply(
							[],
							this.left.declarations.map(declarator =>
								extractNames(declarator.id)
							)
						)
					: [];

			this.args = names.map(
				name => (name in this.aliases ? this.aliases[name].outer : name)
			);
			this.params = names.map(
				name => (name in this.aliases ? this.aliases[name].inner : name)
			);
		}

		super.transpile(code, transforms);

		const maybePattern = hasDeclaration ? this.left.declarations[0].id : this.left;
		if (maybePattern.type !== 'Identifier') {
			this.destructurePattern(code, maybePattern, hasDeclaration);
		}
	}

	destructurePattern(code, pattern, isDeclaration) {
		const scope = this.findScope(true);
		const i0 = this.getIndentation();
		const i1 = i0 + code.getIndentString();

		const ref = scope.createIdentifier('ref');

		const bodyStart = this.body.body.length ? this.body.body[0].start : this.body.start + 1;

		code.move(pattern.start, pattern.end, bodyStart);

		code.prependRight(pattern.end, isDeclaration ? ref : `var ${ref}`);

		let statementGenerators = [];
		destructure(
			code,
			id => scope.createIdentifier(id),
			({ name }) => scope.resolveName(name),
			pattern,
			ref,
			false,
			statementGenerators
		);

		let suffix = `;\n${i1}`;
		statementGenerators.forEach((fn, i) => {
			if (i === statementGenerators.length - 1) {
				suffix = `;\n\n${i1}`;
			}

			fn(bodyStart, '', suffix);
		});
	}
}

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
jass-0.9.5 vendor/node_modules/buble/src/program/types/ForInStatement.js
jass-0.9.4 vendor/node_modules/buble/src/program/types/ForInStatement.js
jass-0.9.3 vendor/node_modules/buble/src/program/types/ForInStatement.js
jass-0.9.1 vendor/node_modules/buble/src/program/types/ForInStatement.js