-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
59 lines (57 loc) · 1.65 KB
/
index.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
var Typograf = require('typograf');
var tp = new Typograf({
locale:'ru'
});
module.exports = function({ types: t }, opts) {
return {
name: 'typograf',
pre() {
if (opts.disable){
for (var di = 0; di < opts.disable.length; di++ ) {
tp.disableRule(opts.disable[di]);
}
}
if (opts.enable){
for (var ei = 0; ei < opts.enable.length; ei++ ) {
tp.enableRule(opts.enable[ei]);
}
}
if (opts.settings){
for (var si = 0; si < opts.settings.length; si++ ) {
tp.setSetting.apply(tp, opts.settings[si])
}
}
},
visitor: {
Program(programPath) {
var isTpFile = false;
programPath.traverse({
enter(path) {
if (path.node.leadingComments && path.node.leadingComments.find(function(comment) {
return comment.value.trim() === "typograf-enable";
})) {
isTpFile = true;
}
if (path.node.leadingComments && path.node.leadingComments.find(function(comment) {
return comment.value.trim() === "typograf-disable";
})) {
isTpFile = false;
}
},
StringLiteral(path) {
if (isTpFile
&& !t.isImportDeclaration(path.parentPath.node)
&& (path.node.value || '').trim()) {
path.node.value = tp.execute(path.node.value)
}
},
JSXText(path) {
if (isTpFile && (path.node.value || '').trim()) {
path.node.value = tp.execute(path.node.value)
}
},
});
}
}
};
};