-
Notifications
You must be signed in to change notification settings - Fork 48
/
ast-transform.js
94 lines (79 loc) · 2.39 KB
/
ast-transform.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
/* eslint-env node */
'use strict';
const FaIconComponent = require('./build-time-components/fa-icon');
const { getAbstractIcon } = require('./fontawesome-helpers');
class AstTransform {
constructor(options) {
this.options = options;
}
transform(ast) {
this.syntax.traverse(ast, {
BlockStatement: (node) => {
return this._applyTransform(node);
},
MustacheStatement: (node) => {
return this._applyTransform(node);
},
});
return ast;
}
_applyTransform(node) {
if (
node.path.original === 'fa-icon' &&
this._hasStaticIconName(node) &&
!this._hasUnsupportedProperty(node) &&
this._iconExists(node)
) {
return new FaIconComponent(node, { transform: this }).toElement();
}
}
_hasStaticIconName(node) {
if (node.params.length && node.params[0].type === 'StringLiteral') {
//icon is given as a positional param {{fa-icon 'coffee'}}
return true;
}
const hash = node.hash.pairs.find((obj) => obj.key === 'icon');
if (hash) {
return hash.value.type === 'StringLiteral';
}
return false;
}
_iconExists(node) {
let icon, prefix;
if (node.params.length && node.params[0].type === 'StringLiteral') {
//icon is given as a positional param {{fa-icon 'coffee'}}
icon = node.params[0].value;
} else {
const iconHash = node.hash.pairs.find((obj) => obj.key === 'icon');
if (iconHash) {
icon = iconHash.value.value;
}
}
const prefixHash = node.hash.pairs.find((obj) => obj.key === 'prefix');
prefix = prefixHash
? prefixHash.value.value
: this.addon.fontawesomeConfig.defaultPrefix;
if (!getAbstractIcon(icon, prefix)) {
this.addon.ui.writeWarnLine(
`${this.addon.name}: Unable to load icon '${icon}' with prefix '${prefix}'.`
);
return false;
}
return true;
}
_hasUnsupportedProperty(node) {
const transform = node.hash.pairs.find((obj) => obj.key === 'transform');
const mask = node.hash.pairs.find((obj) => obj.key === 'mask');
const flip = node.hash.pairs.find((obj) => obj.key === 'flip');
return transform || mask || flip;
}
}
function buildAstTransform(addon) {
return class EmberFontAwesomeAstTransform extends AstTransform {
constructor(options) {
super(options);
this.addon = addon;
}
};
}
module.exports = buildAstTransform;