This repository has been archived by the owner on Jan 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 66
/
es6-object-concise-method-visitors.js
70 lines (62 loc) · 2.02 KB
/
es6-object-concise-method-visitors.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
* Copyright 2013-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
/*jslint node:true*/
/**
* Desugars concise methods of objects to function expressions.
*
* var foo = {
* method(x, y) { ... }
* };
*
* var foo = {
* method: function(x, y) { ... }
* };
*
*/
var Syntax = require('esprima-fb').Syntax;
var utils = require('../src/utils');
var reservedWordsHelper = require('./reserved-words-helper');
function visitObjectConciseMethod(traverse, node, path, state) {
var isGenerator = node.value.generator;
if (isGenerator) {
utils.catchupWhiteSpace(node.range[0] + 1, state);
}
if (reservedWordsHelper.isReservedWord(node.key.name)) {
utils.catchup(node.key.range[0], state);
utils.append('"', state);
utils.catchup(node.key.range[1], state);
utils.append('"', state);
}
utils.catchup(node.key.range[1], state);
utils.append(':', state);
renderConciseMethod(traverse, node, path, state);
return false;
}
// This method is also used by es6-object-computed-property-visitor to render
// the method for concise computed properties.
function renderConciseMethod(traverse, property, path, state) {
if (property.computed) {
var closingSquareBracketIndex = state.g.source.indexOf(']', property.key.range[1]);
utils.catchup(closingSquareBracketIndex, state);
utils.move(closingSquareBracketIndex + 1, state);
}
utils.append("function" + (property.value.generator ? "*" : ""), state);
path.unshift(property);
traverse(property.value, path, state);
path.shift();
}
visitObjectConciseMethod.test = function(node, path, state) {
return node.type === Syntax.Property &&
node.value.type === Syntax.FunctionExpression &&
node.method === true;
};
exports.renderConciseMethod = renderConciseMethod;
exports.visitorList = [
visitObjectConciseMethod
];