UNPKG

4.09 kBJavaScriptView Raw
1"use strict";
2/**
3 * @license
4 * Copyright (c) 2016 The Polymer Project Authors. All rights reserved.
5 * This code may only be used under the BSD style license found at
6 * http://polymer.github.io/LICENSE.txt
7 * The complete set of authors may be found at
8 * http://polymer.github.io/AUTHORS.txt
9 * The complete set of contributors may be found at
10 * http://polymer.github.io/CONTRIBUTORS.txt
11 * Code distributed by Google as part of the polymer project is also
12 * subject to an additional IP rights grant found at
13 * http://polymer.github.io/PATENTS.txt
14 */
15Object.defineProperty(exports, "__esModule", { value: true });
16function correctSourceRange(sourceRange, locationOffset) {
17 if (!locationOffset || !sourceRange) {
18 return sourceRange;
19 }
20 return {
21 file: locationOffset.filename || sourceRange.file,
22 start: correctPosition(sourceRange.start, locationOffset),
23 end: correctPosition(sourceRange.end, locationOffset)
24 };
25}
26exports.correctSourceRange = correctSourceRange;
27function correctPosition(position, locationOffset) {
28 return {
29 line: position.line + locationOffset.line,
30 column: position.column + (position.line === 0 ? locationOffset.col : 0)
31 };
32}
33exports.correctPosition = correctPosition;
34function uncorrectSourceRange(sourceRange, locationOffset) {
35 if (locationOffset == null || sourceRange == null) {
36 return sourceRange;
37 }
38 return {
39 file: locationOffset.filename || sourceRange.file,
40 start: uncorrectPosition(sourceRange.start, locationOffset),
41 end: uncorrectPosition(sourceRange.end, locationOffset),
42 };
43}
44exports.uncorrectSourceRange = uncorrectSourceRange;
45function uncorrectPosition(position, locationOffset) {
46 const line = position.line - locationOffset.line;
47 return {
48 line: line,
49 column: position.column - (line === 0 ? locationOffset.col : 0)
50 };
51}
52exports.uncorrectPosition = uncorrectPosition;
53/**
54 * Returns -1 if source position `a` comes before source position `b`, returns 0
55 * if they are the same, returns 1 if `a` comes after `b`.
56 */
57function comparePosition(a, b) {
58 if (a.line !== b.line) {
59 return a.line < b.line ? -1 : 1;
60 }
61 if (a.column !== b.column) {
62 return a.column < b.column ? -1 : 1;
63 }
64 return 0;
65}
66exports.comparePosition = comparePosition;
67/**
68 * If the position is inside the range, returns 0. If it comes before the range,
69 * it returns -1. If it comes after the range, it returns 1.
70 *
71 * TODO(rictic): test this method directly (currently most of its tests are
72 * indirectly, through ast-from-source-position).
73 */
74function comparePositionAndRange(position, range, includeEdges) {
75 // Usually we want to include the edges of a range as part
76 // of the thing, but sometimes, e.g. for start and end tags,
77 // we'd rather not.
78 if (includeEdges == null) {
79 includeEdges = true;
80 }
81 if (includeEdges == null) {
82 includeEdges = true;
83 }
84 if (position.line < range.start.line) {
85 return -1;
86 }
87 if (position.line > range.end.line) {
88 return 1;
89 }
90 if (position.line === range.start.line) {
91 if (includeEdges) {
92 if (position.column < range.start.column) {
93 return -1;
94 }
95 }
96 else {
97 if (position.column <= range.start.column) {
98 return -1;
99 }
100 }
101 }
102 if (position.line === range.end.line) {
103 if (includeEdges) {
104 if (position.column > range.end.column) {
105 return 1;
106 }
107 }
108 else {
109 if (position.column >= range.end.column) {
110 return 1;
111 }
112 }
113 }
114 return 0;
115}
116exports.comparePositionAndRange = comparePositionAndRange;
117function isPositionInsideRange(position, range, includeEdges) {
118 if (!range) {
119 return false;
120 }
121 return comparePositionAndRange(position, range, includeEdges) === 0;
122}
123exports.isPositionInsideRange = isPositionInsideRange;
124//# sourceMappingURL=source-range.js.map
\No newline at end of file