forked from Shopify/retext-shopify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
97 lines (81 loc) · 2.51 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
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
/**
* @author Adam Hollett and Jeremy Hanson-Finger
* @copyright 2016 Shopify
* @license MIT
* @module retext:shopify
* @fileoverview Check phrases for simpler alternatives.
*/
'use strict';
/* eslint-env commonjs */
/*
* Dependencies.
*/
var keys = require('object-keys');
var difference = require('array-differ');
var nlcstToString = require('nlcst-to-string');
var quotation = require('quotation');
var search = require('nlcst-search');
var patterns = require('./data/index.json');
/*
* List of all phrases.
*/
var list = keys(patterns);
/**
* Attacher.
*
* @param {Retext} processor
* - Instance.
* @param {Object?} [options]
* - Configuration.
* @param {Array.<string>?} [options.ignore]
* - List of phrases to *not* warn about.
* @return {Function} - `transformer`.
*/
function attacher(processor, options) {
var ignore = (options || {}).ignore || [];
var phrases = difference(list, ignore);
/**
* Search `tree` for validations.
*
* @param {Node} tree - NLCST node.
* @param {VFile} file - Virtual file.
*/
function transformer(tree, file) {
search(tree, phrases, function (match, position, parent, phrase) {
var pattern = patterns[phrase];
var replace = pattern.replace;
var note = pattern.note;
var matchedString = nlcstToString(match);
var value = quotation(nlcstToString(match), '“', '”');
var newvalue = quotation(replace, '“', '”');
var message = undefined;
if (!replace.length) {
message = value + ' is not Shopify style. Avoid using it.';
if (note) {
message += ' (' + note + ')';
}
} else if (matchedString !== replace){
message = value + ' is not Shopify style. Use ' + newvalue +
' instead.';
if (note) {
message += ' (' + note + ')';
}
} else if (matchedString === replace){
return transformer;
}
if (message) {
message = file.warn(message, {
'start': match[0].position.start,
'end': match[match.length - 1].position.end
});
}
message.ruleId = phrase;
message.source = 'retext-shopify';
}, {'allowApostrophes': false, 'allowDashes': true});
}
return transformer;
}
/*
* Expose.
*/
module.exports = attacher;