|
1 | 1 | 'use strict'
|
2 | 2 |
|
3 |
| -var kssBlocksParser = require('./kss-blocks-parser').kssBlocksParser; |
| 3 | +var gonzales = require('gonzales-pe'), |
| 4 | + gonzo = require('gonzales-ast'); |
4 | 5 |
|
5 | 6 | module.exports = {
|
6 | 7 |
|
7 | 8 | /* Split string source into array of code and comment blocks */
|
8 |
| - pureSplitter: function(source) { |
9 |
| - var lines = source.split(/\n|$/g), |
10 |
| - anInlineComment = /^ *\/\//, |
11 |
| - multilineCommentStarted = /\/\*/, |
12 |
| - multilineCommentFinished = /\*\//, |
13 |
| - isMultilineCommentStarted, |
14 |
| - blocks = [], |
15 |
| - block, |
16 |
| - type, |
17 |
| - prevType; |
18 |
| - |
19 |
| - lines.forEach(function(line) { |
20 |
| - |
21 |
| - if (isMultilineCommentStarted) { |
22 |
| - type = 'comment' |
23 |
| - if (multilineCommentFinished.test(line)) { |
24 |
| - isMultilineCommentStarted = false; |
| 9 | + pureSplitter: function(source, syntax) { |
| 10 | + |
| 11 | + syntax = syntax || 'scss'; |
| 12 | + var out = {}, |
| 13 | + ast = gonzales.srcToAST({ |
| 14 | + src: source, |
| 15 | + syntax: syntax |
| 16 | + }), |
| 17 | + block, |
| 18 | + blocks = [], |
| 19 | + prevNode; |
| 20 | + |
| 21 | + gonzo.traverse(ast, [{ |
| 22 | + // Visitor for SASS and SCSS syntaxes |
| 23 | + test: function(name, nodes) { |
| 24 | + return name !== 'stylesheet'; |
| 25 | + }, |
| 26 | + process: function(nodes) { |
| 27 | + var startNewBlock = function() { |
| 28 | + block && blocks.push(block); |
| 29 | + block = { |
| 30 | + type: '', |
| 31 | + content: '' |
| 32 | + }; |
| 33 | + }, |
| 34 | + type; |
| 35 | + |
| 36 | + // Calculate type of the current block |
| 37 | + |
| 38 | + // Multiline comment is comment |
| 39 | + if (nodes[0] == 'commentML') { |
| 40 | + type = 'comment'; |
25 | 41 | }
|
26 |
| - } else { |
27 |
| - isMultilineCommentStarted = multilineCommentStarted.test(line); |
28 |
| - if (isMultilineCommentStarted) { |
29 |
| - type = 'comment' |
30 |
| - } else { |
31 |
| - type = anInlineComment.test(line) ? 'comment' : 'code'; |
| 42 | + // Singleline comment is comment |
| 43 | + else if (nodes[0] === 'commentSL') { |
| 44 | + type = 'comment'; |
32 | 45 | }
|
33 |
| - } |
34 |
| - |
35 |
| - if (prevType != type) { |
36 |
| - // Save old block if a new type appears |
37 |
| - if (block) { |
38 |
| - block.content = block.content.join('\n'); |
39 |
| - blocks.push(block); |
| 46 | + // Single breaklines between singleline comments are comments |
| 47 | + else if (nodes[0] === 's' && nodes[1].split('\n').length <= 2 && prevNode[0] === 'commentSL') { |
| 48 | + type = 'comment'; |
40 | 49 | }
|
41 |
| - // Start a new block after changing the type |
42 |
| - block = { |
43 |
| - type: type, |
44 |
| - content: [line] |
| 50 | + else { |
| 51 | + type = 'code'; |
45 | 52 | }
|
46 |
| - } else { |
47 |
| - block.content.push(line); |
48 |
| - } |
49 | 53 |
|
50 |
| - prevType = type; |
| 54 | + // If type has been changed, start a new block |
| 55 | + if (!block || block.type != type) { |
| 56 | + startNewBlock(); |
| 57 | + } |
51 | 58 |
|
52 |
| - }); |
| 59 | + // Extend current block content |
| 60 | + block.type = type; |
| 61 | + block.content += gonzales.astToSrc({ |
| 62 | + ast: nodes, |
| 63 | + syntax: syntax |
| 64 | + }); |
| 65 | + prevNode = nodes; |
53 | 66 |
|
54 |
| - // Push the last block |
55 |
| - block.content = block.content.join('\n'); |
56 |
| - blocks.push(block); |
| 67 | + } |
| 68 | + }]); |
57 | 69 |
|
| 70 | + // push last block |
| 71 | + blocks.push(block); |
58 | 72 | return blocks;
|
| 73 | + |
59 | 74 | },
|
60 | 75 |
|
61 | 76 | getBlocks: function(source) {
|
@@ -86,11 +101,9 @@ module.exports = {
|
86 | 101 | }
|
87 | 102 | } else {
|
88 | 103 | // Not KSS comments are considered to be parts of code
|
89 |
| - pair.code.push('\n') |
90 | 104 | pair.code.push(block.content)
|
91 | 105 | }
|
92 | 106 | } else {
|
93 |
| - pair.code.push('\n') |
94 | 107 | pair.code.push(block.content)
|
95 | 108 | }
|
96 | 109 |
|
|
0 commit comments