Skip to content

Commit

Permalink
Merge pull request #1211 from bryceosterhaus/prettierPlugoptioon
Browse files Browse the repository at this point in the history
fix(prettier-plugin): fix issue with eslint-disable and comments below it
  • Loading branch information
bryceosterhaus authored May 14, 2024
2 parents 63f578b + 02d6a9b commit 25abb2e
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 16 deletions.
1 change: 1 addition & 0 deletions projects/prettier-plugin/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@

export {parsers} from './parsers.mjs';
export {printers} from './printers.mjs';
export {options} from './options.mjs';
14 changes: 14 additions & 0 deletions projects/prettier-plugin/options.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* SPDX-FileCopyrightText: © 2020 Liferay, Inc. <https://liferay.com>
* SPDX-License-Identifier: MIT
*/

export const options = {
commentIgnorePatterns: {
array: true,
category: 'Format',
description: 'Ignore comment nodes that contain a given pattern',
since: '1.0.0',
type: 'string',
},
};
7 changes: 6 additions & 1 deletion projects/prettier-plugin/parsers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ function transformParser(parserName, defaultParser) {

const ast = defaultParser.parse(formattedText, options);

formattedText = linesAroundComments(formattedText, ast, parserName);
formattedText = linesAroundComments(
formattedText,
ast,
parserName,
options
);

return {
body: formattedText,
Expand Down
31 changes: 25 additions & 6 deletions projects/prettier-plugin/rules/lines-around-comments.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
* SPDX-License-Identifier: MIT
*/

export function linesAroundComments(formattedText, ast, parserName) {
export function linesAroundComments(formattedText, ast, parserName, options) {
const {commentIgnorePatterns = []} = options;
const totalLines = ast.loc.end.line;

let hasDirective = false;
let linesAdded = 0;
const ignoredLines = [];

/*
* Track where each inline comment is so that we can group them
Expand All @@ -18,7 +20,6 @@ export function linesAroundComments(formattedText, ast, parserName) {
isInlineComment(commentNode) &&
!isEndofLineComment(commentNode, formattedText, parserName)
) {

/*
* Subtract '1' to make it zero based counting
*/
Expand All @@ -37,6 +38,18 @@ export function linesAroundComments(formattedText, ast, parserName) {
let formattedTextByLines = formattedText.split('\n');

ast.comments.forEach((commentNode) => {
/*
* Ignore if comment node value matches option
*/
if (
commentIgnorePatterns.find((pattern) => {
const regex = new RegExp(pattern);

return regex.exec(commentNode.value);
})
) {
return;
}

/*
* Ignore comments that are at the end of a line
Expand Down Expand Up @@ -66,7 +79,8 @@ export function linesAroundComments(formattedText, ast, parserName) {
/*
* Don't add a line after if the comment is for eslint
*/
if (commentNode.value.includes('disable-next-line')) {
if (commentNode.value.includes('eslint-disable')) {
ignoredLines.push(endingLine + 1);
skipAfter = true;
}

Expand All @@ -81,8 +95,14 @@ export function linesAroundComments(formattedText, ast, parserName) {
* Don't add a line before if its the first line in the file
* or
* Don't add a line before if the line above is a directive
* or
* Don't add a line before if the line was ignored by a previous comment
*/
if (startingLine === 0 || (hasDirective && startingLine === 1)) {
if (
startingLine === 0 ||
(hasDirective && startingLine === 1) ||
ignoredLines.includes(startingLine)
) {
skipBefore = true;
}

Expand Down Expand Up @@ -157,8 +177,7 @@ function getContentsBeforeColumn(node, source, parserName) {

if (parserName === 'typescript') {
index = node.range[0];
}
else {
} else {
index = node.loc.start.index;
}

Expand Down
97 changes: 88 additions & 9 deletions projects/prettier-plugin/test/index.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ return false;
`,
},
{
_name: 'disable line after if it is an "ignore" comment',
_name: 'disable line after if it is an "eslint-disable" comment',
code: `if (true) {
// eslint-disable-next-line
var foo = 'test'; // foo
Expand All @@ -134,6 +134,80 @@ return false;
var foo = 'test'; // foo
return false;
}
`,
},
{
_name: 'disable line after if it is an "eslint-disable" block comment',
code: `if (true) {
/* eslint-disable */
var foo = 'test'; // foo
return false;
}`,
expected: `if (true) {
/* eslint-disable */
var foo = 'test'; // foo
return false;
}
`,
},
{
_name: 'ignore triple slash references in TS',
code: `/* eslint-disable */
/// <reference path="foo.d.ts" />
/// <reference path="bar.d.ts" />`,
expected: `/* eslint-disable */
/// <reference path="foo.d.ts" />
/// <reference path="bar.d.ts" />
`,
},
{
_config: {
commentIgnorePatterns: ['ignore-this-comment'],
},
_name: 'respects commentIgnorePatterns option',
code: `var foo = 'bar';
// ignore-this-comment
var bar = 'foo';
`,
expected: `var foo = 'bar';
// ignore-this-comment
var bar = 'foo';
`,
},
{
_config: {
commentIgnorePatterns: ['do-ignore-.*-comment'],
},
_name: 'respects commentIgnorePatterns option with regex',
code: `var foo = 'bar';
// do-ignore-this-comment
// do-not-ignore-this-comment
var bar = 'foo';
`,
expected: `var foo = 'bar';
// do-ignore-this-comment
// do-not-ignore-this-comment
var bar = 'foo';
`,
},
{
_config: {
commentIgnorePatterns: ['mylint-disable-next-line.*'],
},
_name: 'respects commentIgnorePatterns option with regex 2',
code: `// mylint-disable-next-line some-rule
var foo = 'bar';
// mylint-disable
var bar = 'foo';
`,
expected: `// mylint-disable-next-line some-rule
var foo = 'bar';
// mylint-disable
var bar = 'foo';
`,
},
];
Expand All @@ -146,10 +220,15 @@ describe('babel', () => {
});

fixtures.forEach((fixture) => {
test(fixture._name, async () => {
const {_config = {}, _name, code, expected} = fixture;

test(_name, async () => {
assert.equal(
await format(fixture.code, babelConfig),
fixture.expected
await format(code, {
...babelConfig,
..._config,
}),
expected
);
});
});
Expand All @@ -158,15 +237,15 @@ describe('babel', () => {
describe('typescript', () => {
test('prettier runs', async () => {
const code = `if (foo) {}`;

assert.ok(await format(code, tsConfig));
});

fixtures.forEach((fixture) => {
test(fixture._name, async () => {
const {_config = {}, _name, code, expected} = fixture;

test(_name, async () => {
assert.equal(
await format(fixture.code, tsConfig),
fixture.expected
await format(code, {...tsConfig, ..._config}),
expected
);
});
});
Expand Down

0 comments on commit 25abb2e

Please sign in to comment.