Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80542 views
1
/**
2
* Copyright 2013 Facebook, Inc.
3
*
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
7
*
8
* http://www.apache.org/licenses/LICENSE-2.0
9
*
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
15
*/
16
17
/*jslint node:true*/
18
19
/**
20
* Desugars concise methods of objects to function expressions.
21
*
22
* var foo = {
23
* method(x, y) { ... }
24
* };
25
*
26
* var foo = {
27
* method: function(x, y) { ... }
28
* };
29
*
30
*/
31
32
var Syntax = require('esprima-fb').Syntax;
33
var utils = require('../src/utils');
34
var reservedWordsHelper = require('./reserved-words-helper');
35
36
function visitObjectConciseMethod(traverse, node, path, state) {
37
var isGenerator = node.value.generator;
38
if (isGenerator) {
39
utils.catchupWhiteSpace(node.range[0] + 1, state);
40
}
41
if (node.computed) { // [<expr>]() { ...}
42
utils.catchup(node.key.range[1] + 1, state);
43
} else if (reservedWordsHelper.isReservedWord(node.key.name)) {
44
utils.catchup(node.key.range[0], state);
45
utils.append('"', state);
46
utils.catchup(node.key.range[1], state);
47
utils.append('"', state);
48
}
49
50
utils.catchup(node.key.range[1], state);
51
utils.append(
52
':function' + (isGenerator ? '*' : ''),
53
state
54
);
55
path.unshift(node);
56
traverse(node.value, path, state);
57
path.shift();
58
return false;
59
}
60
61
visitObjectConciseMethod.test = function(node, path, state) {
62
return node.type === Syntax.Property &&
63
node.value.type === Syntax.FunctionExpression &&
64
node.method === true;
65
};
66
67
exports.visitorList = [
68
visitObjectConciseMethod
69
];
70
71