Skip to content

Commit 2bf85b9

Browse files
Jantherfvictorio
andauthored
Remove strip-comments dependency (#1066)
* removing strip-comments * updating dependencies * rebase fix * adding missing test * Include filename in error * adding the case where there is no filepath in the options --------- Co-authored-by: Franco Victorio <[email protected]>
1 parent 4c15c9b commit 2bf85b9

File tree

7 files changed

+117
-80
lines changed

7 files changed

+117
-80
lines changed

package-lock.json

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

package.json

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,8 @@
8686
"@babel/code-frame": "^7.26.2",
8787
"@types/jest": "^29.5.14",
8888
"@types/semver": "^7.5.8",
89-
"@types/strip-comments": "^2.0.4",
90-
"@typescript-eslint/eslint-plugin": "^8.12.2",
91-
"@typescript-eslint/parser": "^8.12.2",
89+
"@typescript-eslint/eslint-plugin": "^8.14.0",
90+
"@typescript-eslint/parser": "^8.14.0",
9291
"c8": "^9.1.0",
9392
"cross-env": "^7.0.3",
9493
"eslint": "^9.14.0",
@@ -111,8 +110,7 @@
111110
"dependencies": {
112111
"@nomicfoundation/slang": "0.18.3",
113112
"@solidity-parser/parser": "^0.18.0",
114-
"semver": "^7.6.3",
115-
"strip-comments": "^2.0.1"
113+
"semver": "^7.6.3"
116114
},
117115
"peerDependencies": {
118116
"prettier": ">=3.0.0"

src/slang-utils/create-parser.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import { VersionExpressionSets as SlangVersionExpressionSets } from '@nomicfoundation/slang/ast';
12
import { NonterminalKind, Query } from '@nomicfoundation/slang/cst';
23
import { Parser } from '@nomicfoundation/slang/parser';
3-
import strip from 'strip-comments';
44
import {
55
maxSatisfying,
66
minSatisfying,
@@ -9,6 +9,9 @@ import {
99
satisfies,
1010
validRange
1111
} from 'semver';
12+
import { VersionExpressionSets } from '../slang-nodes/VersionExpressionSets.js';
13+
14+
import type { ParserOptions } from 'prettier';
1215

1316
const supportedVersions = Parser.supportedVersions();
1417

@@ -25,10 +28,14 @@ const milestoneVersions = Array.from(
2528
return versions;
2629
}, []);
2730

31+
const query = Query.parse(
32+
'[VersionPragma @versionRanges [VersionExpressionSets]]'
33+
);
34+
2835
// TODO if we ended up selecting the same version that the pragmas were parsed with,
2936
// should we be able to reuse/just return the already parsed CST, instead of
3037
// returning a Parser and forcing user to parse it again?
31-
export function createParser(text: string): Parser {
38+
export function createParser(text: string, options: ParserOptions): Parser {
3239
let inferredRanges: string[] = [];
3340

3441
for (const version of milestoneVersions) {
@@ -42,7 +49,7 @@ export function createParser(text: string): Parser {
4249
(versions, inferredRange) => {
4350
if (!validRange(inferredRange)) {
4451
throw new Error(
45-
"Couldn't infer any version from the ranges in the pragmas."
52+
`Couldn't infer any version from the ranges in the pragmas${options.filepath ? ` for file ${options.filepath}` : ''}`
4653
);
4754
}
4855
return versions.filter((supportedVersion) =>
@@ -60,18 +67,20 @@ export function createParser(text: string): Parser {
6067
function tryToCollectPragmas(text: string, version: string): string[] {
6168
const language = Parser.create(version);
6269
const parseOutput = language.parse(NonterminalKind.SourceUnit, text);
63-
const query = Query.parse(
64-
'[VersionPragma @versionRanges [VersionExpressionSets]]'
65-
);
6670
const matches = parseOutput.createTreeCursor().query([query]);
6771
const ranges: string[] = [];
6872

6973
let match;
7074
while ((match = matches.next())) {
75+
const versionRange = new SlangVersionExpressionSets(
76+
match.captures.versionRanges[0].node.asNonterminalNode()!
77+
);
7178
ranges.push(
72-
strip(match.captures.versionRanges[0].node.unparse(), {
73-
keepProtected: true
74-
})
79+
// Replace all comments that could be in the expression with whitespace
80+
new VersionExpressionSets(versionRange).comments.reduce(
81+
(range, comment) => range.replace(comment.value, ' '),
82+
versionRange.cst.unparse()
83+
)
7584
);
7685
}
7786

src/slangSolidityParser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export default function parse(
2222
const parser =
2323
compiler && supportedVersions.includes(compiler)
2424
? Parser.create(compiler)
25-
: createParser(text);
25+
: createParser(text, options);
2626

2727
const parseOutput = parser.parse(NonterminalKind.SourceUnit, text);
2828
printWarning(
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`FunctionDefinitions.sol format 1`] = `
4+
====================================options=====================================
5+
parsers: ["slang-solidity"]
6+
printWidth: 80
7+
| printWidth
8+
=====================================input======================================
9+
contract FunctionDefinitions {
10+
function () external {}
11+
function () external payable {}
12+
}
13+
14+
=====================================output=====================================
15+
contract FunctionDefinitions {
16+
function() external {}
17+
function() external payable {}
18+
}
19+
20+
================================================================================
21+
`;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
runFormatTest(import.meta, ['slang-solidity']);

tests/unit/slang-utils/create-parser.test.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ describe('inferLanguage', function () {
55
const supportedVersions = Parser.supportedVersions();
66
const latestSupportedVersion =
77
supportedVersions[supportedVersions.length - 1];
8+
const options = { filepath: 'test.sol' };
89

910
const fixtures = [
1011
{
@@ -90,14 +91,39 @@ describe('inferLanguage', function () {
9091

9192
for (const { description, source, version } of fixtures) {
9293
test(description, function () {
93-
const parser = createParser(source);
94+
const parser = createParser(source, options);
9495
expect(parser.version).toEqual(version);
9596
});
9697
}
9798

99+
test('should throw when a pragma is broken by new lines, whitespace and comments', function () {
100+
expect(() =>
101+
createParser(
102+
`pragma solidity 0.
103+
// comment 1
104+
7.
105+
/* comment 2*/
106+
3;`,
107+
options
108+
)
109+
).toThrow(
110+
"Couldn't infer any version from the ranges in the pragmas for file test.sol"
111+
);
112+
expect(() =>
113+
createParser(
114+
`pragma solidity 0.
115+
// comment 1
116+
7.
117+
/* comment 2*/
118+
3;`,
119+
{}
120+
)
121+
).toThrow("Couldn't infer any version from the ranges in the pragmas");
122+
});
123+
98124
test.skip('should throw an error if there are incompatible ranges', function () {
99125
expect(() =>
100-
createParser(`pragma solidity ^0.8.0; pragma solidity 0.7.6;`)
126+
createParser(`pragma solidity ^0.8.0; pragma solidity 0.7.6;`, options)
101127
).toThrow();
102128
});
103129
});

0 commit comments

Comments
 (0)