-
Notifications
You must be signed in to change notification settings - Fork 46k
/
react.js
141 lines (117 loc) · 4.05 KB
/
react.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
* Copyright 2013 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*global exports:true*/
"use strict";
var Syntax = require('esprima-fb').Syntax;
var utils = require('jstransform/src/utils');
function visitReactTag(traverse, object, path, state) {
object.attributes.forEach(function(attr, index) {
traverse(attr.value, path, state);
});
object.children.forEach(function(child, index) {
if (child.type === Syntax.Literal) {
codemodXJSLiteral(child, state);
} else if (child.type === Syntax.XJSExpressionContainer) {
var isNotAfterLiteral =
index == 0 ||
object.children[index - 1].type !== Syntax.Literal;
var isNotBeforeLiteral =
index == object.children.length - 1 ||
object.children[index + 1].type !== Syntax.Literal;
codemodXJSExpressionContainer(
traverse,
child,
isNotAfterLiteral,
isNotBeforeLiteral,
path,
state
);
} else {
traverse(child, path, state);
}
});
return false;
}
visitReactTag.test = function(object, path, state) {
// only run react when react @jsx namespace is specified in docblock
var jsx = utils.getDocblock(state).jsx;
return object.type === Syntax.XJSElement && jsx && jsx.length;
};
function codemodXJSLiteral(object, state) {
var value = object.raw;
utils.catchup(object.range[0], state);
/*
This can be used to "annotate spaces" inserted by this transformation,
so that they can be more easily recognized as such in the final code
{' '}
{'\\x20'}
{(' ')}
{' '||0}
{' '||AnyTextYouLike}
{' '||'AnyTextYouLike'}
{GlobalVariableWithASpace}
*/
var space = "{' '}";
/*
· space
¬ newline
{expr} = <tag>
Old whitespace rules:
{1}··Aaa··Bbb··{2}··{3} → {1}·Aaa··Bbb·{2}{3}
{1}¬¬Aaa¬¬Bbb¬¬{2}¬¬{3} → {1}·Aaa·Bbb·{2}{3}
New whitespace rules:
{1}··Aaa··Bbb··{2}··{3} → {1}··Aaa··Bbb··{2}··{3}
{1}¬¬Aaa¬¬Bbb¬¬{2}¬¬{3} → {1}Aaa·Bbb{2}{3}
Required transformation:
{1}··{2} = {1}··{2} → {1}{2}
{1}··Aaa··{2} = {1}··Aaa··{2} → {1}·Aaa·{2}
{1}¬¬Aaa¬¬{2} = {1}Aaa{2} → {1}·Aaa·{2}
*/
// {1}··{2} = {1}··{2} → {1}{2}
value = value.replace(/^[ \t]+$/, '');
// {1}··Aaa··{2} = {1}··Aaa··{2} → {1}·Aaa·{2}
value = value.replace(/^[ \t]+([^ \t\r\n])/, " $1");
value = value.replace(/([^ \t\r\n])[ \t]+$/, "$1 ");
// {1}¬¬Aaa¬¬{2} = {1}Aaa{2} → {1}·Aaa·{2}
value = value.replace(/^([ \t]*[\r\n][ \t\r\n]*)([^ \t\r\n].*)/, "$1" + space + "$2");
value = value.replace(/([^ \t\r\n])([ \t]*[\r\n][ \t\r\n]*)$/, "$1" + space + "$2");
// Rendered whitespace tabs are replaced with spaces
value = value.replace(/[^ \t\r\n][ ]*[\t][ \t]*[^ \t\r\n]/, function(match) {
return match.replace(/\t/g, ' ');
});
utils.append(value, state);
utils.move(object.range[1], state);
}
function codemodXJSExpressionContainer(
traverse,
object,
isNotAfterLiteral,
isNotBeforeLiteral,
path,
state) {
utils.catchup(object.range[0], state);
traverse(object.expression, path, state);
// Unbox the previously required {' '}-workaround
var raw = object.expression.raw;
var isSpace = raw === "' '" || raw === '" "';
if (isNotAfterLiteral && isNotBeforeLiteral && isSpace) {
utils.append(' ', state);
utils.move(object.range[1], state);
} else {
utils.catchup(object.range[1], state);
}
}
exports.visitReactTag = visitReactTag;