forked from prettier/prettier
/
angular.js
128 lines (120 loc) · 3.45 KB
/
angular.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
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
"use strict";
const {
builders: { join, line, group },
} = require("../../document/index.js");
const { hasNode, hasComment, getComments } = require("../utils.js");
const { printBinaryishExpression } = require("./binaryish.js");
/** @typedef {import("../../common/ast-path")} AstPath */
function printAngular(path, options, print) {
const node = path.getValue();
// Angular nodes always starts with `NG`
if (!node.type.startsWith("NG")) {
return;
}
switch (node.type) {
case "NGRoot":
return [
print("node"),
!hasComment(node.node)
? ""
: " //" + getComments(node.node)[0].value.trimEnd(),
];
case "NGPipeExpression":
return printBinaryishExpression(path, options, print);
case "NGChainedExpression":
return group(
join(
[";", line],
path.map(
(childPath) =>
hasNgSideEffect(childPath) ? print() : ["(", print(), ")"],
"expressions"
)
)
);
case "NGEmptyExpression":
return "";
case "NGQuotedExpression":
return [node.prefix, ": ", node.value.trim()];
case "NGMicrosyntax":
return path.map(
(childPath, index) => [
index === 0
? ""
: isNgForOf(childPath.getValue(), index, node)
? " "
: [";", line],
print(),
],
"body"
);
case "NGMicrosyntaxKey":
return /^[$_a-z][\w$]*(?:-[$_a-z][\w$])*$/i.test(node.name)
? node.name
: JSON.stringify(node.name);
case "NGMicrosyntaxExpression":
return [
print("expression"),
node.alias === null ? "" : [" as ", print("alias")],
];
case "NGMicrosyntaxKeyedExpression": {
const index = path.getName();
const parentNode = path.getParentNode();
const shouldNotPrintColon =
isNgForOf(node, index, parentNode) ||
(((index === 1 &&
(node.key.name === "then" || node.key.name === "else")) ||
(index === 2 &&
node.key.name === "else" &&
parentNode.body[index - 1].type ===
"NGMicrosyntaxKeyedExpression" &&
parentNode.body[index - 1].key.name === "then")) &&
parentNode.body[0].type === "NGMicrosyntaxExpression");
return [
print("key"),
shouldNotPrintColon ? " " : ": ",
print("expression"),
];
}
case "NGMicrosyntaxLet":
return [
"let ",
print("key"),
node.value === null ? "" : [" = ", print("value")],
];
case "NGMicrosyntaxAs":
return [print("key"), " as ", print("alias")];
default:
/* istanbul ignore next */
throw new Error(
`Unknown Angular node type: ${JSON.stringify(node.type)}.`
);
}
}
function isNgForOf(node, index, parentNode) {
return (
node.type === "NGMicrosyntaxKeyedExpression" &&
node.key.name === "of" &&
index === 1 &&
parentNode.body[0].type === "NGMicrosyntaxLet" &&
parentNode.body[0].value === null
);
}
/** identify if an angular expression seems to have side effects */
/**
* @param {AstPath} path
* @returns {boolean}
*/
function hasNgSideEffect(path) {
return hasNode(path.getValue(), (node) => {
switch (node.type) {
case undefined:
return false;
case "CallExpression":
case "OptionalCallExpression":
case "AssignmentExpression":
return true;
}
});
}
module.exports = { printAngular };