forked from amilajack/eslint-plugin-flowtype-errors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get-program.js
63 lines (55 loc) · 1.29 KB
/
get-program.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
// @flow
type Pos = {
line: number,
column: number
};
export type Loc = {
start: Pos,
end: Pos
};
export type Program = { text: string, loc: Loc, offset: Pos };
export default function(source: Object, node: Object): ?Program {
// Ignore if body is empty.
if (node.body.length === 0) {
return;
}
const body0 = node.body[0];
const comments0 = node.comments[0];
let start;
let range;
// With babel-eslint, program.loc.start.column is always 0,
// workaround it by using the first node of the body to get the offset.
if (comments0) {
start =
node.range[0] < comments0.range[0]
? {
line: body0.loc.start.line,
column: body0.loc.start.column
}
: {
line: comments0.loc.start.line,
column: comments0.loc.start.column
};
range = [
Math.min(node.range[0], comments0.range[0]),
Math.max(node.range[1], node.comments[node.comments.length - 1].range[1])
];
} else {
start = {
line: body0.loc.start.line,
column: body0.loc.start.column
};
range = node.range;
}
return {
text: source.text.slice(range[0], range[1]),
loc: {
start,
end: start
},
offset: {
line: start.line - 1,
column: start.column
}
};
}