getProp-parser-test.js
4.87 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/* eslint-env mocha */
import assert from 'assert';
import entries from 'object.entries';
import fromEntries from 'object.fromentries';
import { getOpeningElement, setParserName, fallbackToBabylon } from '../helper';
import getProp from '../../src/getProp';
const literal = {
source: '<div {...{ id: "foo" }} />',
target: '<div id="foo" />',
offset: { keyOffset: -6, valueOffset: -7 },
};
const expression1 = {
source: '<div {...{ id }} />',
target: '<div id={id} />',
offset: { keyOffset: -6, valueOffset: -2 },
};
const expression2 = {
source: '<div {...{ id: `foo${bar}baz` }} />', // eslint-disable-line no-template-curly-in-string
target: '<div id={`foo${bar}baz`} />', // eslint-disable-line no-template-curly-in-string
offset: { keyOffset: -6, valueOffset: -6 },
};
describe('getProp', () => {
it('should create the correct AST for literal with flow parser', () => {
actualTest('flow', literal);
});
it('should create the correct AST for literal with babel parser', () => {
actualTest('babel', literal);
});
it('should create the correct AST for expression with flow parser (1)', () => {
actualTest('flow', expression1);
});
it('should create the correct AST for expression with babel parser (1)', () => {
actualTest('babel', expression1);
});
it('should create the correct AST for expression with flow parser (2)', () => {
actualTest('flow', expression2);
});
it('should create the correct AST for expression with babel parser (2)', () => {
actualTest('babel', expression2);
});
});
function actualTest(parserName, test) {
setParserName(parserName);
const { source, target, offset } = test;
const sourceProps = stripConstructors(getOpeningElement(source).attributes);
const targetProps = stripConstructors(getOpeningElement(target).attributes);
const prop = 'id';
const sourceResult = getProp(sourceProps, prop);
const targetResult = getProp(targetProps, prop);
if (fallbackToBabylon && parserName === 'babel' && test === literal) {
// Babylon (node < 6) adds an `extra: null` prop to a literal if it is parsed from a
// JSXAttribute, other literals don't get this.
sourceResult.value.extra = null;
}
assert.deepStrictEqual(
adjustLocations(sourceResult, offset),
adjustRange(targetResult),
);
}
function adjustRange({ name, value: { expression, ...value }, ...node }) {
return {
...adjustNodeRange(node),
name: adjustNodeRange(name),
value: {
...adjustNodeRange(value),
...(expression ? { expression: adjustNodeRangeRecursively(expression) } : {}),
},
};
}
function adjustNodeRange(node) {
if (!node.loc) {
return node;
}
const [start, end] = node.range || [node.start, node.end];
return {
...node,
end: undefined,
range: [start, end],
start: undefined,
};
}
function adjustNodeRangeRecursively(node) {
if (Array.isArray(node)) {
return node.map(adjustNodeRangeRecursively);
}
if (node && typeof node === 'object') {
return adjustNodeRange(mapValues(node, adjustNodeRangeRecursively));
}
return node;
}
function stripConstructors(value) {
return JSON.parse(JSON.stringify(value));
}
function adjustLocations(node, { keyOffset, valueOffset }) {
const hasExpression = !!node.value.expression;
return {
...adjustNodeLocations(node, {
startOffset: keyOffset,
endOffset: valueOffset + (hasExpression ? 1 : 0),
}),
name: adjustNodeLocations(node.name, { startOffset: keyOffset, endOffset: keyOffset }),
value: {
...adjustNodeLocations(node.value, {
startOffset: valueOffset - (hasExpression ? 1 : 0),
endOffset: valueOffset + (hasExpression ? 1 : 0),
}),
...(hasExpression
? {
expression: adjustLocationsRecursively(
node.value.expression,
{ startOffset: valueOffset, endOffset: valueOffset },
),
}
: {}
),
},
};
}
function adjustNodeLocations(node, { startOffset, endOffset }) {
if (!node.loc) {
return node;
}
const [start, end] = node.range || [];
return {
...node,
end: undefined,
loc: {
...node.loc,
start: {
...node.loc.start,
column: node.loc.start.column + startOffset,
},
end: {
...node.loc.end,
column: node.loc.end.column + endOffset,
},
},
range: [start + startOffset, end + endOffset],
start: undefined,
};
}
function adjustLocationsRecursively(node, { startOffset, endOffset }) {
if (Array.isArray(node)) {
return node.map((x) => adjustLocationsRecursively(x, { startOffset, endOffset }));
}
if (node && typeof node === 'object') {
return adjustNodeLocations(
mapValues(node, (x) => adjustLocationsRecursively(x, { startOffset, endOffset })),
{ startOffset, endOffset },
);
}
return node;
}
function mapValues(o, f) {
return fromEntries(entries(o).map(([k, v]) => [k, f(v)]));
}