Skip to content

Commit f6cc2db

Browse files
authored
Ignore comments in an ignored block (#428)
* updating dependencies * linting test config * comments inside blocks are considered as already printed * couldn`t get around this one
1 parent d36bc9d commit f6cc2db

File tree

9 files changed

+7950
-5217
lines changed

9 files changed

+7950
-5217
lines changed

package-lock.json

Lines changed: 7810 additions & 5153 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,22 +61,22 @@
6161
"node": ">=10"
6262
},
6363
"devDependencies": {
64-
"codecov": "^3.6.5",
65-
"eslint": "^7.3.1",
66-
"eslint-config-airbnb-base": "^14.1.0",
64+
"codecov": "^3.8.1",
65+
"eslint": "^7.19.0",
66+
"eslint-config-airbnb-base": "^14.2.1",
6767
"eslint-config-prettier": "^7.0.0",
68-
"eslint-plugin-import": "^2.20.2",
69-
"jest": "^26.0.1",
68+
"eslint-plugin-import": "^2.22.1",
69+
"jest": "^26.6.3",
7070
"jest-mock-now": "^1.3.0",
71-
"jest-watch-typeahead": "^0.6.0"
71+
"jest-watch-typeahead": "^0.6.1"
7272
},
7373
"dependencies": {
7474
"@solidity-parser/parser": "^0.11.0",
7575
"dir-to-object": "^2.0.0",
76-
"emoji-regex": "^9.0.0",
76+
"emoji-regex": "^9.2.1",
7777
"escape-string-regexp": "^4.0.0",
78-
"prettier": "^2.0.5",
79-
"semver": "^7.3.2",
78+
"prettier": "^2.2.1",
79+
"semver": "^7.3.4",
8080
"solidity-comments-extractor": "^0.0.4",
8181
"string-width": "^4.2.0"
8282
}

src/comments/ignore.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function ignoreComments(node) {
2+
if (node === undefined || node === null) return;
3+
const keys = Object.keys(node);
4+
keys.forEach((key) => {
5+
if (key === 'comments') {
6+
node.comments.forEach((comment) => {
7+
// eslint-disable-next-line no-param-reassign
8+
comment.printed = true;
9+
});
10+
return;
11+
}
12+
if (typeof node[key] === 'object') {
13+
ignoreComments(node[key]);
14+
}
15+
});
16+
}
17+
18+
module.exports = ignoreComments;

src/printer.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const nodes = require('./nodes');
22
const { hasNodeIgnoreComment } = require('./prettier-comments/common/util.js');
3+
const ignoreComments = require('./comments/ignore.js');
34

45
function genericPrint(path, options, print) {
56
const node = path.getValue();
@@ -12,6 +13,8 @@ function genericPrint(path, options, print) {
1213
}
1314

1415
if (hasNodeIgnoreComment(node)) {
16+
ignoreComments(node);
17+
1518
return options.originalText.slice(
1619
options.locStart(node),
1720
options.locEnd(node) + 1

tests/PrettierIgnore/PrettierIgnore.sol

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,20 @@ contract PrettierIgnore {
1616
);
1717
}
1818
}
19+
20+
// prettier-ignore
21+
contract Example {
22+
// Test comment
23+
function() public payable {
24+
// this should be marked as printed
25+
// Everything inside is also ignored
26+
matrix(
27+
1, 0, 0,
28+
0, 1, 0,
29+
0, 0, 1
30+
);
31+
if (true) {
32+
// comments of children should be marked as printed
33+
}
34+
}
35+
}

tests/PrettierIgnore/__snapshots__/jsfmt.spec.js.snap

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,23 @@ contract PrettierIgnore {
1919
);
2020
}
2121
}
22+
23+
// prettier-ignore
24+
contract Example {
25+
// Test comment
26+
function() public payable {
27+
// this should be marked as printed
28+
// Everything inside is also ignored
29+
matrix(
30+
1, 0, 0,
31+
0, 1, 0,
32+
0, 0, 1
33+
);
34+
if (true) {
35+
// comments of children should be marked as printed
36+
}
37+
}
38+
}
2239
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2340
pragma solidity ^0.5.2;
2441
@@ -35,4 +52,21 @@ contract PrettierIgnore {
3552
}
3653
}
3754
55+
// prettier-ignore
56+
contract Example {
57+
// Test comment
58+
function() public payable {
59+
// this should be marked as printed
60+
// Everything inside is also ignored
61+
matrix(
62+
1, 0, 0,
63+
0, 1, 0,
64+
0, 0, 1
65+
);
66+
if (true) {
67+
// comments of children should be marked as printed
68+
}
69+
}
70+
}
71+
3872
`;

tests_config/helpers.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
const prettier = require('prettier');
4+
5+
function mergeDefaultOptions(parserConfig) {
6+
return {
7+
plugins: [path.dirname(__dirname)],
8+
printWidth: 80,
9+
...parserConfig
10+
};
11+
}
12+
13+
function prettyprint(src, options) {
14+
const result = prettier.formatWithCursor(src, options);
15+
if (options.cursorOffset >= 0) {
16+
result.formatted = `${result.formatted.slice(
17+
0,
18+
result.cursorOffset
19+
)}<|>${result.formatted.slice(result.cursorOffset)}`;
20+
}
21+
return result.formatted;
22+
}
23+
24+
/**
25+
* Wraps a string in a marker object that is used by `./raw-serializer.js` to
26+
* directly print that string in a snapshot without escaping all double quotes.
27+
* Backticks will still be escaped.
28+
*/
29+
function raw(string) {
30+
if (typeof string !== 'string') {
31+
throw new Error('Raw snapshots have to be strings.');
32+
}
33+
return { [Symbol.for('raw')]: string };
34+
}
35+
36+
function read(filename) {
37+
return fs.readFileSync(filename, 'utf8');
38+
}
39+
40+
function parse(string, opts) {
41+
return prettier.__debug.parse(string, opts, /* massage */ true).ast;
42+
}
43+
44+
module.exports = { mergeDefaultOptions, parse, prettyprint, raw, read };

tests_config/raw-serializer.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
// source: https://github.com/prettier/prettier/blob/ee2839bacbf6a52d004fa2f0373b732f6f191ccc/tests_config/raw-serializer.js
2-
'use strict';
3-
42
const RAW = Symbol.for('raw');
53

64
module.exports = {

tests_config/run_spec.js

Lines changed: 15 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
// source: https://github.com/prettier/prettier/blob/ee2839bacbf6a52d004fa2f0373b732f6f191ccc/tests_config/run_spec.js
2-
'use strict';
3-
42
const fs = require('fs');
53
const path = require('path');
4+
const {
5+
read,
6+
mergeDefaultOptions,
7+
prettyprint,
8+
raw,
9+
parse
10+
} = require('./helpers');
611

7-
const AST_COMPARE = process.env['AST_COMPARE'];
8-
9-
const prettier = require('prettier');
12+
const { AST_COMPARE } = process.env;
1013

11-
function run_spec(dirname, options) {
12-
fs.readdirSync(dirname).forEach(filename => {
13-
const filepath = dirname + '/' + filename;
14+
function runSpec(dirname, options) {
15+
fs.readdirSync(dirname).forEach((filename) => {
16+
const filepath = `${dirname}/${filename}`;
1417
if (
1518
path.extname(filename) !== '.snap' &&
1619
fs.lstatSync(filepath).isFile() &&
@@ -45,16 +48,16 @@ function run_spec(dirname, options) {
4548
const output = prettyprint(input, mergedOptions);
4649
test(filename, () => {
4750
expect(
48-
raw(source + '~'.repeat(mergedOptions.printWidth) + '\n' + output)
51+
raw(`${source + '~'.repeat(mergedOptions.printWidth)}\n${output}`)
4952
).toMatchSnapshot();
5053
});
5154

5255
if (AST_COMPARE) {
5356
test(`${filepath} parse`, () => {
54-
const compareOptions = Object.assign({}, mergedOptions);
57+
const compareOptions = { ...mergedOptions };
5558
delete compareOptions.cursorOffset;
5659
const astMassaged = parse(input, compareOptions);
57-
let ppastMassaged = undefined;
60+
let ppastMassaged;
5861

5962
expect(() => {
6063
ppastMassaged = parse(
@@ -73,45 +76,4 @@ function run_spec(dirname, options) {
7376
});
7477
}
7578

76-
global.run_spec = run_spec;
77-
78-
function parse(string, opts) {
79-
return prettier.__debug.parse(string, opts, /* massage */ true).ast;
80-
}
81-
82-
function prettyprint(src, options) {
83-
const result = prettier.formatWithCursor(src, options);
84-
if (options.cursorOffset >= 0) {
85-
result.formatted =
86-
result.formatted.slice(0, result.cursorOffset) +
87-
'<|>' +
88-
result.formatted.slice(result.cursorOffset);
89-
}
90-
return result.formatted;
91-
}
92-
93-
function read(filename) {
94-
return fs.readFileSync(filename, 'utf8');
95-
}
96-
97-
/**
98-
* Wraps a string in a marker object that is used by `./raw-serializer.js` to
99-
* directly print that string in a snapshot without escaping all double quotes.
100-
* Backticks will still be escaped.
101-
*/
102-
function raw(string) {
103-
if (typeof string !== 'string') {
104-
throw new Error('Raw snapshots have to be strings.');
105-
}
106-
return { [Symbol.for('raw')]: string };
107-
}
108-
109-
function mergeDefaultOptions(parserConfig) {
110-
return Object.assign(
111-
{
112-
plugins: [path.dirname(__dirname)],
113-
printWidth: 80
114-
},
115-
parserConfig
116-
);
117-
}
79+
global.run_spec = runSpec;

0 commit comments

Comments
 (0)