Skip to content

Commit 0cd6b84

Browse files
leighmanczosel
authored andcommitted
fix: insert pragma when first docblock mid-file (#1310) (#1311)
1 parent 79578a9 commit 0cd6b84

3 files changed

Lines changed: 155 additions & 9 deletions

File tree

src/pragma.js

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,21 @@ const memoize = require("mem");
55

66
const reHasPragma = /@prettier|@format/;
77

8-
const extractDocBlocks = memoize(text => {
8+
const getPageLevelDocBlock = memoize(text => {
99
const parsed = parse(text);
1010

11-
return parsed.comments.filter(el => el.kind === "commentblock");
11+
const [firstChild] = parsed.children;
12+
const [firstDocBlock] = parsed.comments.filter(
13+
el => el.kind === "commentblock"
14+
);
15+
16+
if (
17+
firstChild &&
18+
firstDocBlock &&
19+
firstDocBlock.loc.start.line < firstChild.loc.start.line
20+
) {
21+
return firstDocBlock;
22+
}
1223
});
1324

1425
function guessLineEnding(text) {
@@ -27,10 +38,10 @@ function hasPragma(text) {
2738
return false;
2839
}
2940

30-
const [firstDocBlock] = extractDocBlocks(text);
41+
const pageLevelDocBlock = getPageLevelDocBlock(text);
3142

32-
if (firstDocBlock) {
33-
const { value } = firstDocBlock;
43+
if (pageLevelDocBlock) {
44+
const { value } = pageLevelDocBlock;
3445

3546
return reHasPragma.test(value);
3647
}
@@ -59,17 +70,17 @@ function injectPragma(docblock, text) {
5970
}
6071

6172
function insertPragma(text) {
62-
const [firstDocBlock] = extractDocBlocks(text);
73+
const pageLevelDocBlock = getPageLevelDocBlock(text);
6374

64-
if (firstDocBlock) {
75+
if (pageLevelDocBlock) {
6576
const {
6677
start: { offset: startOffset },
6778
end: { offset: endOffset }
68-
} = firstDocBlock.loc;
79+
} = pageLevelDocBlock.loc;
6980
const before = text.substring(0, startOffset);
7081
const after = text.substring(endOffset);
7182

72-
return `${before}${injectPragma(firstDocBlock.value, text)}${after}`;
83+
return `${before}${injectPragma(pageLevelDocBlock.value, text)}${after}`;
7384
}
7485

7586
const openTag = "<?php";

tests/insert-pragma/__snapshots__/jsfmt.spec.js.snap

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,129 @@ echo "Hello World";<LF>
204204
================================================================================
205205
`;
206206

207+
exports[`pragma-firstdocblockmidfile.php 1`] = `
208+
====================================options=====================================
209+
endOfLine: "cr"
210+
insertPragma: true
211+
parsers: ["php"]
212+
printWidth: 80
213+
| printWidth
214+
=====================================input======================================
215+
<?php<LF>
216+
<LF>
217+
class Something<LF>
218+
{<LF>
219+
function whatever ()<LF>
220+
{<LF>
221+
/**<LF>
222+
* Some mid-file docblock<LF>
223+
*/<LF>
224+
$variable = 3;<LF>
225+
}<LF>
226+
}<LF>
227+
228+
=====================================output=====================================
229+
<?php<CR>
230+
/**<CR>
231+
* @format<CR>
232+
*/<CR>
233+
<CR>
234+
class Something<CR>
235+
{<CR>
236+
function whatever()<CR>
237+
{<CR>
238+
/**<CR>
239+
* Some mid-file docblock<CR>
240+
*/<CR>
241+
$variable = 3;<CR>
242+
}<CR>
243+
}<CR>
244+
245+
================================================================================
246+
`;
247+
248+
exports[`pragma-firstdocblockmidfile.php 2`] = `
249+
====================================options=====================================
250+
endOfLine: "crlf"
251+
insertPragma: true
252+
parsers: ["php"]
253+
printWidth: 80
254+
| printWidth
255+
=====================================input======================================
256+
<?php<LF>
257+
<LF>
258+
class Something<LF>
259+
{<LF>
260+
function whatever ()<LF>
261+
{<LF>
262+
/**<LF>
263+
* Some mid-file docblock<LF>
264+
*/<LF>
265+
$variable = 3;<LF>
266+
}<LF>
267+
}<LF>
268+
269+
=====================================output=====================================
270+
<?php<CRLF>
271+
/**<CRLF>
272+
* @format<CRLF>
273+
*/<CRLF>
274+
<CRLF>
275+
class Something<CRLF>
276+
{<CRLF>
277+
function whatever()<CRLF>
278+
{<CRLF>
279+
/**<CRLF>
280+
* Some mid-file docblock<CRLF>
281+
*/<CRLF>
282+
$variable = 3;<CRLF>
283+
}<CRLF>
284+
}<CRLF>
285+
286+
================================================================================
287+
`;
288+
289+
exports[`pragma-firstdocblockmidfile.php 3`] = `
290+
====================================options=====================================
291+
endOfLine: "lf"
292+
insertPragma: true
293+
parsers: ["php"]
294+
printWidth: 80
295+
| printWidth
296+
=====================================input======================================
297+
<?php<LF>
298+
<LF>
299+
class Something<LF>
300+
{<LF>
301+
function whatever ()<LF>
302+
{<LF>
303+
/**<LF>
304+
* Some mid-file docblock<LF>
305+
*/<LF>
306+
$variable = 3;<LF>
307+
}<LF>
308+
}<LF>
309+
310+
=====================================output=====================================
311+
<?php<LF>
312+
/**<LF>
313+
* @format<LF>
314+
*/<LF>
315+
<LF>
316+
class Something<LF>
317+
{<LF>
318+
function whatever()<LF>
319+
{<LF>
320+
/**<LF>
321+
* Some mid-file docblock<LF>
322+
*/<LF>
323+
$variable = 3;<LF>
324+
}<LF>
325+
}<LF>
326+
327+
================================================================================
328+
`;
329+
207330
exports[`pragma-nodocblock-CRLF.php 1`] = `
208331
====================================options=====================================
209332
endOfLine: "cr"
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
class Something
4+
{
5+
function whatever ()
6+
{
7+
/**
8+
* Some mid-file docblock
9+
*/
10+
$variable = 3;
11+
}
12+
}

0 commit comments

Comments
 (0)