1 | "use strict";
|
---|
2 | /**
|
---|
3 | * @license
|
---|
4 | * Copyright Google LLC All Rights Reserved.
|
---|
5 | *
|
---|
6 | * Use of this source code is governed by an MIT-style license that can be
|
---|
7 | * found in the LICENSE file at https://angular.io/license
|
---|
8 | */
|
---|
9 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
10 | exports.removeElementFromArrayExpression = exports.getParentSyntaxList = void 0;
|
---|
11 | const ts = require("typescript");
|
---|
12 | /**
|
---|
13 | * Retrieves the parent syntax list of the given node. A syntax list node is usually
|
---|
14 | * hidden from the default AST node hierarchy because it only contains information that
|
---|
15 | * is need when printing a node. e.g. it contains information about comma positions in
|
---|
16 | * an array literal expression.
|
---|
17 | */
|
---|
18 | function getParentSyntaxList(node) {
|
---|
19 | if (!node.parent) {
|
---|
20 | return null;
|
---|
21 | }
|
---|
22 | const parent = node.parent;
|
---|
23 | const { pos, end } = node;
|
---|
24 | for (const child of parent.getChildren()) {
|
---|
25 | if (child.pos > end || child === node) {
|
---|
26 | return null;
|
---|
27 | }
|
---|
28 | if (child.kind === ts.SyntaxKind.SyntaxList && child.pos <= pos && child.end >= end) {
|
---|
29 | return child;
|
---|
30 | }
|
---|
31 | }
|
---|
32 | return null;
|
---|
33 | }
|
---|
34 | exports.getParentSyntaxList = getParentSyntaxList;
|
---|
35 | /** Looks for the trailing comma of the given element within the syntax list. */
|
---|
36 | function findTrailingCommaToken(list, element) {
|
---|
37 | let foundElement = false;
|
---|
38 | for (let child of list.getChildren()) {
|
---|
39 | if (!foundElement && child === element) {
|
---|
40 | foundElement = true;
|
---|
41 | }
|
---|
42 | else if (foundElement) {
|
---|
43 | if (child.kind === ts.SyntaxKind.CommaToken) {
|
---|
44 | return child;
|
---|
45 | }
|
---|
46 | break;
|
---|
47 | }
|
---|
48 | }
|
---|
49 | return null;
|
---|
50 | }
|
---|
51 | /** Removes a given element from its parent array literal expression. */
|
---|
52 | function removeElementFromArrayExpression(element, recorder) {
|
---|
53 | recorder.remove(element.getFullStart(), element.getFullWidth());
|
---|
54 | const syntaxList = getParentSyntaxList(element);
|
---|
55 | if (!syntaxList) {
|
---|
56 | return;
|
---|
57 | }
|
---|
58 | // if there is a trailing comma token for the element, we need to remove it
|
---|
59 | // because otherwise the array literal expression will have syntax failures.
|
---|
60 | const trailingComma = findTrailingCommaToken(syntaxList, element);
|
---|
61 | if (trailingComma !== null) {
|
---|
62 | recorder.remove(trailingComma.getFullStart(), trailingComma.getFullWidth());
|
---|
63 | }
|
---|
64 | }
|
---|
65 | exports.removeElementFromArrayExpression = removeElementFromArrayExpression;
|
---|
66 | //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicmVtb3ZlLWFycmF5LWVsZW1lbnQuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi8uLi8uLi8uLi8uLi8uLi8uLi9zcmMvbWF0ZXJpYWwvc2NoZW1hdGljcy9uZy11cGRhdGUvbWlncmF0aW9ucy9oYW1tZXItZ2VzdHVyZXMtdjkvcmVtb3ZlLWFycmF5LWVsZW1lbnQudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBOzs7Ozs7R0FNRzs7O0FBR0gsaUNBQWlDO0FBRWpDOzs7OztHQUtHO0FBQ0gsU0FBZ0IsbUJBQW1CLENBQUMsSUFBYTtJQUMvQyxJQUFJLENBQUMsSUFBSSxDQUFDLE1BQU0sRUFBRTtRQUNoQixPQUFPLElBQUksQ0FBQztLQUNiO0lBQ0QsTUFBTSxNQUFNLEdBQUcsSUFBSSxDQUFDLE1BQU0sQ0FBQztJQUMzQixNQUFNLEVBQUMsR0FBRyxFQUFFLEdBQUcsRUFBQyxHQUFHLElBQUksQ0FBQztJQUN4QixLQUFLLE1BQU0sS0FBSyxJQUFJLE1BQU0sQ0FBQyxXQUFXLEVBQUUsRUFBRTtRQUN4QyxJQUFJLEtBQUssQ0FBQyxHQUFHLEdBQUcsR0FBRyxJQUFJLEtBQ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