react / wstein / node_modules / react / node_modules / envify / node_modules / jstransform / visitors / es6-object-concise-method-visitors.js
80542 views/**1* Copyright 2013 Facebook, Inc.2*3* Licensed under the Apache License, Version 2.0 (the "License");4* you may not use this file except in compliance with the License.5* You may obtain a copy of the License at6*7* http://www.apache.org/licenses/LICENSE-2.08*9* Unless required by applicable law or agreed to in writing, software10* distributed under the License is distributed on an "AS IS" BASIS,11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12* See the License for the specific language governing permissions and13* limitations under the License.14*/1516/*jslint node:true*/1718/**19* Desugars concise methods of objects to function expressions.20*21* var foo = {22* method(x, y) { ... }23* };24*25* var foo = {26* method: function(x, y) { ... }27* };28*29*/3031var Syntax = require('esprima-fb').Syntax;32var utils = require('../src/utils');33var reservedWordsHelper = require('./reserved-words-helper');3435function visitObjectConciseMethod(traverse, node, path, state) {36var isGenerator = node.value.generator;37if (isGenerator) {38utils.catchupWhiteSpace(node.range[0] + 1, state);39}40if (node.computed) { // [<expr>]() { ...}41utils.catchup(node.key.range[1] + 1, state);42} else if (reservedWordsHelper.isReservedWord(node.key.name)) {43utils.catchup(node.key.range[0], state);44utils.append('"', state);45utils.catchup(node.key.range[1], state);46utils.append('"', state);47}4849utils.catchup(node.key.range[1], state);50utils.append(51':function' + (isGenerator ? '*' : ''),52state53);54path.unshift(node);55traverse(node.value, path, state);56path.shift();57return false;58}5960visitObjectConciseMethod.test = function(node, path, state) {61return node.type === Syntax.Property &&62node.value.type === Syntax.FunctionExpression &&63node.method === true;64};6566exports.visitorList = [67visitObjectConciseMethod68];697071