-
Notifications
You must be signed in to change notification settings - Fork 450
Expand file tree
/
Copy pathquotes.js
More file actions
58 lines (50 loc) · 1.43 KB
/
quotes.js
File metadata and controls
58 lines (50 loc) · 1.43 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
'use strict';
module.exports = {
name: 'quotes',
syntax: ['css', 'less', 'sass', 'scss'],
accepts: {
string: /^single|double$/
},
/**
* Processes tree node.
* @param {node} ast
*/
process: function(ast) {
let value = this.value;
ast.traverseByType('string', function(string) {
if (string.content[0] === '"' && value === 'single') {
string.content = string.content
// Unescape all escaped double quotes
.replace(/\\"/g, '"')
// Escape all the single quotes
.replace(/([^\\])'/g, '$1\\\'')
// Replace the first and the last quote
.replace(/^"|"$/g, '\'');
} else if (string.content[0] === '\'' && value === 'double') {
string.content = string.content
// Unescape all escaped single quotes
.replace(/\\'/g, '\'')
// Escape all the double quotes
.replace(/([^\\])"/g, '$1\\\"')
// Replace the first and the last quote
.replace(/^'|'$/g, '"');
}
});
},
/**
* Detects the value of an option at the tree node.
*
* @param {node} ast
*/
detect: function(ast) {
let detected = [];
ast.traverseByType('string', function(string) {
if (string.content[0] === '"') {
detected.push('double');
} else if (string.content[0] === '\'') {
detected.push('single');
}
});
return detected;
}
};