|
8 | 8 | The above copyright notice and this permission notice shall be |
9 | 9 | included in all copies or substantial portions of this Source Code Form. |
10 | 10 | */ |
11 | | -const Comment = require('postcss/lib/comment'); |
| 11 | +const PostCssComment = require('postcss/lib/comment'); |
| 12 | + |
| 13 | +const { stringify } = require('../ValuesStringifier'); |
12 | 14 |
|
13 | 15 | const inlineRegex = /(\/\/)/; |
14 | 16 |
|
15 | | -Comment.testInline = (token) => inlineRegex.test(token[1]); |
| 17 | +class Comment extends PostCssComment { |
| 18 | + static testInline(token) { |
| 19 | + return inlineRegex.test(token[1]); |
| 20 | + } |
16 | 21 |
|
17 | | -Comment.tokenizeNext = (tokens, parser) => { |
18 | | - const [first] = tokens; |
19 | | - const newlineIndex = tokens.findIndex((t) => /\n/.test(t[1])); |
20 | | - let bits = tokens; |
21 | | - let rest = []; |
| 22 | + static tokenizeNext(tokens, parser) { |
| 23 | + const [first] = tokens; |
| 24 | + const newlineIndex = tokens.findIndex((t) => /\n/.test(t[1])); |
| 25 | + let bits = tokens; |
| 26 | + let rest = []; |
22 | 27 |
|
23 | | - if (newlineIndex >= 0) { |
24 | | - bits = tokens.slice(0, newlineIndex); |
25 | | - rest = tokens.slice(newlineIndex); |
26 | | - } |
| 28 | + if (newlineIndex >= 0) { |
| 29 | + bits = tokens.slice(0, newlineIndex); |
| 30 | + rest = tokens.slice(newlineIndex); |
| 31 | + } |
| 32 | + |
| 33 | + bits = bits.map((t) => t[1]); |
27 | 34 |
|
28 | | - bits = bits.map((t) => t[1]); |
| 35 | + // see tilde comment in tokenizeInline |
| 36 | + const text = bits.concat('~~').join(''); |
| 37 | + const last = bits[bits.length - 1]; |
| 38 | + const newToken = ['comment', text, first[2], first[3], last[2], last[3]]; |
29 | 39 |
|
30 | | - // see tilde comment in tokenizeInline |
31 | | - const text = bits.concat('~~').join(''); |
32 | | - const last = bits[bits.length - 1]; |
33 | | - const newToken = ['comment', text, first[2], first[3], last[2], last[3]]; |
| 40 | + parser.back([newToken, ...rest]); |
| 41 | + } |
34 | 42 |
|
35 | | - parser.back([newToken, ...rest]); |
36 | | -}; |
| 43 | + static tokenizeInline(tokens, parser) { |
| 44 | + const [first, ...rest] = tokens; |
| 45 | + const bits = first[1].split(/(\/\/.+)/).filter((t) => !!t); |
| 46 | + const newTokens = []; |
| 47 | + const [, , startLine, , endLine] = first; |
| 48 | + let [, , , startChar, , endChar] = first; |
37 | 49 |
|
38 | | -Comment.tokenizeInline = (tokens, parser) => { |
39 | | - const [first, ...rest] = tokens; |
40 | | - const bits = first[1].split(/(\/\/.+)/).filter((t) => !!t); |
41 | | - const newTokens = []; |
42 | | - const [, , startLine, , endLine] = first; |
43 | | - let [, , , startChar, , endChar] = first; |
| 50 | + for (let bit of bits) { |
| 51 | + const comment = bit.slice(0, 2) === '//'; |
| 52 | + const type = comment ? 'comment' : 'word'; |
44 | 53 |
|
45 | | - for (let bit of bits) { |
46 | | - const comment = bit.slice(0, 2) === '//'; |
47 | | - const type = comment ? 'comment' : 'word'; |
| 54 | + if (comment) { |
| 55 | + // the Parser base comment() method trims the last two characters when creating the node |
| 56 | + // these tildes are added to counter that. it's hacky, but it works, and we don't have to |
| 57 | + // re-implement the method |
| 58 | + bit += '~~'; |
| 59 | + } |
48 | 60 |
|
49 | | - if (comment) { |
50 | | - // the Parser base comment() method trims the last two characters when creating the node |
51 | | - // these tildes are added to counter that. it's hacky, but it works, and we don't have to |
52 | | - // re-implement the method |
53 | | - bit += '~~'; |
54 | | - } |
| 61 | + if (bit !== bits[0]) { |
| 62 | + startChar = endChar + 1; |
| 63 | + } |
55 | 64 |
|
56 | | - if (bit !== bits[0]) { |
57 | | - startChar = endChar + 1; |
58 | | - } |
| 65 | + endChar = startChar + bit.length - 1; |
59 | 66 |
|
60 | | - endChar = startChar + bit.length - 1; |
| 67 | + newTokens.push([type, bit, startLine, startChar, endLine, endChar]); |
| 68 | + } |
61 | 69 |
|
62 | | - newTokens.push([type, bit, startLine, startChar, endLine, endChar]); |
| 70 | + parser.back(newTokens.concat(rest)); |
63 | 71 | } |
64 | 72 |
|
65 | | - parser.back(newTokens.concat(rest)); |
66 | | -}; |
| 73 | + toString(stringifier = stringify) { |
| 74 | + return super.toString(stringifier); |
| 75 | + } |
| 76 | +} |
67 | 77 |
|
68 | 78 | module.exports = Comment; |
0 commit comments