|
| 1 | +/* |
| 2 | + * decaffeinate suggestions: |
| 3 | + * DS102: Remove unnecessary code created because of implicit returns |
| 4 | + * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md |
| 5 | + */ |
| 6 | +const BodyParser = require('../lib/snippet-body-parser'); |
| 7 | + |
| 8 | +describe("Snippet Body Parser", function() { |
| 9 | + it("breaks a snippet body into lines, with each line containing tab stops at the appropriate position", function() { |
| 10 | + const bodyTree = BodyParser.parse(`\ |
| 11 | +the quick brown $1fox \${2:jumped \${3:over} |
| 12 | +}the \${4:lazy} dog\ |
| 13 | +` |
| 14 | + ); |
| 15 | + |
| 16 | + return expect(bodyTree).toEqual([ |
| 17 | + "the quick brown ", |
| 18 | + {index: 1, content: []}, |
| 19 | + "fox ", |
| 20 | + { |
| 21 | + index: 2, |
| 22 | + content: [ |
| 23 | + "jumped ", |
| 24 | + {index: 3, content: ["over"]}, |
| 25 | + "\n" |
| 26 | + ], |
| 27 | + }, |
| 28 | + "the ", |
| 29 | + {index: 4, content: ["lazy"]}, |
| 30 | + " dog" |
| 31 | + ]); |
| 32 | +}); |
| 33 | + |
| 34 | + it("removes interpolated variables in placeholder text (we don't currently support it)", function() { |
| 35 | + const bodyTree = BodyParser.parse(`\ |
| 36 | +module \${1:ActiveRecord::\${TM_FILENAME/(?:\\A|_)([A-Za-z0-9]+)(?:\\.rb)?/(?2::\\u$1)/g}}\ |
| 37 | +` |
| 38 | + ); |
| 39 | + |
| 40 | + return expect(bodyTree).toEqual([ |
| 41 | + "module ", |
| 42 | + { |
| 43 | + "index": 1, |
| 44 | + "content": ["ActiveRecord::", ""] |
| 45 | + } |
| 46 | + ]); |
| 47 | +}); |
| 48 | + |
| 49 | + it("skips escaped tabstops", function() { |
| 50 | + const bodyTree = BodyParser.parse(`\ |
| 51 | +snippet $1 escaped \\$2 \\\\$3\ |
| 52 | +` |
| 53 | + ); |
| 54 | + |
| 55 | + return expect(bodyTree).toEqual([ |
| 56 | + "snippet ", |
| 57 | + { |
| 58 | + index: 1, |
| 59 | + content: [] |
| 60 | + }, |
| 61 | + " escaped $2 \\", |
| 62 | + { |
| 63 | + index: 3, |
| 64 | + content: [] |
| 65 | + } |
| 66 | + ]); |
| 67 | +}); |
| 68 | + |
| 69 | + it("includes escaped right-braces", function() { |
| 70 | + const bodyTree = BodyParser.parse(`\ |
| 71 | +snippet \${1:{\\}}\ |
| 72 | +` |
| 73 | + ); |
| 74 | + |
| 75 | + return expect(bodyTree).toEqual([ |
| 76 | + "snippet ", |
| 77 | + { |
| 78 | + index: 1, |
| 79 | + content: ["{}"] |
| 80 | + } |
| 81 | + ]); |
| 82 | +}); |
| 83 | + |
| 84 | + it("parses a snippet with transformations", function() { |
| 85 | + const bodyTree = BodyParser.parse(`\ |
| 86 | +<\${1:p}>$0</\${1/f/F/}>\ |
| 87 | +` |
| 88 | + ); |
| 89 | + return expect(bodyTree).toEqual([ |
| 90 | + '<', |
| 91 | + {index: 1, content: ['p']}, |
| 92 | + '>', |
| 93 | + {index: 0, content: []}, |
| 94 | + '</', |
| 95 | + {index: 1, content: [], substitution: {find: /f/g, replace: ['F']}}, |
| 96 | + '>' |
| 97 | + ]); |
| 98 | +}); |
| 99 | + |
| 100 | + it("parses a snippet with multiple tab stops with transformations", function() { |
| 101 | + const bodyTree = BodyParser.parse(`\ |
| 102 | +\${1:placeholder} \${1/(.)/\\u$1/} $1 \${2:ANOTHER} \${2/^(.*)$/\\L$1/} $2\ |
| 103 | +` |
| 104 | + ); |
| 105 | + |
| 106 | + return expect(bodyTree).toEqual([ |
| 107 | + {index: 1, content: ['placeholder']}, |
| 108 | + ' ', |
| 109 | + { |
| 110 | + index: 1, |
| 111 | + content: [], |
| 112 | + substitution: { |
| 113 | + find: /(.)/g, |
| 114 | + replace: [ |
| 115 | + {escape: 'u'}, |
| 116 | + {backreference: 1} |
| 117 | + ] |
| 118 | + } |
| 119 | + }, |
| 120 | + ' ', |
| 121 | + {index: 1, content: []}, |
| 122 | + ' ', |
| 123 | + {index: 2, content: ['ANOTHER']}, |
| 124 | + ' ', |
| 125 | + { |
| 126 | + index: 2, |
| 127 | + content: [], |
| 128 | + substitution: { |
| 129 | + find: /^(.*)$/g, |
| 130 | + replace: [ |
| 131 | + {escape: 'L'}, |
| 132 | + {backreference: 1} |
| 133 | + ] |
| 134 | + } |
| 135 | + }, |
| 136 | + ' ', |
| 137 | + {index: 2, content: []}, |
| 138 | + ]); |
| 139 | +}); |
| 140 | + |
| 141 | + |
| 142 | + it("parses a snippet with transformations and mirrors", function() { |
| 143 | + const bodyTree = BodyParser.parse(`\ |
| 144 | +\${1:placeholder}\n\${1/(.)/\\u$1/}\n$1\ |
| 145 | +` |
| 146 | + ); |
| 147 | + |
| 148 | + return expect(bodyTree).toEqual([ |
| 149 | + {index: 1, content: ['placeholder']}, |
| 150 | + '\n', |
| 151 | + { |
| 152 | + index: 1, |
| 153 | + content: [], |
| 154 | + substitution: { |
| 155 | + find: /(.)/g, |
| 156 | + replace: [ |
| 157 | + {escape: 'u'}, |
| 158 | + {backreference: 1} |
| 159 | + ] |
| 160 | + } |
| 161 | + }, |
| 162 | + '\n', |
| 163 | + {index: 1, content: []} |
| 164 | + ]); |
| 165 | +}); |
| 166 | + |
| 167 | + it("parses a snippet with a format string and case-control flags", function() { |
| 168 | + const bodyTree = BodyParser.parse(`\ |
| 169 | +<\${1:p}>$0</\${1/(.)(.*)/\\u$1$2/}>\ |
| 170 | +` |
| 171 | + ); |
| 172 | + |
| 173 | + return expect(bodyTree).toEqual([ |
| 174 | + '<', |
| 175 | + {index: 1, content: ['p']}, |
| 176 | + '>', |
| 177 | + {index: 0, content: []}, |
| 178 | + '</', |
| 179 | + { |
| 180 | + index: 1, |
| 181 | + content: [], |
| 182 | + substitution: { |
| 183 | + find: /(.)(.*)/g, |
| 184 | + replace: [ |
| 185 | + {escape: 'u'}, |
| 186 | + {backreference: 1}, |
| 187 | + {backreference: 2} |
| 188 | + ] |
| 189 | + } |
| 190 | + }, |
| 191 | + '>' |
| 192 | + ]); |
| 193 | +}); |
| 194 | + |
| 195 | + it("parses a snippet with an escaped forward slash in a transform", function() { |
| 196 | + // Annoyingly, a forward slash needs to be double-backslashed just like the |
| 197 | + // other escapes. |
| 198 | + const bodyTree = BodyParser.parse(`\ |
| 199 | +<\${1:p}>$0</\${1/(.)\\/(.*)/\\u$1$2/}>\ |
| 200 | +` |
| 201 | + ); |
| 202 | + |
| 203 | + return expect(bodyTree).toEqual([ |
| 204 | + '<', |
| 205 | + {index: 1, content: ['p']}, |
| 206 | + '>', |
| 207 | + {index: 0, content: []}, |
| 208 | + '</', |
| 209 | + { |
| 210 | + index: 1, |
| 211 | + content: [], |
| 212 | + substitution: { |
| 213 | + find: /(.)\/(.*)/g, |
| 214 | + replace: [ |
| 215 | + {escape: 'u'}, |
| 216 | + {backreference: 1}, |
| 217 | + {backreference: 2} |
| 218 | + ] |
| 219 | + } |
| 220 | + }, |
| 221 | + '>' |
| 222 | + ]); |
| 223 | +}); |
| 224 | + |
| 225 | + it("parses a snippet with a placeholder that mirrors another tab stop's content", function() { |
| 226 | + const bodyTree = BodyParser.parse(`\ |
| 227 | +$4console.\${3:log}('\${2:$1}', $1);$0\ |
| 228 | +` |
| 229 | + ); |
| 230 | + |
| 231 | + return expect(bodyTree).toEqual([ |
| 232 | + {index: 4, content: []}, |
| 233 | + 'console.', |
| 234 | + {index: 3, content: ['log']}, |
| 235 | + '(\'', |
| 236 | + { |
| 237 | + index: 2, content: [ |
| 238 | + {index: 1, content: []} |
| 239 | + ] |
| 240 | + }, |
| 241 | + '\', ', |
| 242 | + {index: 1, content: []}, |
| 243 | + ');', |
| 244 | + {index: 0, content: []} |
| 245 | + ]); |
| 246 | +}); |
| 247 | + |
| 248 | + return it("parses a snippet with a placeholder that mixes text and tab stop references", function() { |
| 249 | + const bodyTree = BodyParser.parse(`\ |
| 250 | +$4console.\${3:log}('\${2:uh $1}', $1);$0\ |
| 251 | +` |
| 252 | + ); |
| 253 | + |
| 254 | + return expect(bodyTree).toEqual([ |
| 255 | + {index: 4, content: []}, |
| 256 | + 'console.', |
| 257 | + {index: 3, content: ['log']}, |
| 258 | + '(\'', |
| 259 | + { |
| 260 | + index: 2, content: [ |
| 261 | + 'uh ', |
| 262 | + {index: 1, content: []} |
| 263 | + ] |
| 264 | + }, |
| 265 | + '\', ', |
| 266 | + {index: 1, content: []}, |
| 267 | + ');', |
| 268 | + {index: 0, content: []} |
| 269 | + ]); |
| 270 | +}); |
| 271 | +}); |
0 commit comments