Skip to content

Commit e7ad30d

Browse files
committed
Clear line when printing warnings
1 parent f213b18 commit e7ad30d

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

src/common/print-warning.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export function printWarning(message) {
2+
// Prettier prints some temporary messages while formatting, and this warning
3+
// can mess with that output. We clear the line and move the cursor to the
4+
// beginning of the line to avoid this.
5+
//
6+
// \x1b: Escape character
7+
// [2K: Escape code to clear the entire line
8+
// \r: Carriage return
9+
const clearLine = '\x1b[2K\r';
10+
11+
console.warn(`${clearLine}[prettier-solidity] ${message}`);
12+
}

src/parser.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import parser from '@solidity-parser/parser';
33
import coerce from 'semver/functions/coerce.js';
44
import satisfies from 'semver/functions/satisfies.js';
55

6+
import { printWarning } from './common/print-warning.js';
7+
68
const tryHug = (node, operators) => {
79
if (node.type === 'BinaryOperation' && operators.includes(node.operator))
810
return {
@@ -27,8 +29,8 @@ function parse(text, _parsers, options = _parsers) {
2729
if (!satisfies(compiler, ctx.value)) {
2830
// @TODO: investigate the best way to warn that would apply to
2931
// different editors.
30-
console.warn(
31-
`[prettier-solidity] The compiler option is set to '${options.compiler}', which does not satisfy 'pragma solidity ${ctx.value}'.`
32+
printWarning(
33+
`The compiler option is set to '${options.compiler}', which does not satisfy 'pragma solidity ${ctx.value}'.`
3234
);
3335
}
3436
},

src/printer.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as nodes from './nodes/index.js';
2+
import { printWarning } from './common/print-warning.js';
23
import {
34
hasNodeIgnoreComment,
45
prettierVersionSatisfies
@@ -16,7 +17,7 @@ function once(factory) {
1617
}
1718

1819
const warnDeprecation = once(() => {
19-
console.warn(
20+
printWarning(
2021
`'solidity-parse' has been deprecated, please use 'slang-solidity'.`
2122
);
2223
return true;

0 commit comments

Comments
 (0)