-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtabstop-transform.js
99 lines (86 loc) · 2.92 KB
/
tabstop-transform.js
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
93
94
95
96
97
98
99
'use strict';
require('mocha');
const path = require('path');
const assert = require('assert').strict;
const { parse, render } = require('../lib/Parser');
const data = {
TM_DIRECTORY: __dirname,
TM_FILENAME: 'parse.js',
TM_FILEPATH: path.join(__dirname, 'parse.js')
};
const transforms = type => {
return (input, options) => {
return parse(input, options).find(type);
};
};
const params = node => {
return omit(node.params, ['replacers']);
};
const omit = (node, keys = []) => {
let obj = {};
for (let key of Object.keys(node)) {
if (!keys.includes(key)) {
obj[key] = node[key];
}
}
return obj;
};
describe('tabstop transforms', () => {
describe('.parse()', () => {
const transform = transforms('tabstop_transform');
it('should parse placeholder transforms', () => {
const input = 'Foo ${1/./=/} Bar';
const node = transform(input);
assert.equal(input.slice(...node.loc.range), '${1/./=/}');
assert.equal(node.stringify(), '${1/./=/}');
assert.equal(node.inner(), '1/./=/');
});
it('should parse placeholder transforms with embedded regex characters', () => {
const input = 'Foo ${1/[${1:.}/]/=/} Bar';
const node = transform(input);
assert.equal(input.slice(...node.loc.range), '${1/[${1:.}/]/=/}');
assert.equal(node.stringify(), '${1/[${1:.}/]/=/}');
assert.equal(node.inner(), '1/[${1:.}/]/=/');
});
it('should parse placeholder transforms with regex flags', () => {
const input = 'Foo ${1/./=/gim} Bar';
const node = transform(input);
assert.equal(node.type, 'tabstop_transform');
assert.equal(input.slice(...node.loc.range), '${1/./=/gim}');
assert.equal(node.stringify(), '${1/./=/gim}');
assert.equal(node.inner(), '1/./=/gim');
});
it('should parse format variable', () => {
const input = 'Foo ${1/(.)/${1}/g} Bar';
const node = transform(input);
assert.equal(node.type, 'tabstop_transform');
assert.equal(input.slice(...node.loc.range), '${1/(.)/${1}/g}');
assert.equal(node.stringify(), '${1/(.)/${1}/g}');
assert.equal(node.inner(), '1/(.)/${1}/g');
});
it('should use placeholder values', () => {
const input = 'errorContext: `${1:err}`, error:${1/err/ok/}';
assert.equal(render(input), 'errorContext: `err`, error:ok');
});
it('should parse format variable', () => {
assert.deepEqual(params(transform('Foo ${1/(.)/${1}/g} Bar')), {
type: 'tabstop_transform',
varname: '1',
regex: /(.)/g,
format: '${1}',
source: '(.)',
string: '${1/(.)/${1}/g}',
flags: 'g'
});
assert.deepEqual(params(transform('Foo ${1/(.)/${1:upcase}/g} Bar')), {
type: 'tabstop_transform',
string: '${1/(.)/${1:upcase}/g}',
varname: '1',
regex: /(.)/g,
format: '${1:upcase}',
source: '(.)',
flags: 'g'
});
});
});
});