refactor(named parameters): mimic original traceur architecture

This commit is contained in:
Victor Berchet 2014-10-08 12:46:38 +02:00
parent 5527a1b1a4
commit 5818c3bf28
7 changed files with 109 additions and 38 deletions

View File

@ -1,16 +0,0 @@
import {ParseTree} from 'traceur/src/syntax/trees/ParseTree';
export class NamedParams extends ParseTree {
constructor(location, propertyNameAndValues) {
this.location = location;
this.propertyNameAndValues = propertyNameAndValues;
}
visit(visitor) {
visitor.visitNamedParamsExpression(this);
}
transform(transformer) {
return this;
}
}

View File

@ -1,7 +0,0 @@
import {BindingElement} from 'traceur/src/syntax/trees/ParseTrees';
export class ObjectPatternBindingElement extends BindingElement {
visit(visitor) {
visitor.visitObjectPatternBindingElement(this);
}
}

View File

@ -1,12 +1,15 @@
import {ParseTreeTransformer} from 'traceur/src/codegeneration/ParseTreeTransformer';
import {ParseTreeTransformer} from './ParseTreeTransformer';
import {
BINDING_ELEMENT,
OBJECT_PATTERN,
OBJECT_LITERAL_EXPRESSION
} from 'traceur/src/syntax/trees/ParseTreeType';
import {NamedParams} from '../ast/named_params';
import {ObjectPatternBindingElement} from '../ast/object_pattern_binding_element';
import {
NamedParameterList,
ObjectPatternBindingElement
} from '../syntax/trees/ParseTrees';
/**
* Transforms maps into named parameters:
@ -22,6 +25,8 @@ import {ObjectPatternBindingElement} from '../ast/object_pattern_binding_element
*/
export class NamedParamsTransformer extends ParseTreeTransformer {
/**
* Transform function calls.
*
* @param {CallExpression} tree
* @return {ParseTree}
*/
@ -58,10 +63,12 @@ export class NamedParamsTransformer extends ParseTreeTransformer {
_replaceLastArgWithNamedParams(tree) {
var args = tree.args.args;
var last = this._last(args);
args[args.length - 1] = new NamedParams(last.location, last.propertyNameAndValues);
args[args.length - 1] = new NamedParameterList(last.location, last.propertyNameAndValues);
}
/**
* Transform function declaration.
*
* @param {ObjectPattern} tree
* @return {ParseTree}
*/

View File

@ -4,7 +4,9 @@ import {
import {
ClassFieldDeclaration,
PropertyConstructorAssignment
PropertyConstructorAssignment,
NamedParameterList,
ObjectPatternBindingElement
} from '../syntax/trees/ParseTrees'
export class ParseTreeTransformer extends TraceurParseTreeTransformer {
@ -28,4 +30,21 @@ export class ParseTreeTransformer extends TraceurParseTreeTransformer {
tree.name, tree.parameterList, tree.typeAnnotation, tree.annotations, tree.body, tree.isConst,
initializerList);
}
}
transformNamedParameterList(tree) {
var nvPairs = this.transformList(tree.parameterNameAndValues);
if (nvPairs === tree.parameterNameAndValues) {
return tree;
}
return new NamedParameterList(tree.location, nvPairs);
}
transformObjectPatternBindingElement(tree) {
var binding = this.transformAny(tree.binding);
var initializer = this.transformAny(tree.initializer);
if (binding === tree.binding && initializer === tree.initializer) {
return tree;
}
return new ObjectPatternBindingElement(tree.location, binding, initializer);
}
}

View File

@ -323,8 +323,8 @@ export class DartParseTreeWriter extends JavaScriptParseTreeWriter {
this.writeSpace_()
}
visitNamedParamsExpression(tree) {
this.writeList_(tree.propertyNameAndValues, COMMA, false);
visitNamedParameterList(tree) {
this.writeList_(tree.parameterNameAndValues, COMMA, false);
}
toString() {

View File

@ -1,2 +1,4 @@
export var CLASS_FIELD_DECLARATION = 'CLASS_FIELD_DECLARATION';
export var PROPERTY_CONSTRUCTOR_ASSIGNMENT = 'PROPERTY_CONSTRUCTOR_ASSIGNMENT';
export var PROPERTY_CONSTRUCTOR_ASSIGNMENT = 'PROPERTY_CONSTRUCTOR_ASSIGNMENT';
export var NAMED_PARAMETER_LIST = 'NAMED_PARAMETER_LIST';
export var OBJECT_PATTERN_BINDING_ELEMENT = 'OBJECT_PATTERN_BINDING_ELEMENT';

View File

@ -4,9 +4,7 @@ import {PropertyMethodAssignment} from 'traceur/src/syntax/trees/ParseTrees';
import * as ParseTreeType from './ParseTreeType';
/**
* Property declaration
*/
// Class field declaration
export class ClassFieldDeclaration extends ParseTree {
constructor(location, lvalue, typeAnnotation, isFinal) {
this.location = location;
@ -30,9 +28,7 @@ export class ClassFieldDeclaration extends ParseTree {
var CLASS_FIELD_DECLARATION = ParseTreeType.CLASS_FIELD_DECLARATION;
/**
* Class constructor
*/
// Class constructor
export class PropertyConstructorAssignment extends PropertyMethodAssignment {
/**
* @param {SourceRange} location
@ -78,3 +74,73 @@ export class PropertyConstructorAssignment extends PropertyMethodAssignment {
var PROPERTY_CONSTRUCTOR_ASSIGNMENT = ParseTreeType.PROPERTY_CONSTRUCTOR_ASSIGNMENT;
// Named parameters
export class NamedParameterList extends ParseTree {
/**
* @param {SourceRange} location
* @param {Array.<ParseTree>} parameterNameAndValues
*/
constructor(location, parameterNameAndValues) {
this.location = location;
this.parameterNameAndValues = parameterNameAndValues;
}
/**
* @param {ParseTreeTransformer} transformer
*/
transform(transformer) {
return transformer.transformNamedParameterList(this);
}
/**
* @param {ParseTreeVisitor} visitor
*/
visit(visitor) {
visitor.visitNamedParameterList(this);
}
/**
* @type {ParseTreeType}
*/
get type() {
return NAMED_PARAMETER_LIST;
}
}
var NAMED_PARAMETER_LIST = ParseTreeType.NAMED_PARAMETER_LIST;
// Object pattern binding element
export class ObjectPatternBindingElement extends ParseTree {
/**
* @param {SourceRange} location
* @param {BindingIdentifier|ObjectPattern|ArrayPattern} binding
* @param {ParseTree} initializer
*/
constructor(location, binding, initializer) {
this.location = location;
this.binding = binding;
this.initializer = initializer;
}
/**
* @param {ParseTreeTransformer} transformer
*/
transform(transformer) {
return transformer.transformObjectPatternBindingElement(this);
}
/**
* @param {ParseTreeVisitor} visitor
*/
visit(visitor) {
visitor.visitObjectPatternBindingElement(this);
}
/**
* @type {ParseTreeType}
*/
get type() {
return OBJECT_PATTERN_BINDING_ELEMENT;
}
}
var OBJECT_PATTERN_BINDING_ELEMENT = ParseTreeType.OBJECT_PATTERN_BINDING_ELEMENT;