Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .c8rc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"functions": 90,
"statements": 90,
"exclude": ["/node_modules/"],
"include": ["src/**/*.ts", "!src/slang-utils/backward-compatibility.ts"],
"include": ["src/**/*.ts"],
"reporter": ["lcov", "text"],
"temp-dir": "./coverage/"
}
3 changes: 1 addition & 2 deletions src/binary-operator-printers/logical.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import { shouldGroupOrIndent } from './utils/should-group-or-indent.js';
const { indent } = doc.builders;

const indentIfNecessaryBuilder = (path, options) => (document) => {
let node = path.getNode();
for (let i = 0; ; i += 1) {
for (let i = 0, { node } = path; ; i += 1) {
const parentNode = path.getParentNode(i);
if (notIndentParentTypes.includes(parentNode.type)) return document;
if (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const rightOperandPrinter = (node, path, print, options) => {

// If it's a single binary operation, avoid having a small right
// operand like - 1 on its own line
const parent = path.getParentNode();
const { parent } = path;
return node.left.type !== 'BinaryOperation' &&
(parent.type !== 'BinaryOperation' || assignment.match(parent.operator))
? group(right)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const { group } = doc.builders;

export const createGroupIfNecessaryBuilder =
(shouldIndentMatchers) => (path) => (document) => {
const parentNode = path.getParentNode();
if (shouldGroupOrIndent(parentNode, shouldIndentMatchers))
const { parent } = path;
if (shouldGroupOrIndent(parent, shouldIndentMatchers))
return group(document);
return document;
};
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ export const notIndentParentTypes = [

export const createIndentIfNecessaryBuilder =
(shouldIndentMatchers) => (path) => (document) => {
let node = path.getNode();
for (let i = 0; ; i += 1) {
for (let i = 0, { node } = path; ; i += 1) {
const parentNode = path.getParentNode(i);
if (notIndentParentTypes.includes(parentNode.type)) return document;
if (shouldGroupOrIndent(parentNode, shouldIndentMatchers))
Expand Down
11 changes: 7 additions & 4 deletions src/comments/handlers/handleContractDefinitionComments.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { util } from 'prettier';
import { getNextNonSpaceNonCommentCharacter } from '../../common/backward-compatibility.js';

const { addLeadingComment, addTrailingComment, addDanglingComment } = util;
const {
addLeadingComment,
addTrailingComment,
addDanglingComment,
getNextNonSpaceNonCommentCharacter
} = util;

function handleContractDefinitionComments({
text,
Expand All @@ -22,8 +26,7 @@ function handleContractDefinitionComments({
// it is a {}.
const nextCharacter = getNextNonSpaceNonCommentCharacter(
text,
comment,
options.locEnd
options.locEnd(comment)
);

// The comment is behind the start of the Block `{}` or behind a base contract
Expand Down
11 changes: 7 additions & 4 deletions src/comments/handlers/handleModifierInvocationComments.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { util } from 'prettier';
import { getNextNonSpaceNonCommentCharacter } from '../../common/backward-compatibility.js';

const { addLeadingComment, addTrailingComment, addDanglingComment } = util;
const {
addLeadingComment,
addTrailingComment,
addDanglingComment,
getNextNonSpaceNonCommentCharacter
} = util;

function handleModifierInvocationComments({
text,
Expand All @@ -21,8 +25,7 @@ function handleModifierInvocationComments({
// it is a ().
const nextCharacter = getNextNonSpaceNonCommentCharacter(
text,
comment,
options.locEnd
options.locEnd(comment)
);

// The comment is behind the start of the Parentheses `()`
Expand Down
34 changes: 0 additions & 34 deletions src/common/backward-compatibility.js

This file was deleted.

19 changes: 4 additions & 15 deletions src/common/printer-helpers.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { doc } from 'prettier';
import {
isLast,
isNextLineEmpty,
isPrettier2
} from './backward-compatibility.js';
import { doc, util } from 'prettier';

const { group, indent, join, line, softline, hardline } = doc.builders;
const { isNextLineEmpty } = util;

export const printComments = (node, path, options, filter = () => true) => {
if (!node.comments) return '';
Expand All @@ -26,14 +22,7 @@ export const printComments = (node, path, options, filter = () => true) => {
.filter(Boolean)
);

// The following if statement will never be 100% covered in a single run
// since it depends on the version of Prettier being used.
// Mocking the behaviour will introduce a lot of maintenance in the tests.
/* c8 ignore start */
return isPrettier2
? document.parts // Prettier V2
: document; // Prettier V3
/* c8 ignore stop */
return document;
};

export function printPreservingEmptyLines(path, key, options, print) {
Expand All @@ -56,7 +45,7 @@ export function printPreservingEmptyLines(path, key, options, print) {

// Only attempt to append an empty line if `node` is not the last item
if (
!isLast(childPath, key, index) &&
!childPath.isLast &&
isNextLineEmpty(options.originalText, options.locEnd(node) + 1)
) {
// Append an empty line if the original text already had an one after
Expand Down
4 changes: 2 additions & 2 deletions src/nodes/Conditional.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { printSeparatedItem } from '../common/printer-helpers.js';
const { group, hardline, ifBreak, indent, line, softline } = doc.builders;

const experimentalTernaries = (node, path, print, options) => {
const parent = path.getParentNode();
const { parent } = path;
const isNested = parent.type === 'Conditional';
const isNestedAsTrueExpression = isNested && parent.trueExpression === node;
const falseExpressionIsNested = node.falseExpression.type === 'Conditional';
Expand Down Expand Up @@ -68,7 +68,7 @@ const traditionalTernaries = (path, print) =>
indent([
// Nested trueExpression and falseExpression are always printed in a new
// line
path.getParentNode().type === 'Conditional' ? hardline : line,
path.parent.type === 'Conditional' ? hardline : line,
'? ',
path.call(print, 'trueExpression'),
line,
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/ExpressionStatement.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const ExpressionStatement = {
print: ({ node, options, path, print }) => {
const parts = [];

const parent = path.getParentNode();
const { parent } = path;

if (parent.type === 'IfStatement') {
if (node.comments?.length) {
Expand Down
7 changes: 3 additions & 4 deletions src/nodes/FunctionDefinition.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { doc } from 'prettier';
import { getNextNonSpaceNonCommentCharacter } from '../common/backward-compatibility.js';
import { doc, util } from 'prettier';
import {
printComments,
printSeparatedItem,
printSeparatedList
} from '../common/printer-helpers.js';

const { dedent, group, indent, join, line } = doc.builders;
const { getNextNonSpaceNonCommentCharacter } = util;

const functionName = (node, options) => {
if (node.isConstructor && !node.name) return 'constructor';
Expand Down Expand Up @@ -40,8 +40,7 @@ const parameters = (parametersType, node, path, print, options) => {
(comment) =>
getNextNonSpaceNonCommentCharacter(
options.originalText,
comment,
options.locEnd
options.locEnd(comment)
) === ')'
);
return parameterComments.length > 0
Expand Down
26 changes: 10 additions & 16 deletions src/prettier-comments/language-js/comments.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { util } from "prettier";
import { getNextNonSpaceNonCommentCharacter } from "../../common/backward-compatibility.js";

const {
addLeadingComment,
addTrailingComment,
addDanglingComment,
hasNewline,
hasNewlineInRange
hasNewlineInRange,
getNextNonSpaceNonCommentCharacter,
getNextNonSpaceNonCommentCharacterIndex
} = util

export function handleOwnLineComment(comment, text, options, ast, isLastComment) {
Expand Down Expand Up @@ -242,8 +243,7 @@ function handleIfStatementComments(
// it is a ).
const nextCharacter = getNextNonSpaceNonCommentCharacter(
text,
comment,
options.locEnd
options.locEnd(comment)
);
if (nextCharacter === ")") {
addTrailingComment(precedingNode, comment);
Expand Down Expand Up @@ -311,8 +311,7 @@ function handleWhileComments(
// it is a ).
const nextCharacter = getNextNonSpaceNonCommentCharacter(
text,
comment,
options.locEnd
options.locEnd(comment)
);
if (nextCharacter === ")") {
addTrailingComment(precedingNode, comment);
Expand Down Expand Up @@ -470,8 +469,7 @@ function handleMethodNameComments(
// comment should be attached to value instead of key
getNextNonSpaceNonCommentCharacter(
text,
precedingNode,
options.locEnd
options.locEnd(precedingNode)
) !== ":"
) {
addTrailingComment(precedingNode, comment);
Expand Down Expand Up @@ -507,8 +505,7 @@ function handleFunctionNameComments(
if (
getNextNonSpaceNonCommentCharacter(
text,
comment,
options.locEnd
options.locEnd(comment)
) !== "("
) {
return false;
Expand Down Expand Up @@ -536,8 +533,7 @@ function handleCommentAfterArrowParams(text, enclosingNode, comment, options) {

const index = getNextNonSpaceNonCommentCharacterIndex(
text,
comment,
options
options.locEnd(comment)
);
if (text.substr(index, 2) === "=>") {
addDanglingComment(enclosingNode, comment);
Expand All @@ -551,8 +547,7 @@ function handleCommentInEmptyParens(text, enclosingNode, comment, options) {
if (
getNextNonSpaceNonCommentCharacter(
text,
comment,
options.locEnd
options.locEnd(comment)
) !== ")"
) {
return false;
Expand Down Expand Up @@ -622,8 +617,7 @@ function handleLastFunctionArgComments(
enclosingNode.type === "ClassMethod") &&
getNextNonSpaceNonCommentCharacter(
text,
comment,
options.locEnd
options.locEnd(comment)
) === ")"
) {
addTrailingComment(precedingNode, comment);
Expand Down
5 changes: 1 addition & 4 deletions src/printer.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import * as nodes from './nodes/index.js';
import { printWarning } from './common/print-warning.js';
import {
hasNodeIgnoreComment,
prettierVersionSatisfies
} from './common/util.js';
import { hasNodeIgnoreComment } from './common/util.js';
import ignoreComments from './comments/ignore.js';

function once(factory) {
Expand Down