Front end of the Slack clone application.

index.js 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. var Syntax = require('esprima-fb').Syntax;
  2. var jstransform = require('jstransform');
  3. var through = require('through');
  4. var utils = require('jstransform/src/utils');
  5. var reserved = [
  6. "break", "case", "catch", "continue", "default", "delete", "do", "else",
  7. "finally", "for", "function", "if", "in", "instanceof", "new", "return",
  8. "switch", "this", "throw", "try", "typeof", "var", "void", "while", "with",
  9. "abstract", "boolean", "byte", "char", "class", "const", "debugger",
  10. "double", "enum", "export", "extends", "final", "float", "goto",
  11. "implements", "import", "int", "interface", "long", "native", "package",
  12. "private", "protected", "public", "short", "static", "super",
  13. "synchronized", "throws", "transient", "volatile",
  14. ];
  15. var reservedDict = {};
  16. reserved.forEach(function(k) {
  17. reservedDict[k] = true;
  18. });
  19. // In: x.class = 3;
  20. // Out: x["class"] = 3;
  21. function visitMemberExpression(traverse, node, path, state) {
  22. traverse(node.object, path, state);
  23. utils.catchup(node.object.range[1], state);
  24. utils.append('[', state);
  25. utils.catchupWhiteSpace(node.property.range[0], state);
  26. utils.append('"', state);
  27. utils.catchup(node.property.range[1], state);
  28. utils.append('"]', state);
  29. return false;
  30. }
  31. visitMemberExpression.test = function(node, path, state) {
  32. return node.type === Syntax.MemberExpression &&
  33. node.property.type === Syntax.Identifier &&
  34. reservedDict[node.property.name] === true;
  35. };
  36. // In: x = {class: 2};
  37. // Out: x = {"class": 2};
  38. function visitProperty(traverse, node, path, state) {
  39. utils.catchup(node.key.range[0], state);
  40. utils.append('"', state);
  41. utils.catchup(node.key.range[1], state);
  42. utils.append('"', state);
  43. utils.catchup(node.value.range[0], state);
  44. traverse(node.value, path, state);
  45. return false;
  46. }
  47. visitProperty.test = function(node, path, state) {
  48. return node.type === Syntax.Property &&
  49. node.key.type === Syntax.Identifier &&
  50. reservedDict[node.key.name] === true;
  51. };
  52. var reCommaOrComment = /,|\/\*.+?\*\/|\/\/[^\n]+/g;
  53. function stripComma(value) {
  54. return value.replace(reCommaOrComment, function(text) {
  55. if (text === ',') {
  56. return '';
  57. } else {
  58. // Preserve comments
  59. return text;
  60. }
  61. });
  62. }
  63. // In: [1, 2, 3,]
  64. // Out: [1, 2, 3]
  65. function visitArrayOrObjectExpression(traverse, node, path, state) {
  66. // Copy the opening '[' or '{'
  67. utils.catchup(node.range[0] + 1, state);
  68. var elements = node.type === Syntax.ArrayExpression ?
  69. node.elements :
  70. node.properties;
  71. elements.forEach(function(element, i) {
  72. if (element == null && i === elements.length - 1) {
  73. throw new Error(
  74. "Elisions ending an array are interpreted inconsistently " +
  75. "in IE8; remove the extra comma or use 'undefined' explicitly");
  76. }
  77. if (element != null) {
  78. // Copy commas from after previous element, if any
  79. utils.catchup(element.range[0], state);
  80. traverse(element, path, state);
  81. }
  82. });
  83. // Skip over a trailing comma, if any
  84. utils.catchup(node.range[1] - 1, state, stripComma);
  85. utils.catchup(node.range[1], state);
  86. return false;
  87. }
  88. visitArrayOrObjectExpression.test = function(node, path, state) {
  89. return node.type === Syntax.ArrayExpression ||
  90. node.type === Syntax.ObjectExpression;
  91. };
  92. var visitorList = [
  93. visitMemberExpression,
  94. visitProperty,
  95. visitArrayOrObjectExpression
  96. ];
  97. function transform(code) {
  98. return jstransform.transform(visitorList, code).code;
  99. }
  100. function process(file) {
  101. if (/\.json$/.test(file)) return through();
  102. var data = '';
  103. function write(chunk) {
  104. data += chunk;
  105. }
  106. function compile() {
  107. var source;
  108. try {
  109. source = transform(data);
  110. } catch (e) {
  111. return this.emit('error', e);
  112. }
  113. this.queue(source);
  114. this.queue(null);
  115. }
  116. return through(write, compile);
  117. }
  118. module.exports = process;
  119. module.exports.isReserved = function(word) {
  120. return reservedDict.hasOwnProperty(word) ? !!reservedDict[word] : false;
  121. };
  122. module.exports.transform = transform;
  123. module.exports.visitorList = visitorList;