Skip to content

Commit

Permalink
Replace the uglify-js 1 ast format with estree
Browse files Browse the repository at this point in the history
  • Loading branch information
papandreou committed Sep 1, 2018
1 parent ece94c0 commit 8d396e7
Show file tree
Hide file tree
Showing 9 changed files with 669 additions and 98 deletions.
143 changes: 125 additions & 18 deletions lib/CldrPluralRule.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,60 +10,167 @@ function rangeListToJavaScriptAst(rangeListNode, lhsJavaScriptAst, withinSemanti
var range = rangeListNode.ranges[i],
itemJavaScriptAst;
if (range.type === 'number') {
itemJavaScriptAst = ['binary', '===', lhsJavaScriptAst, ['num', range.value]];
itemJavaScriptAst = {
type: 'BinaryExpression',
operator: '===',
left: lhsJavaScriptAst,
right: {
type: 'Literal',
value: range.value
}
};
} else {
// range.type === 'range'
seenRange = true;
itemJavaScriptAst = ['binary', '&&', ['binary', '>=', lhsJavaScriptAst, ['num', range.min.value]],
['binary', '<=', lhsJavaScriptAst, ['num', range.max.value]]];
itemJavaScriptAst = {
type: 'LogicalExpression',
operator: '&&',
left: {
type: 'BinaryExpression',
operator: '>=',
left: lhsJavaScriptAst,
right: {
type: 'Literal',
value: range.min.value
}
},
right: {
type: 'BinaryExpression',
operator: '<=',
left: lhsJavaScriptAst,
right: {
type: 'Literal',
value: range.max.value
}
}
};
}
if (javaScriptAst) {
javaScriptAst = ['binary', '||', itemJavaScriptAst, javaScriptAst];
javaScriptAst = {
type: 'LogicalExpression',
operator: '||',
left: itemJavaScriptAst,
right: javaScriptAst
};
} else {
javaScriptAst = itemJavaScriptAst;
}
}
if (seenRange && !withinSemantics) {
javaScriptAst = ['binary', '&&', ['binary', '===', lhsJavaScriptAst,
['call', ['dot', ['name', 'Math'], 'floor'], [lhsJavaScriptAst]]],
javaScriptAst];
javaScriptAst = {
type: 'LogicalExpression',
operator: '&&',
left: {
type: 'BinaryExpression',
operator: '===',
left: lhsJavaScriptAst,
right: {
type: 'CallExpression',
callee: {
type: 'MemberExpression',
computed: false,
object: {
type: 'Identifier',
name: 'Math'
},
property: {
type: 'Identifier',
name: 'floor'
}
},
arguments: [lhsJavaScriptAst]
}
},
right: javaScriptAst
};
}
return javaScriptAst;
}

