|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const test = require('supertape'); |
| 4 | + |
| 5 | +const { |
| 6 | + parse, |
| 7 | + print, |
| 8 | + traverse, |
| 9 | +} = require('putout'); |
| 10 | + |
| 11 | +const {addParens, removeParens} = require('./parens'); |
| 12 | + |
| 13 | +test('putout: operate: parens: removeParens: putout', (t) => { |
| 14 | + const source = '(b = 3)'; |
| 15 | + const ast = parse(source); |
| 16 | + |
| 17 | + traverse(ast, { |
| 18 | + AssignmentExpression: removeParens, |
| 19 | + }); |
| 20 | + |
| 21 | + const result = print(ast); |
| 22 | + const expected = 'b = 3;\n'; |
| 23 | + |
| 24 | + t.equal(result, expected); |
| 25 | + t.end(); |
| 26 | +}); |
| 27 | + |
| 28 | +test('putout: operate: parens: removeParens: babel', (t) => { |
| 29 | + const source = '(b = 3)'; |
| 30 | + const ast = parse(source, { |
| 31 | + printer: 'babel', |
| 32 | + }); |
| 33 | + |
| 34 | + traverse(ast, { |
| 35 | + AssignmentExpression(path) { |
| 36 | + removeParens(path); |
| 37 | + path.stop(); |
| 38 | + }, |
| 39 | + }); |
| 40 | + |
| 41 | + const result = print(ast, { |
| 42 | + printer: 'babel', |
| 43 | + }); |
| 44 | + |
| 45 | + const expected = 'b = 3;\n'; |
| 46 | + |
| 47 | + t.equal(result, expected); |
| 48 | + t.end(); |
| 49 | +}); |
| 50 | + |
| 51 | +test('putout: operate: parens: addParens', (t) => { |
| 52 | + const source = 'const b = a'; |
| 53 | + const ast = parse(source); |
| 54 | + |
| 55 | + traverse(ast, { |
| 56 | + VariableDeclarator(path) { |
| 57 | + addParens(path.get('init')); |
| 58 | + path.stop(); |
| 59 | + }, |
| 60 | + }); |
| 61 | + |
| 62 | + const result = print(ast); |
| 63 | + const expected = 'const b = (a);\n'; |
| 64 | + |
| 65 | + t.equal(result, expected); |
| 66 | + t.end(); |
| 67 | +}); |
| 68 | + |
| 69 | +test('putout: operate: parens: addParens: babel', (t) => { |
| 70 | + const source = 'b = 3'; |
| 71 | + const ast = parse(source, { |
| 72 | + printer: 'babel', |
| 73 | + }); |
| 74 | + |
| 75 | + traverse(ast, { |
| 76 | + AssignmentExpression(path) { |
| 77 | + addParens(path); |
| 78 | + path.stop(); |
| 79 | + }, |
| 80 | + }); |
| 81 | + |
| 82 | + const result = print(ast, { |
| 83 | + printer: 'babel', |
| 84 | + }); |
| 85 | + |
| 86 | + const expected = '(b = 3);\n'; |
| 87 | + |
| 88 | + t.equal(result, expected); |
| 89 | + t.end(); |
| 90 | +}); |
| 91 | + |
| 92 | +test('putout: operate: parens: addParens: babel: ts', (t) => { |
| 93 | + const source = 'const a: boolean = true;'; |
| 94 | + const ast = parse(source, { |
| 95 | + printer: 'babel', |
| 96 | + isTS: true, |
| 97 | + }); |
| 98 | + |
| 99 | + traverse(ast, { |
| 100 | + TSBooleanKeyword(path) { |
| 101 | + addParens(path); |
| 102 | + path.stop(); |
| 103 | + }, |
| 104 | + }); |
| 105 | + |
| 106 | + const result = print(ast, { |
| 107 | + printer: 'babel', |
| 108 | + }); |
| 109 | + |
| 110 | + const expected = 'const a: (boolean) = true;\n'; |
| 111 | + |
| 112 | + t.equal(result, expected); |
| 113 | + t.end(); |
| 114 | +}); |
0 commit comments