Skip to content

Commit 8e8ec0b

Browse files
committed
feature: @putout/plugin-montag: add-newline-before-text: multiline
1 parent ae0860e commit 8e8ec0b

4 files changed

Lines changed: 72 additions & 4 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
test('putout: operator: json: isDocker', ({ok}) => {
2+
const source = montag`
3+
__putout_processor_docker([
4+
['MAINTAINER', 'docker@moby.com']
5+
]);\n
6+
`;
7+
8+
const is = isDocker(source);
9+
10+
ok(is);
11+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
test('putout: operator: json: isDocker', ({ok}) => {
2+
const source = montag`__putout_processor_docker([
3+
['MAINTAINER', 'docker@moby.com']
4+
]);\n
5+
`;
6+
7+
const is = isDocker(source);
8+
9+
ok(is);
10+
});

packages/plugin-montag/lib/add-newline-before-text/index.js

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import {types} from 'putout';
2+
3+
const {isVariableDeclarator} = types;
4+
15
export const report = () => `Add '\\n' after montag`;
26

37
export const match = () => ({
@@ -14,12 +18,50 @@ export const match = () => ({
1418
export const replace = () => ({
1519
'montag`__a`': ({__a}, path) => {
1620
const {raw} = __a.value;
17-
const {column} = path.node.loc.start;
18-
const indentBefore = Array(column + 4).join(' ');
19-
const indentAfter = Array(column).join(' ');
21+
const column = getColumn(path);
2022

21-
__a.value.raw = `\n${indentBefore}${raw}\n${indentAfter}`;
23+
if (raw.includes('\n'))
24+
__a.value.raw = createMultilineValue({
25+
raw,
26+
column,
27+
});
28+
else
29+
__a.value.raw = createOnelineValue({
30+
raw,
31+
column,
32+
});
2233

2334
return path;
2435
},
2536
});
37+
38+
function createOnelineValue({raw, column}) {
39+
const indentAfter = Array(column).join(' ');
40+
const indentBefore = Array(column + 4).join(' ');
41+
42+
return `\n${indentBefore}${raw}\n${indentAfter}`;
43+
}
44+
45+
function createMultilineValue({raw, column}) {
46+
const rawLines = raw.split('\n');
47+
const indentAfter = Array(column).join(' ');
48+
const indentBefore = Array(column).join(' ');
49+
const count = rawLines.length - 1;
50+
const lines = [];
51+
52+
for (const [index, line] of rawLines.entries()) {
53+
if (index === count)
54+
break;
55+
56+
lines.push(`${indentBefore} ${line}`);
57+
}
58+
59+
return `\n ${lines.join('\n')}\n ${indentAfter}`;
60+
}
61+
62+
function getColumn(path) {
63+
if (isVariableDeclarator(path.parentPath))
64+
return path.parentPath.parentPath.node.loc.start.column;
65+
66+
return path.node.loc.start.column;
67+
}

packages/plugin-montag/lib/add-newline-before-text/index.spec.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,8 @@ test('montag: add-newline-before-text: no report: space', (t) => {
2121
t.noReport('space');
2222
t.end();
2323
});
24+
25+
test('montag: add-newline-before-text: transform: indent', (t) => {
26+
t.transform('indent');
27+
t.end();
28+
});

0 commit comments

Comments
 (0)