function nodeToJavaScriptAst(node) {
switch (node.type) {
case 'number':
return ['num', node.value];
return {
type: 'Literal',
value: node.value
};
case 'n':
case 'i':
case 'v':
case 'w':
case 'f':
case 't':
return ['name', node.type];
return {
type: 'Identifier',
name: node.type
};
case 'is':
return ['binary', '==='].concat(node.operands.map(nodeToJavaScriptAst));
return {
type: 'BinaryExpression',
operator: '===',
left: nodeToJavaScriptAst(node.operands[0]),
right: nodeToJavaScriptAst(node.operands[1])
};
case 'isnot':
return ['binary', '!=='].concat(node.operands.map(nodeToJavaScriptAst));
return {
type: 'BinaryExpression',
operator: '!==',
left: nodeToJavaScriptAst(node.operands[0]),
right: nodeToJavaScriptAst(node.operands[1])
};
case 'mod':
return ['binary', '%'].concat(node.operands.map(nodeToJavaScriptAst));
return {
type: 'BinaryExpression',
operator: '%',
left: nodeToJavaScriptAst(node.operands[0]),
right: nodeToJavaScriptAst(node.operands[1])
};
case 'and':
return ['binary', '&&'].concat(node.operands.map(nodeToJavaScriptAst));
return {
type: 'BinaryExpression',
operator: '&&',
left: nodeToJavaScriptAst(node.operands[0]),
right: nodeToJavaScriptAst(node.operands[1])
};
case 'or':
return ['binary', '||'].concat(node.operands.map(nodeToJavaScriptAst));
return {
type: 'BinaryExpression',
operator: '||',
left: nodeToJavaScriptAst(node.operands[0]),
right: nodeToJavaScriptAst(node.operands[1])
};
case 'not':
return ['unary-prefix', '!', nodeToJavaScriptAst(node.operands)];
return {
type: 'UnaryExpression',
operator: '!',
prefix: true,
argument: nodeToJavaScriptAst(node.operands)
};
case 'isnot':
return ['binary', '!=='].concat(node.operands.map(nodeToJavaScriptAst));
return {
type: 'BinaryExpression',
operator: '!==',
left: nodeToJavaScriptAst(node.operands[0]),
right: nodeToJavaScriptAst(node.operands[1])
};
case 'within':
return rangeListToJavaScriptAst(node.operands[1], nodeToJavaScriptAst(node.operands[0]), true);
case 'notwithin':
return ['unary-prefix', '!', rangeListToJavaScriptAst(node.operands[1], nodeToJavaScriptAst(node.operands[0]), true)];
return {
type: 'UnaryExpression',
operator: '!',
prefix: true,
argument: rangeListToJavaScriptAst(node.operands[1], nodeToJavaScriptAst(node.operands[0]), true)
};
case 'in':
return rangeListToJavaScriptAst(node.operands[1], nodeToJavaScriptAst(node.operands[0]), false);
case 'notin':
return ['unary-prefix', '!', rangeListToJavaScriptAst(node.operands[1], nodeToJavaScriptAst(node.operands[0]), false)];
return {
type: 'UnaryExpression',
operator: '!',
prefix: false,
argument: rangeListToJavaScriptAst(node.operands[1], nodeToJavaScriptAst(node.operands[0]), false)
};
default:
throw new Error('nodeToJavaScriptAst: Unknown node type: ' + node.type);
}
Expand Down
106 changes: 80 additions & 26 deletions lib/CldrPluralRuleSet.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var CldrPluralRule = require('./CldrPluralRule'),
cldrPluralRuleTermFunctionByName = require('./cldrPluralRuleTermFunctionByName'),
uglifyJs = require('uglify-js');
esprima = require('esprima');

function CldrPluralRuleSet() {
this.cldrPluralRuleByCount = {};
Expand Down Expand Up @@ -35,44 +35,98 @@ CldrPluralRuleSet.prototype = {
Object.keys(this.cldrPluralRuleByCount).forEach(function (count) {
var cldrPluralRule = this.cldrPluralRuleByCount[count];
cldrPluralRule.updateIsUsedByTerm(isUsedByTerm);
statementAsts.push(
[
'if',
cldrPluralRule.toJavaScriptAst(),
['return', ['string', count]]
]
);
statementAsts.push({
type: 'IfStatement',
test: cldrPluralRule.toJavaScriptAst(),
consequent: {
type: 'ReturnStatement',
argument: {
type: 'Literal',
value: count
}
}
});
}, this);
statementAsts.push(['return', ['string', 'other']]);
statementAsts.push({
type: 'ReturnStatement',
argument: {
type: 'Literal',
value: 'other'
}
});
var varAsts = [];

['i', 'v', 'w', 'f', 't'].forEach(function (term) {
if (isUsedByTerm[term]) {
varAsts.push([term, uglifyJs.parser.parse(cldrPluralRuleTermFunctionByName[term].toString())[1][0][3][0][1]]);
varAsts.push({
type: 'VariableDeclarator',
id: {
type: 'Identifier',
name: term
},
init: esprima.parse(cldrPluralRuleTermFunctionByName[term].toString()).body[0].body.body[0].argument
});
}
});

if (Object.keys(isUsedByTerm).length !== 0) {
statementAsts.unshift(
statementAsts.unshift({
// if (typeof n === 'string') n = parseInt(n, 10);
[ 'if',
[ 'binary',
'===',
[ 'unary-prefix', 'typeof', [ 'name', 'n' ] ],
[ 'string', 'string' ] ],
[ 'stat',
[ 'assign',
true,
[ 'name', 'n' ],
[ 'call',
[ 'name', 'parseInt' ],
[ [ 'name', 'n' ], [ 'num', 10 ] ] ] ] ],
undefined ]
);
type: 'IfStatement',
test: {
type: 'BinaryExpression',
operator: '===',
left: {
type: 'UnaryExpression',
operator: 'typeof',
prefix: true,
argument: {
type: 'Identifier',
name: 'n'
}
},
right: {
type: 'Literal',
value: 'string'
}
},
consequent: {
type: 'ExpressionStatement',
expression: {
type: 'AssignmentExpression',
operator: '=',
left: {
type: 'Identifier',
name: 'n'
},
right: {
type: 'CallExpression',
callee: {
type: 'Identifier',
name: 'parseInt'
},
arguments: [
{
type: 'Identifier',
name: 'n'
},
{
type: 'Literal',
value: 10
}
]
}
}
}
});
}

if (varAsts.length > 0) {
statementAsts.unshift(['var', varAsts]);
statementAsts.unshift({
type: 'VariableDeclaration',
kind: 'var',
declarations: varAsts
});
}
return statementAsts;
}
Expand Down

0 comments on commit 8d396e7

Please sign in to comment.