forked from ember-cli/eslint-plugin-ember
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatic-attr-value.js
More file actions
64 lines (61 loc) · 1.72 KB
/
static-attr-value.js
File metadata and controls
64 lines (61 loc) · 1.72 KB
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
'use strict';
/**
* Return the statically-known string value of a Glimmer attribute value node,
* or `undefined` when the value is dynamic (cannot be resolved at lint time).
*
* Unwraps:
* - GlimmerTextNode → chars
* - GlimmerMustacheStatement with a literal path (boolean/string/number) → stringified value
* - GlimmerConcatStatement whose parts are all statically resolvable → joined string
*
* A missing/undefined value (valueless attribute, e.g. `<input disabled>`)
* returns the empty string. Pass `attr.value` — not the attribute itself.
*/
function getStaticAttrValue(value) {
if (value === null || value === undefined) {
return '';
}
if (value.type === 'GlimmerTextNode') {
return value.chars;
}
if (value.type === 'GlimmerMustacheStatement') {
return extractLiteral(value.path);
}
if (value.type === 'GlimmerConcatStatement') {
const parts = value.parts || [];
let out = '';
for (const part of parts) {
if (part.type === 'GlimmerTextNode') {
out += part.chars;
continue;
}
if (part.type === 'GlimmerMustacheStatement') {
const literal = extractLiteral(part.path);
if (literal === undefined) {
return undefined;
}
out += literal;
continue;
}
return undefined;
}
return out;
}
return undefined;
}
function extractLiteral(path) {
if (!path) {
return undefined;
}
if (path.type === 'GlimmerBooleanLiteral') {
return path.value ? 'true' : 'false';
}
if (path.type === 'GlimmerStringLiteral') {
return path.value;
}
if (path.type === 'GlimmerNumberLiteral') {
return String(path.value);
}
return undefined;
}
module.exports = { getStaticAttrValue };