prettier-plugin-solidity
Version:
A Prettier Plugin for automatically formatting your Solidity code.
35 lines • 1.96 kB
JavaScript
import { util, version } from 'prettier';
import satisfies from 'semver/functions/satisfies.js';
export const prettierVersionSatisfies = (range) => satisfies(version, range);
export function printString(rawContent, options) {
const double = { quote: '"', regex: /"/g };
const single = { quote: "'", regex: /'/g };
const preferred = options.singleQuote ? single : double;
const alternate = preferred === single ? double : single;
let shouldUseAlternateQuote = false;
// If `rawContent` contains at least one of the quote preferred for enclosing
// the string, we might want to enclose with the alternate quote instead, to
// minimize the number of escaped quotes.
// Also check for the alternate quote, to determine if we're allowed to swap
// the quotes on a DirectiveLiteral.
if (rawContent.includes(preferred.quote) ||
rawContent.includes(alternate.quote)) {
const numPreferredQuotes = (rawContent.match(preferred.regex) || []).length;
const numAlternateQuotes = (rawContent.match(alternate.regex) || []).length;
shouldUseAlternateQuote = numPreferredQuotes > numAlternateQuotes;
}
const enclosingQuote = shouldUseAlternateQuote
? alternate.quote
: preferred.quote;
// It might sound unnecessary to use `makeString` even if the string already
// is enclosed with `enclosingQuote`, but it isn't. The string could contain
// unnecessary escapes (such as in `"\'"`). Always using `makeString` makes
// sure that we consistently output the minimum amount of escaped quotes.
return util.makeString(rawContent, enclosingQuote);
}
export function hasNodeIgnoreComment(node) {
var _a;
return (((_a = node === null || node === void 0 ? void 0 : node.comments) === null || _a === void 0 ? void 0 : _a.length) > 0 &&
node.comments.some((comment) => comment.value.trim() === 'prettier-ignore'));
}
//# sourceMappingURL=util.js.map