Last change
on this file since ceaed42 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
5.2 KB
|
Line | |
---|
1 | /**
|
---|
2 | * @license
|
---|
3 | * Copyright Google LLC All Rights Reserved.
|
---|
4 | *
|
---|
5 | * Use of this source code is governed by an MIT-style license that can be
|
---|
6 | * found in the LICENSE file at https://angular.io/license
|
---|
7 | */
|
---|
8 | /**
|
---|
9 | * A path is an ordered set of elements. Typically a path is to a
|
---|
10 | * particular offset in a source file. The head of the list is the top
|
---|
11 | * most node. The tail is the node that contains the offset directly.
|
---|
12 | *
|
---|
13 | * For example, the expression `a + b + c` might have an ast that looks
|
---|
14 | * like:
|
---|
15 | * +
|
---|
16 | * / \
|
---|
17 | * a +
|
---|
18 | * / \
|
---|
19 | * b c
|
---|
20 | *
|
---|
21 | * The path to the node at offset 9 would be `['+' at 1-10, '+' at 7-10,
|
---|
22 | * 'c' at 9-10]` and the path the node at offset 1 would be
|
---|
23 | * `['+' at 1-10, 'a' at 1-2]`.
|
---|
24 | */
|
---|
25 | export class AstPath {
|
---|
26 | constructor(path, position = -1) {
|
---|
27 | this.path = path;
|
---|
28 | this.position = position;
|
---|
29 | }
|
---|
30 | get empty() {
|
---|
31 | return !this.path || !this.path.length;
|
---|
32 | }
|
---|
33 | get head() {
|
---|
34 | return this.path[0];
|
---|
35 | }
|
---|
36 | get tail() {
|
---|
37 | return this.path[this.path.length - 1];
|
---|
38 | }
|
---|
39 | parentOf(node) {
|
---|
40 | return node && this.path[this.path.indexOf(node) - 1];
|
---|
41 | }
|
---|
42 | childOf(node) {
|
---|
43 | return this.path[this.path.indexOf(node) + 1];
|
---|
44 | }
|
---|
45 | first(ctor) {
|
---|
46 | for (let i = this.path.length - 1; i >= 0; i--) {
|
---|
47 | let item = this.path[i];
|
---|
48 | if (item instanceof ctor)
|
---|
49 | return item;
|
---|
50 | }
|
---|
51 | }
|
---|
52 | push(node) {
|
---|
53 | this.path.push(node);
|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