Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80540 views
1
/**
2
* Copyright 2014 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
/*global exports:true*/
18
19
var Syntax = require('esprima-fb').Syntax;
20
var utils = require('../src/utils');
21
var reserverdWordsHelper = require('./reserved-words-helper');
22
23
/**
24
* Code adapted from https://github.com/spicyj/es3ify
25
* The MIT License (MIT)
26
* Copyright (c) 2014 Ben Alpert
27
*/
28
29
function visitProperty(traverse, node, path, state) {
30
utils.catchup(node.key.range[0], state);
31
utils.append('"', state);
32
utils.catchup(node.key.range[1], state);
33
utils.append('"', state);
34
utils.catchup(node.value.range[0], state);
35
traverse(node.value, path, state);
36
return false;
37
}
38
39
visitProperty.test = function(node) {
40
return node.type === Syntax.Property &&
41
node.key.type === Syntax.Identifier &&
42
!node.method &&
43
!node.shorthand &&
44
!node.computed &&
45
reserverdWordsHelper.isES3ReservedWord(node.key.name);
46
};
47
48
function visitMemberExpression(traverse, node, path, state) {
49
traverse(node.object, path, state);
50
utils.catchup(node.property.range[0] - 1, state);
51
utils.append('[', state);
52
utils.catchupWhiteSpace(node.property.range[0], state);
53
utils.append('"', state);
54
utils.catchup(node.property.range[1], state);
55
utils.append('"]', state);
56
return false;
57
}
58
59
visitMemberExpression.test = function(node) {
60
return node.type === Syntax.MemberExpression &&
61
node.property.type === Syntax.Identifier &&
62
reserverdWordsHelper.isES3ReservedWord(node.property.name);
63
};
64
65
exports.visitorList = [
66
visitProperty,
67
visitMemberExpression
68
];
69
70