es6-object-short-notation-visitors.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /**
  2. * Copyright 2013-present, Facebook, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. */
  9. /*jslint node: true*/
  10. /**
  11. * Desugars ES6 Object Literal short notations into ES3 full notation.
  12. *
  13. * // Easier return values.
  14. * function foo(x, y) {
  15. * return {x, y}; // {x: x, y: y}
  16. * };
  17. *
  18. * // Destructuring.
  19. * function init({port, ip, coords: {x, y}}) { ... }
  20. *
  21. */
  22. var Syntax = require('esprima-fb').Syntax;
  23. var utils = require('../src/utils');
  24. /**
  25. * @public
  26. */
  27. function visitObjectLiteralShortNotation(traverse, node, path, state) {
  28. utils.catchup(node.key.range[1], state);
  29. utils.append(':' + node.key.name, state);
  30. return false;
  31. }
  32. visitObjectLiteralShortNotation.test = function(node, path, state) {
  33. return node.type === Syntax.Property &&
  34. node.kind === 'init' &&
  35. node.shorthand === true &&
  36. path[0].type !== Syntax.ObjectPattern;
  37. };
  38. exports.visitorList = [
  39. visitObjectLiteralShortNotation
  40. ];