-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
92 lines (86 loc) · 2.02 KB
/
index.js
File metadata and controls
92 lines (86 loc) · 2.02 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
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
import { makeMarkers, makeCutRule, makePasteRule } from 'restream'
import { commentsRe, inlineCommentsRe } from './lib'
/**
* Create a new set of rules, where service markers are used to exclude comments and strings from processing.
* @param {!Array<!_restream.Rule>} [rules] A set of rules to surround with markers. Typically, this will be done by `alamode`.
*/
const makeRules = (rules = []) => {
const {
comments,
inlineComments,
strings,
literals,
escapes,
regexes,
regexGroups,
} = makeMarkers({
comments: commentsRe,
inlineComments: inlineCommentsRe,
strings: /(["'])(.*?)\1/gm,
literals: /`([\s\S]*?)`/gm,
escapes: /\\[\\`'"/]/g,
regexes: /\/(.+?)\//gm,
regexGroups: /\[(.*?)\]/gm,
})
const mr = [
comments, inlineComments, strings, literals,
escapes, regexes, regexGroups,
]
const [
cutComments, cutInlineComments, cutStrings, cutLiterals,
cutEscapes, cutRegexes, cutRegexGroups,
] = mr.map(makeCutRule)
const [
pasteComments, pasteInlineComments, pasteStrings, pasteLiterals,
pasteEscapes, pasteRegexes, pasteRegexGroups,
] = mr.map(m => makePasteRule(m))
const allRules = [
cutEscapes,
cutComments,
cutInlineComments,
cutStrings,
cutRegexGroups,
cutRegexes,
cutLiterals,
stopRule,
...rules,
pasteLiterals,
pasteRegexes,
pasteRegexGroups,
pasteStrings,
pasteInlineComments,
pasteComments,
pasteEscapes,
]
return {
rules: allRules,
markers: {
literals,
strings,
comments,
inlineComments,
escapes,
regexes,
regexGroups,
},
}
}
/**
* For debugging where markers went wrong.
* @type {_restream.Rule}
*/
const stopRule = {
re: /./,
replacement(m) {
/** @suppress {checkTypes} */
const stop = this['stopProcessing']
if (stop) this.brake()
return m
},
}
export default makeRules
export { inlineCommentsRe, commentsRe }
/**
* @suppress {nonStandardJsDocs}
* @typedef {import('restream').Rule} _restream.Rule
*/