Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(language-js/pragma.js): support pragma in comments #5357

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/language-js/pragma.js
Expand Up @@ -2,9 +2,32 @@

const docblock = require("jest-docblock");

function hasPragmaInLeadingComments(text) {
// ignore leading hashbang
text = text.replace(/^#!.*/, "");
const leadingCommentRegExp = /\s+|\/\/(.*)|\/\*([^]*?)\*\//gm;
let match;
let lastIndex = 0;
while ((match = leadingCommentRegExp.exec(text))) {
if (match.index !== lastIndex) {
return false;
}
lastIndex = match.index + match[0].length;
const comment = match[1] || match[2];
if (/(\s|^)@(prettier|format)(\s|$)/.test(comment)) {
return true;
}
}
return false;
}

function hasPragma(text) {
const pragmas = Object.keys(docblock.parse(docblock.extract(text)));
return pragmas.indexOf("prettier") !== -1 || pragmas.indexOf("format") !== -1;
return (
pragmas.indexOf("prettier") !== -1 ||
pragmas.indexOf("format") !== -1 ||
hasPragmaInLeadingComments(text)
);
}

function insertPragma(text) {
Expand Down
64 changes: 64 additions & 0 deletions tests/require-pragma/js/__snapshots__/jsfmt.spec.js.snap
@@ -1,5 +1,69 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`module-with-block-comment-pragma.js - flow-verify 1`] = `
/**
* @flow
*/
// this is a test
/*
*
@prettier
*/

function foo(bar)


{

return bar +
3 +
4;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/**
* @flow
*/
// this is a test
/*
*
@prettier
*/

function foo(bar) {
return bar + 3 + 4;
}

`;

exports[`module-with-inline-comment-pragma.js - flow-verify 1`] = `
/**
* @flow
*/
// this is a test
// @prettier

function foo(bar)


{

return bar +
3 +
4;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/**
* @flow
*/
// this is a test
// @prettier

function foo(bar) {
return bar + 3 + 4;
}

`;

exports[`module-with-pragma.js - flow-verify 1`] = `
/**
* @flow
Expand Down
18 changes: 18 additions & 0 deletions tests/require-pragma/js/module-with-block-comment-pragma.js
@@ -0,0 +1,18 @@
/**
* @flow
*/
// this is a test
/*
*
@prettier
*/

function foo(bar)


{

return bar +
3 +
4;
}
15 changes: 15 additions & 0 deletions tests/require-pragma/js/module-with-inline-comment-pragma.js
@@ -0,0 +1,15 @@
/**
* @flow
*/
// this is a test
// @prettier

function foo(bar)


{

return bar +
3 +
4;
}
@@ -0,0 +1,25 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`ignores non-leading // @prettier comment (stderr) 1`] = `""`;

exports[`ignores non-leading // @prettier comment (write) 1`] = `Array []`;

exports[`supports /* @flow @prettier */ comment (stderr) 1`] = `""`;

exports[`supports /* @flow @prettier */ comment (write) 1`] = `Array []`;

exports[`supports /* @prettier */ comment (stderr) 1`] = `""`;

exports[`supports /* @prettier */ comment (write) 1`] = `Array []`;

exports[`supports // @format comment (stderr) 1`] = `""`;

exports[`supports // @format comment (write) 1`] = `Array []`;

exports[`supports // @prettier comment (stderr) 1`] = `""`;

exports[`supports // @prettier comment (write) 1`] = `Array []`;

exports[`supports // @prettier comment after hashbang (stderr) 1`] = `""`;

exports[`supports // @prettier comment after hashbang (write) 1`] = `Array []`;
144 changes: 144 additions & 0 deletions tests_integration/__tests__/pragma-in-comments.js
@@ -0,0 +1,144 @@
"use strict";

const runPrettier = require("../runPrettier");

expect.addSnapshotSerializer(require("../path-serializer"));

describe("supports // @prettier comment", () => {
runPrettier("cli/", ["--stdin-filepath", "abc.js", "--require-pragma"], {
input: `/**
* blah
*/
// @flow
// @prettier


var foo = 2
`
}).test({
stdout: `/**
* blah
*/
// @flow
// @prettier

var foo = 2;
`,
status: 0
});
});

describe("supports // @prettier comment after hashbang", () => {
runPrettier("cli/", ["--stdin-filepath", "abc.js", "--require-pragma"], {
input: `#!/usr/bin/env node
/**
* blah
*/
// @flow
// @prettier


var foo = 2
`
}).test({
stdout: `#!/usr/bin/env node
/**
* blah
*/
// @flow
// @prettier

var foo = 2;
`,
status: 0
});
});

describe("supports // @format comment", () => {
runPrettier("cli/", ["--stdin-filepath", "abc.js", "--require-pragma"], {
input: `/**
* blah
*/
// @flow
// @format


var foo = 2
`
}).test({
stdout: `/**
* blah
*/
// @flow
// @format

var foo = 2;
`,
status: 0
});
});

describe("supports /* @prettier */ comment", () => {
runPrettier("cli/", ["--stdin-filepath", "abc.js", "--require-pragma"], {
input: `/**
* blah
*/
// @flow
/* @prettier */


var foo = 2
`
}).test({
stdout: `/**
* blah
*/
// @flow
/* @prettier */

var foo = 2;
`,
status: 0
});
});

describe("supports /* @flow @prettier */ comment", () => {
runPrettier("cli/", ["--stdin-filepath", "abc.js", "--require-pragma"], {
input: `/**
* blah
*/
/* @flow @prettier */


var foo = 2
`
}).test({
stdout: `/**
* blah
*/
/* @flow @prettier */

var foo = 2;
`,
status: 0
});
});

describe("ignores non-leading // @prettier comment", () => {
const input = `/**
* blah
*/
// @flow
"use strict";
// @prettier


var foo = 2
`;
runPrettier("cli/", ["--stdin-filepath", "abc.js", "--require-pragma"], {
input
}).test({
stdout: input,
status: 0
});
});