BVB Source Codes

prettier Show emit.js Source code

Return Download prettier: download emit.js Source code - Download prettier Source code - Type:.js
  1. /**
  2.  * @flow
  3.  */
  4. import * as t from './jsAst';
  5.  
  6. const b = t.builders;
  7.  
  8. import type {
  9.         TypedNode
  10. } from './ast';
  11.  
  12. function getBinaryOp(op: 'plus' | 'minus' | 'divide' | 'multiply') : '+' | '-' | '*' | '/' {
  13.   switch (op) {
  14.   case 'plus':
  15.     return '+';
  16.   case 'minus':
  17.     return '-';
  18.   case 'divide':
  19.     return '/';
  20.   case 'multiply':
  21.     return '*';
  22.   default:
  23.     throw new Error('Invalid binary operator: ' + op);
  24.   }
  25. }
  26.  
  27. export function emitExpression(node: TypedNode) : t.Expression {
  28.   switch (node.exprNodeType) {
  29.   case 'string_literal': // FALLTHROUGH
  30.   case 'number':
  31.     return b.literal(node.value);
  32.   case 'variable':
  33.     return b.memberExpression(
  34.       b.identifier('vars'),
  35.       b.identifier(node.name),
  36.       false
  37.     );
  38.   case 'binary_op': {
  39.     const lhs = emitExpression(node.lhs);
  40.     const rhs = emitExpression(node.rhs);
  41.  
  42.     const op = getBinaryOp(node.binaryOp);
  43.     return b.binaryExpression(op, lhs, rhs);
  44.   }
  45.   case 'unary_minus': {
  46.     const operand = emitExpression(node.op);
  47.     return b.unaryExpression('-', operand, true);
  48.   }
  49.   case 'function_invocation': {
  50.     const callee = b.memberExpression(
  51.       b.identifier('fns'),
  52.       b.identifier(node.name),
  53.       false
  54.     );
  55.  
  56.     const args = node.parameters.map(
  57.       (n) => emitExpression(n)
  58.     );
  59.  
  60.     return b.callExpression(callee, args);
  61.   }
  62.   default:
  63.     throw new Error('Unknown expression type: ' + node.type);
  64.   }
  65. }
  66.  
downloademit.js Source code - Download prettier Source code
Related Source Codes/Software:
storybook - 2017-06-07
ionicons - The premium icon font for Ionic ... 2017-06-07
AsyncDisplayKit - Smooth asynchronous user interfaces for iOS apps. ... 2017-06-07
lottie-android - Render After Effects animations natively on Androi... 2017-06-07
parse-server - Parse-compatible API server module for Node/Expres... 2017-06-07
inferno - An extremely fast, React-like JavaScript library f... 2017-06-08
guetzli - Perceptual JPEG encoder 2017-06-08
cs-video-courses - List of Computer Science courses with video lectur... 2017-06-08
interviews - Everything you need to know to get the job. 2017-06-08
prepack - Prepack is a partial evaluator for JavaScript. Pre... 2017-06-08
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top