From 490cebf03f006ce06a6bee97c044353deaf6c988 Mon Sep 17 00:00:00 2001 From: Klaus Date: Tue, 17 Jun 2025 18:07:28 +0100 Subject: [PATCH 1/3] dropping backward compatibility in the `antlr` parser --- src/binary-operator-printers/logical.js | 2 +- .../create-binary-operation-printer.js | 2 +- .../create-group-if-necessary-builder.js | 4 +-- .../create-indent-if-necessary-builder.js | 2 +- .../handleContractDefinitionComments.js | 11 +++--- .../handleModifierInvocationComments.js | 11 +++--- src/common/backward-compatibility.js | 34 ------------------- src/common/printer-helpers.js | 19 +++-------- src/nodes/Conditional.js | 4 +-- src/nodes/ExpressionStatement.js | 2 +- src/nodes/FunctionDefinition.js | 7 ++-- src/prettier-comments/language-js/comments.js | 26 ++++++-------- src/printer.js | 5 +-- 13 files changed, 40 insertions(+), 89 deletions(-) delete mode 100644 src/common/backward-compatibility.js diff --git a/src/binary-operator-printers/logical.js b/src/binary-operator-printers/logical.js index bd327413c..f2022a4d6 100644 --- a/src/binary-operator-printers/logical.js +++ b/src/binary-operator-printers/logical.js @@ -7,7 +7,7 @@ import { shouldGroupOrIndent } from './utils/should-group-or-indent.js'; const { indent } = doc.builders; const indentIfNecessaryBuilder = (path, options) => (document) => { - let node = path.getNode(); + let { node } = path; for (let i = 0; ; i += 1) { const parentNode = path.getParentNode(i); if (notIndentParentTypes.includes(parentNode.type)) return document; diff --git a/src/binary-operator-printers/printers/create-binary-operation-printer.js b/src/binary-operator-printers/printers/create-binary-operation-printer.js index e7b182967..9f382c8d9 100644 --- a/src/binary-operator-printers/printers/create-binary-operation-printer.js +++ b/src/binary-operator-printers/printers/create-binary-operation-printer.js @@ -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) diff --git a/src/binary-operator-printers/printers/create-group-if-necessary-builder.js b/src/binary-operator-printers/printers/create-group-if-necessary-builder.js index 136f3afd4..cdecfb4d4 100644 --- a/src/binary-operator-printers/printers/create-group-if-necessary-builder.js +++ b/src/binary-operator-printers/printers/create-group-if-necessary-builder.js @@ -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; }; diff --git a/src/binary-operator-printers/printers/create-indent-if-necessary-builder.js b/src/binary-operator-printers/printers/create-indent-if-necessary-builder.js index 3722ed260..f996dec9e 100644 --- a/src/binary-operator-printers/printers/create-indent-if-necessary-builder.js +++ b/src/binary-operator-printers/printers/create-indent-if-necessary-builder.js @@ -12,7 +12,7 @@ export const notIndentParentTypes = [ export const createIndentIfNecessaryBuilder = (shouldIndentMatchers) => (path) => (document) => { - let node = path.getNode(); + let { node } = path; for (let i = 0; ; i += 1) { const parentNode = path.getParentNode(i); if (notIndentParentTypes.includes(parentNode.type)) return document; diff --git a/src/comments/handlers/handleContractDefinitionComments.js b/src/comments/handlers/handleContractDefinitionComments.js index 2a6961692..f4468ea7c 100644 --- a/src/comments/handlers/handleContractDefinitionComments.js +++ b/src/comments/handlers/handleContractDefinitionComments.js @@ -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, @@ -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 diff --git a/src/comments/handlers/handleModifierInvocationComments.js b/src/comments/handlers/handleModifierInvocationComments.js index f9034ff5b..7e8f9c681 100644 --- a/src/comments/handlers/handleModifierInvocationComments.js +++ b/src/comments/handlers/handleModifierInvocationComments.js @@ -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, @@ -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 `()` diff --git a/src/common/backward-compatibility.js b/src/common/backward-compatibility.js deleted file mode 100644 index a8ae3d512..000000000 --- a/src/common/backward-compatibility.js +++ /dev/null @@ -1,34 +0,0 @@ -import { util } from 'prettier'; -import { prettierVersionSatisfies } from './util.js'; - -export const isPrettier2 = prettierVersionSatisfies('^2.3.0'); - -// The functions in this file 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. - -export function isNextLineEmpty(text, startIndex) { - return isPrettier2 - ? util.isNextLineEmptyAfterIndex(text, startIndex) - : util.isNextLineEmpty(text, startIndex); // V3 deprecated `isNextLineEmptyAfterIndex` -} - -export function getNextNonSpaceNonCommentCharacterIndex(text, node, locEnd) { - return isPrettier2 - ? util.getNextNonSpaceNonCommentCharacterIndex(text, node, locEnd) - : util.getNextNonSpaceNonCommentCharacterIndex(text, locEnd(node)); // V3 signature changed -} - -export function getNextNonSpaceNonCommentCharacter(text, node, locEnd) { - return isPrettier2 - ? text.charAt( - util.getNextNonSpaceNonCommentCharacterIndex(text, node, locEnd) - ) - : util.getNextNonSpaceNonCommentCharacter(text, locEnd(node)); // V3 exposes this function directly -} - -export function isLast(path, key, index) { - return isPrettier2 - ? index === path.getParentNode()[key].length - 1 - : path.isLast; -} diff --git a/src/common/printer-helpers.js b/src/common/printer-helpers.js index 1401e1aac..bfd6f146f 100644 --- a/src/common/printer-helpers.js +++ b/src/common/printer-helpers.js @@ -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 ''; @@ -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) { @@ -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 diff --git a/src/nodes/Conditional.js b/src/nodes/Conditional.js index e49f03b81..2c1f408eb 100644 --- a/src/nodes/Conditional.js +++ b/src/nodes/Conditional.js @@ -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'; @@ -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, diff --git a/src/nodes/ExpressionStatement.js b/src/nodes/ExpressionStatement.js index 6f40afe7d..5149f8358 100644 --- a/src/nodes/ExpressionStatement.js +++ b/src/nodes/ExpressionStatement.js @@ -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) { diff --git a/src/nodes/FunctionDefinition.js b/src/nodes/FunctionDefinition.js index f6e31955e..5a3d9c69e 100644 --- a/src/nodes/FunctionDefinition.js +++ b/src/nodes/FunctionDefinition.js @@ -1,5 +1,4 @@ -import { doc } from 'prettier'; -import { getNextNonSpaceNonCommentCharacter } from '../common/backward-compatibility.js'; +import { doc, util } from 'prettier'; import { printComments, printSeparatedItem, @@ -7,6 +6,7 @@ import { } 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'; @@ -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 diff --git a/src/prettier-comments/language-js/comments.js b/src/prettier-comments/language-js/comments.js index 7b0003828..675e952bc 100644 --- a/src/prettier-comments/language-js/comments.js +++ b/src/prettier-comments/language-js/comments.js @@ -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) { @@ -242,8 +243,7 @@ function handleIfStatementComments( // it is a ). const nextCharacter = getNextNonSpaceNonCommentCharacter( text, - comment, - options.locEnd + options.locEnd(comment) ); if (nextCharacter === ")") { addTrailingComment(precedingNode, comment); @@ -311,8 +311,7 @@ function handleWhileComments( // it is a ). const nextCharacter = getNextNonSpaceNonCommentCharacter( text, - comment, - options.locEnd + options.locEnd(comment) ); if (nextCharacter === ")") { addTrailingComment(precedingNode, comment); @@ -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); @@ -507,8 +505,7 @@ function handleFunctionNameComments( if ( getNextNonSpaceNonCommentCharacter( text, - comment, - options.locEnd + options.locEnd(comment) ) !== "(" ) { return false; @@ -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); @@ -551,8 +547,7 @@ function handleCommentInEmptyParens(text, enclosingNode, comment, options) { if ( getNextNonSpaceNonCommentCharacter( text, - comment, - options.locEnd + options.locEnd(comment) ) !== ")" ) { return false; @@ -622,8 +617,7 @@ function handleLastFunctionArgComments( enclosingNode.type === "ClassMethod") && getNextNonSpaceNonCommentCharacter( text, - comment, - options.locEnd + options.locEnd(comment) ) === ")" ) { addTrailingComment(precedingNode, comment); diff --git a/src/printer.js b/src/printer.js index b3aa11560..4fb44c3e9 100644 --- a/src/printer.js +++ b/src/printer.js @@ -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) { From 8e21c77461d0352fb45429677214a84a1146b247 Mon Sep 17 00:00:00 2001 From: Klaus Date: Tue, 17 Jun 2025 18:13:46 +0100 Subject: [PATCH 2/3] removing backward-compatibility from c8 configuration --- .c8rc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.c8rc b/.c8rc index 8a83706f2..8510c2fd2 100644 --- a/.c8rc +++ b/.c8rc @@ -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/" } From 9cad3a0cef0b1f60d78171c74009a1c3deed7a05 Mon Sep 17 00:00:00 2001 From: Klaus Date: Wed, 18 Jun 2025 09:06:02 +0100 Subject: [PATCH 3/3] initialising variable in the for loop instead of outside of it --- src/binary-operator-printers/logical.js | 3 +-- .../printers/create-indent-if-necessary-builder.js | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/binary-operator-printers/logical.js b/src/binary-operator-printers/logical.js index f2022a4d6..089b57217 100644 --- a/src/binary-operator-printers/logical.js +++ b/src/binary-operator-printers/logical.js @@ -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; - 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 ( diff --git a/src/binary-operator-printers/printers/create-indent-if-necessary-builder.js b/src/binary-operator-printers/printers/create-indent-if-necessary-builder.js index f996dec9e..a3444f654 100644 --- a/src/binary-operator-printers/printers/create-indent-if-necessary-builder.js +++ b/src/binary-operator-printers/printers/create-indent-if-necessary-builder.js @@ -12,8 +12,7 @@ export const notIndentParentTypes = [ export const createIndentIfNecessaryBuilder = (shouldIndentMatchers) => (path) => (document) => { - let { node } = path; - 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))