prettier-plugin-solidity 2.0.0-beta → 2.0.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/comments/handlers/index.js +0 -1
- package/dist/comments/handlers/index.js.map +1 -1
- package/dist/common/print-warning.js +12 -0
- package/dist/common/print-warning.js.map +1 -0
- package/dist/index.js +18 -16
- package/dist/index.js.map +1 -1
- package/dist/parser.js +2 -2
- package/dist/parser.js.map +1 -1
- package/dist/printer.js +2 -1
- package/dist/printer.js.map +1 -1
- package/dist/slang-comments/handlers/handle-contract-definition-comments.js +0 -7
- package/dist/slang-comments/handlers/handle-contract-definition-comments.js.map +1 -1
- package/dist/slang-comments/handlers/handle-modifier-invocation-comments.js +27 -0
- package/dist/slang-comments/handlers/handle-modifier-invocation-comments.js.map +1 -0
- package/dist/slang-comments/handlers/index.js +2 -0
- package/dist/slang-comments/handlers/index.js.map +1 -1
- package/dist/slang-nodes/ContractDefinition.js +1 -2
- package/dist/slang-nodes/ContractDefinition.js.map +1 -1
- package/dist/slang-nodes/FunctionDefinition.js +1 -2
- package/dist/slang-nodes/FunctionDefinition.js.map +1 -1
- package/dist/slang-nodes/ImportDeconstructionSymbols.js +1 -2
- package/dist/slang-nodes/ImportDeconstructionSymbols.js.map +1 -1
- package/dist/slang-nodes/ModifierInvocation.js +4 -2
- package/dist/slang-nodes/ModifierInvocation.js.map +1 -1
- package/dist/slang-nodes/RevertStatement.js +2 -2
- package/dist/slang-nodes/RevertStatement.js.map +1 -1
- package/dist/slang-nodes/{VersionSpecifiers.js → SimpleVersionLiteral.js} +3 -3
- package/dist/slang-nodes/SimpleVersionLiteral.js.map +1 -0
- package/dist/slang-nodes/SourceUnit.js +11 -2
- package/dist/slang-nodes/SourceUnit.js.map +1 -1
- package/dist/slang-nodes/VersionExpression.js +12 -25
- package/dist/slang-nodes/VersionExpression.js.map +1 -1
- package/dist/slang-nodes/VersionLiteral.js +24 -0
- package/dist/slang-nodes/VersionLiteral.js.map +1 -0
- package/dist/slang-nodes/VersionOperator.js +15 -0
- package/dist/slang-nodes/VersionOperator.js.map +1 -0
- package/dist/slang-nodes/VersionRange.js +5 -10
- package/dist/slang-nodes/VersionRange.js.map +1 -1
- package/dist/slang-nodes/VersionTerm.js +24 -0
- package/dist/slang-nodes/VersionTerm.js.map +1 -0
- package/dist/slang-utils/infer-language.js +62 -0
- package/dist/slang-utils/infer-language.js.map +1 -0
- package/dist/slang-utils/is-comment.js.map +1 -1
- package/dist/slang-utils/print-warning.js +12 -0
- package/dist/slang-utils/print-warning.js.map +1 -0
- package/dist/slangSolidityParser.js +13 -6
- package/dist/slangSolidityParser.js.map +1 -1
- package/package.json +8 -9
- package/src/comments/handlers/index.js +0 -1
- package/src/common/print-warning.js +12 -0
- package/src/index.ts +18 -16
- package/src/parser.js +4 -3
- package/src/printer.js +2 -1
- package/src/slang-comments/handlers/handle-contract-definition-comments.ts +0 -8
- package/src/slang-comments/handlers/handle-modifier-invocation-comments.ts +40 -0
- package/src/slang-comments/handlers/index.ts +2 -0
- package/src/slang-nodes/ContractDefinition.ts +1 -2
- package/src/slang-nodes/FunctionDefinition.ts +1 -2
- package/src/slang-nodes/ImportDeconstructionSymbols.ts +1 -2
- package/src/slang-nodes/ModifierInvocation.ts +4 -2
- package/src/slang-nodes/RevertStatement.ts +2 -2
- package/src/slang-nodes/{VersionSpecifiers.ts → SimpleVersionLiteral.ts} +3 -3
- package/src/slang-nodes/SourceUnit.ts +11 -2
- package/src/slang-nodes/VersionExpression.ts +19 -37
- package/src/slang-nodes/VersionLiteral.ts +42 -0
- package/src/slang-nodes/VersionOperator.ts +29 -0
- package/src/slang-nodes/VersionRange.ts +7 -14
- package/src/slang-nodes/VersionTerm.ts +41 -0
- package/src/slang-nodes/index.d.ts +8 -4
- package/src/slang-utils/infer-language.ts +94 -0
- package/src/slang-utils/is-comment.ts +1 -1
- package/src/slang-utils/print-warning.ts +12 -0
- package/src/slangSolidityParser.ts +17 -8
- package/src/types.d.ts +4 -2
- package/dist/slang-nodes/VersionComparator.js +0 -19
- package/dist/slang-nodes/VersionComparator.js.map +0 -1
- package/dist/slang-nodes/VersionSpecifiers.js.map +0 -1
- package/src/slang-nodes/VersionComparator.ts +0 -36
|
@@ -1,24 +1,19 @@
|
|
|
1
1
|
import { NonterminalKind } from '@nomicfoundation/slang/kinds/index.js';
|
|
2
2
|
import { getNodeMetadata, updateMetadata } from '../slang-utils/metadata.js';
|
|
3
|
-
import {
|
|
3
|
+
import { VersionLiteral } from './VersionLiteral.js';
|
|
4
4
|
export class VersionRange {
|
|
5
5
|
constructor(ast, offset) {
|
|
6
6
|
this.kind = NonterminalKind.VersionRange;
|
|
7
7
|
let metadata = getNodeMetadata(ast, offset);
|
|
8
8
|
const { offsets } = metadata;
|
|
9
|
-
this.
|
|
10
|
-
this.
|
|
11
|
-
|
|
12
|
-
metadata = updateMetadata(metadata, [this.leftOperand, this.rightOperand]);
|
|
9
|
+
this.start = new VersionLiteral(ast.start, offsets[0]);
|
|
10
|
+
this.end = new VersionLiteral(ast.end, offsets[1]);
|
|
11
|
+
metadata = updateMetadata(metadata, [this.start, this.end]);
|
|
13
12
|
this.comments = metadata.comments;
|
|
14
13
|
this.loc = metadata.loc;
|
|
15
14
|
}
|
|
16
15
|
print(path, print) {
|
|
17
|
-
return [
|
|
18
|
-
path.call(print, 'leftOperand'),
|
|
19
|
-
` ${this.operator} `,
|
|
20
|
-
path.call(print, 'rightOperand')
|
|
21
|
-
];
|
|
16
|
+
return [path.call(print, 'start'), ' - ', path.call(print, 'end')];
|
|
22
17
|
}
|
|
23
18
|
}
|
|
24
19
|
//# sourceMappingURL=VersionRange.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"VersionRange.js","sourceRoot":"","sources":["../../src/slang-nodes/VersionRange.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,uCAAuC,CAAC;AACxE,OAAO,EAAE,eAAe,EAAE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Kind } from '@nomicfoundation/slang/kinds/index.js';
|
|
2
|
+
import { getNodeMetadata, updateMetadata } from '../slang-utils/metadata.js';
|
|
3
|
+
import { VersionOperator } from './VersionOperator.js';
|
|
4
|
+
import { VersionLiteral } from './VersionLiteral.js';
|
|
5
|
+
export class VersionTerm {
|
|
6
|
+
constructor(ast, offset) {
|
|
7
|
+
this.kind = NonterminalKind.VersionTerm;
|
|
8
|
+
let metadata = getNodeMetadata(ast, offset);
|
|
9
|
+
const { offsets } = metadata;
|
|
10
|
+
let i = 0;
|
|
11
|
+
if (ast.operator) {
|
|
12
|
+
this.operator = new VersionOperator(ast.operator, offsets[i]);
|
|
13
|
+
i += 1;
|
|
14
|
+
}
|
|
15
|
+
this.literal = new VersionLiteral(ast.literal, offsets[i]);
|
|
16
|
+
metadata = updateMetadata(metadata, [this.operator, this.literal]);
|
|
17
|
+
this.comments = metadata.comments;
|
|
18
|
+
this.loc = metadata.loc;
|
|
19
|
+
}
|
|
20
|
+
print(path, print) {
|
|
21
|
+
return [path.call(print, 'operator'), path.call(print, 'literal')];
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=VersionTerm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"VersionTerm.js","sourceRoot":"","sources":["../../src/slang-nodes/VersionTerm.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,uCAAuC,CAAC;AACxE,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAC;AAC7E,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AACvD,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAMrD,MAAM,OAAO,WAAW;IAWtB,YAAY,GAAoB,EAAE,MAAc;QAVvC,SAAI,GAAG,eAAe,CAAC,WAAW,CAAC;QAW1C,IAAI,QAAQ,GAAG,eAAe,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;QAC5C,MAAM,EAAE,OAAO,EAAE,GAAG,QAAQ,CAAC;QAE7B,IAAI,CAAC,GAAG,CAAC,CAAC;QACV,IAAI,GAAG,CAAC,QAAQ,EAAE,CAAC;YACjB,IAAI,CAAC,QAAQ,GAAG,IAAI,eAAe,CAAC,GAAG,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC;YAC9D,CAAC,IAAI,CAAC,CAAC;QACT,CAAC;QACD,IAAI,CAAC,OAAO,GAAG,IAAI,cAAc,CAAC,GAAG,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC;QAE3D,QAAQ,GAAG,cAAc,CAAC,QAAQ,EAAE,CAAC,IAAI,CAAC,QAAQ,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC;QAEnE,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC,QAAQ,CAAC;QAClC,IAAI,CAAC,GAAG,GAAG,QAAQ,CAAC,GAAG,CAAC;IAC1B,CAAC;IAED,KAAK,CAAC,IAA0B,EAAE,KAAoB;QACpD,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,EAAE,UAAU,CAAC,EAAE,IAAI,CAAC,IAAI,CAAC,KAAK,EAAE,SAAS,CAAC,CAAC,CAAC;IACrE,CAAC;CACF"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { VersionExpressionSets as SlangVersionExpressionSets } from '@nomicfoundation/slang/ast/index.js';
|
|
2
|
+
import { NonterminalKind } from '@nomicfoundation/slang/kinds/index.js';
|
|
3
|
+
import { Language } from '@nomicfoundation/slang/language/index.js';
|
|
4
|
+
import { Query } from '@nomicfoundation/slang/query/index.js';
|
|
5
|
+
import { maxSatisfying, minSatisfying, minor, major, minVersion, validRange } from 'semver';
|
|
6
|
+
import { VersionExpressionSets } from '../slang-nodes/VersionExpressionSets.js';
|
|
7
|
+
const supportedVersions = Language.supportedVersions();
|
|
8
|
+
const milestoneVersions = Array.from(supportedVersions.reduce((minorRanges, version) => {
|
|
9
|
+
minorRanges.add(`^${major(version)}.${minor(version)}.0`);
|
|
10
|
+
return minorRanges;
|
|
11
|
+
}, new Set()))
|
|
12
|
+
.reverse()
|
|
13
|
+
.reduce((versions, range) => {
|
|
14
|
+
versions.push(maxSatisfying(supportedVersions, range));
|
|
15
|
+
versions.push(minSatisfying(supportedVersions, range));
|
|
16
|
+
return versions;
|
|
17
|
+
}, []);
|
|
18
|
+
export function inferLanguage(text) {
|
|
19
|
+
let inferredRange = '';
|
|
20
|
+
for (const version of milestoneVersions) {
|
|
21
|
+
try {
|
|
22
|
+
inferredRange = tryToCollectPragmas(text, version);
|
|
23
|
+
break;
|
|
24
|
+
}
|
|
25
|
+
catch (_a) { }
|
|
26
|
+
}
|
|
27
|
+
if (!minVersion(inferredRange)) {
|
|
28
|
+
throw new Error("Couldn't infer any version from the ranges in the pragmas.");
|
|
29
|
+
}
|
|
30
|
+
const maxSatisfyingVersion = maxSatisfying(supportedVersions, inferredRange);
|
|
31
|
+
return maxSatisfyingVersion
|
|
32
|
+
? new Language(maxSatisfyingVersion)
|
|
33
|
+
: new Language(supportedVersions[supportedVersions.length - 1]);
|
|
34
|
+
}
|
|
35
|
+
function tryToCollectPragmas(text, version) {
|
|
36
|
+
const language = new Language(version);
|
|
37
|
+
const parseOutput = language.parse(NonterminalKind.SourceUnit, text);
|
|
38
|
+
const query = Query.parse('[VersionPragma @versionRanges [VersionExpressionSets]]');
|
|
39
|
+
const matches = parseOutput.createTreeCursor().query([query]);
|
|
40
|
+
const ranges = [];
|
|
41
|
+
let match;
|
|
42
|
+
while ((match = matches.next())) {
|
|
43
|
+
const versionRange = new SlangVersionExpressionSets(match.captures.versionRanges[0].node());
|
|
44
|
+
ranges.push(
|
|
45
|
+
// Replace all comments that could be in the expression with whitespace
|
|
46
|
+
new VersionExpressionSets(versionRange, 0).comments.reduce((range, comment) => range.replace(comment.value, ' '), versionRange.cst.unparse()));
|
|
47
|
+
}
|
|
48
|
+
// Check if we found pragmas.
|
|
49
|
+
if (ranges.length === 0) {
|
|
50
|
+
// If we didn't find pragmas but succeeded parsing the source we keep it.
|
|
51
|
+
if (parseOutput.isValid) {
|
|
52
|
+
return version;
|
|
53
|
+
}
|
|
54
|
+
// Otherwise we throw.
|
|
55
|
+
throw new Error(`Using version ${version} did not find any pragma statement and does not parse without errors.`);
|
|
56
|
+
}
|
|
57
|
+
// validRange rewrites `0.5.0 - 0.6.0` as `>=0.5.0 <=0.6.0` but it returns
|
|
58
|
+
// null if the range is not valid. We have to coerce null to 'null' so it
|
|
59
|
+
// fails the `minVersion(inferredRange)` call.
|
|
60
|
+
return ranges.map((range) => `${validRange(range)}`).join(' ');
|
|
61
|
+
}
|
|
62
|
+
//# sourceMappingURL=infer-language.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"infer-language.js","sourceRoot":"","sources":["../../src/slang-utils/infer-language.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,IAAI,0BAA0B,EAAE,MAAM,qCAAqC,CAAC;AAC1G,OAAO,EAAE,eAAe,EAAE,MAAM,uCAAuC,CAAC;AACxE,OAAO,EAAE,QAAQ,EAAE,MAAM,0CAA0C,CAAC;AACpE,OAAO,EAAE,KAAK,EAAE,MAAM,uCAAuC,CAAC;AAC9D,OAAO,EACL,aAAa,EACb,aAAa,EACb,KAAK,EACL,KAAK,EACL,UAAU,EACV,UAAU,EACX,MAAM,QAAQ,CAAC;AAChB,OAAO,EAAE,qBAAqB,EAAE,MAAM,yCAAyC,CAAC;AAIhF,MAAM,iBAAiB,GAAG,QAAQ,CAAC,iBAAiB,EAAE,CAAC;AAEvD,MAAM,iBAAiB,GAAG,KAAK,CAAC,IAAI,CAClC,iBAAiB,CAAC,MAAM,CAAC,CAAC,WAAW,EAAE,OAAO,EAAE,EAAE;IAChD,WAAW,CAAC,GAAG,CAAC,IAAI,KAAK,CAAC,OAAO,CAAC,IAAI,KAAK,CAAC,OAAO,CAAC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sources":["../../src/slang-utils/is-comment.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,uCAAuC,CAAC;AACrE,OAAO,EAAE,uBAAuB,EAAE,MAAM,iCAAiC,CAAC;AAK1E,MAAM,CAAC,MAAM,cAAc,GAAG,uBAAuB,CAAC;IACpD,YAAY,CAAC,gBAAgB;IAC7B,YAAY,CAAC,uBAAuB;CACrC,
|
|
1
|
+
{"version":3,"file":"is-comment.js","sourceRoot":"","sources":["../../src/slang-utils/is-comment.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,uCAAuC,CAAC;AACrE,OAAO,EAAE,uBAAuB,EAAE,MAAM,iCAAiC,CAAC;AAK1E,MAAM,CAAC,MAAM,cAAc,GAAG,uBAAuB,CAAC;IACpD,YAAY,CAAC,gBAAgB;IAC7B,YAAY,CAAC,uBAAuB;CACrC,CAAmD,CAAC;AAErD,MAAM,CAAC,MAAM,SAAS,GAAG,uBAAuB,CAAC;IAC/C,YAAY,CAAC,gBAAgB;IAC7B,YAAY,CAAC,uBAAuB;IACpC,YAAY,CAAC,iBAAiB;IAC9B,YAAY,CAAC,wBAAwB;CACtC,CAA8C,CAAC"}
|
|
@@ -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
|
+
console.warn(`${clearLine}[prettier-solidity] ${message}`);
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=print-warning.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"print-warning.js","sourceRoot":"","sources":["../../src/slang-utils/print-warning.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,YAAY,CAAC,OAAe;IAC1C,6EAA6E;IAC7E,0EAA0E;IAC1E,uCAAuC;IACvC,EAAE;IACF,yBAAyB;IACzB,6CAA6C;IAC7C,wBAAwB;IACxB,MAAM,SAAS,GAAG,WAAW,CAAC;IAE9B,OAAO,CAAC,IAAI,CAAC,GAAG,SAAS,uBAAuB,OAAO,EAAE,CAAC,CAAC;AAC7D,CAAC"}
|
|
@@ -3,16 +3,23 @@
|
|
|
3
3
|
import { Language } from '@nomicfoundation/slang/language/index.js';
|
|
4
4
|
import { NonterminalKind } from '@nomicfoundation/slang/kinds/index.js';
|
|
5
5
|
import { SourceUnit as SlangSourceUnit } from '@nomicfoundation/slang/ast/index.js';
|
|
6
|
-
import
|
|
6
|
+
import { maxSatisfying } from 'semver';
|
|
7
|
+
import { inferLanguage } from './slang-utils/infer-language.js';
|
|
8
|
+
import { printWarning } from './slang-utils/print-warning.js';
|
|
7
9
|
import { SourceUnit } from './slang-nodes/SourceUnit.js';
|
|
10
|
+
const supportedVersions = Language.supportedVersions();
|
|
8
11
|
export default function parse(text, _parsers, options = _parsers) {
|
|
9
|
-
const compiler =
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
: supportedVersions[supportedVersions.length - 1]);
|
|
12
|
+
const compiler = maxSatisfying(supportedVersions, options.compiler);
|
|
13
|
+
const language = compiler && supportedVersions.includes(compiler)
|
|
14
|
+
? new Language(compiler)
|
|
15
|
+
: inferLanguage(text);
|
|
14
16
|
const parseOutput = language.parse(NonterminalKind.SourceUnit, text);
|
|
17
|
+
printWarning(compiler
|
|
18
|
+
? `Using version ${language.version} based on the compiler option provided.`
|
|
19
|
+
: `Inferred version ${language.version} based on the pragma statements in the code.`);
|
|
15
20
|
if (parseOutput.isValid) {
|
|
21
|
+
// We update the compiler version by the inferred one.
|
|
22
|
+
options.compiler = language.version;
|
|
16
23
|
return new SourceUnit(new SlangSourceUnit(parseOutput.tree()), 0, options);
|
|
17
24
|
}
|
|
18
25
|
throw new Error(parseOutput.errors()[0].message());
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"slangSolidityParser.js","sourceRoot":"","sources":["../src/slangSolidityParser.ts"],"names":[],"mappings":"AAAA,mDAAmD;AACnD,gDAAgD;AAChD,OAAO,EAAE,QAAQ,EAAE,MAAM,0CAA0C,CAAC;AACpE,OAAO,EAAE,eAAe,EAAE,MAAM,uCAAuC,CAAC;AACxE,OAAO,EAAE,UAAU,IAAI,eAAe,EAAE,MAAM,qCAAqC,CAAC;AACpF,OAAO,MAAM,MAAM,
|
|
1
|
+
{"version":3,"file":"slangSolidityParser.js","sourceRoot":"","sources":["../src/slangSolidityParser.ts"],"names":[],"mappings":"AAAA,mDAAmD;AACnD,gDAAgD;AAChD,OAAO,EAAE,QAAQ,EAAE,MAAM,0CAA0C,CAAC;AACpE,OAAO,EAAE,eAAe,EAAE,MAAM,uCAAuC,CAAC;AACxE,OAAO,EAAE,UAAU,IAAI,eAAe,EAAE,MAAM,qCAAqC,CAAC;AACpF,OAAO,EAAE,aAAa,EAAE,MAAM,QAAQ,CAAC;AACvC,OAAO,EAAE,aAAa,EAAE,MAAM,iCAAiC,CAAC;AAChE,OAAO,EAAE,YAAY,EAAE,MAAM,gCAAgC,CAAC;AAC9D,OAAO,EAAE,UAAU,EAAE,MAAM,6BAA6B,CAAC;AAMzD,MAAM,iBAAiB,GAAG,QAAQ,CAAC,iBAAiB,EAAE,CAAC;AAEvD,MAAM,CAAC,OAAO,UAAU,KAAK,CAC3B,IAAY,EACZ,QAA2C,EAC3C,UAAU,QAAkC;IAE5C,MAAM,QAAQ,GAAG,aAAa,CAAC,iBAAiB,EAAE,OAAO,CAAC,QAAQ,CAAC,CAAC;IAEpE,MAAM,QAAQ,GACZ,QAAQ,IAAI,iBAAiB,CAAC,QAAQ,CAAC,QAAQ,CAAC;QAC9C,CAAC,CAAC,IAAI,QAAQ,CAAC,QAAQ,CAAC;QACxB,CAAC,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC;IAE1B,MAAM,WAAW,GAAG,QAAQ,CAAC,KAAK,CAAC,eAAe,CAAC,UAAU,EAAE,IAAI,CAAC,CAAC;IACrE,YAAY,CACV,QAAQ;QACN,CAAC,CAAC,iBAAiB,QAAQ,CAAC,OAAO,yCAAyC;QAC5E,CAAC,CAAC,oBAAoB,QAAQ,CAAC,OAAO,8CAA8C,CACvF,CAAC;IAEF,IAAI,WAAW,CAAC,OAAO,EAAE,CAAC;QACxB,sDAAsD;QACtD,OAAO,CAAC,QAAQ,GAAG,QAAQ,CAAC,OAAO,CAAC;QACpC,OAAO,IAAI,UAAU,CACnB,IAAI,eAAe,CAAC,WAAW,CAAC,IAAI,EAAqB,CAAC,EAC1D,CAAC,EACD,OAAO,CACR,CAAC;IACJ,CAAC;IACD,MAAM,IAAI,KAAK,CAAC,WAAW,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,CAAC,OAAO,EAAE,CAAC,CAAC;AACrD,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "prettier-plugin-solidity",
|
|
3
|
-
"version": "2.0.0-beta",
|
|
3
|
+
"version": "2.0.0-beta.2",
|
|
4
4
|
"description": "A Prettier Plugin for automatically formatting your Solidity code.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -75,15 +75,14 @@
|
|
|
75
75
|
},
|
|
76
76
|
"devDependencies": {
|
|
77
77
|
"@babel/code-frame": "^7.24.7",
|
|
78
|
+
"@types/jest": "^29.5.13",
|
|
78
79
|
"@types/semver": "^7.5.8",
|
|
79
|
-
"@typescript-eslint/eslint-plugin": "^8.
|
|
80
|
-
"@typescript-eslint/parser": "^8.
|
|
80
|
+
"@typescript-eslint/eslint-plugin": "^8.8.0",
|
|
81
|
+
"@typescript-eslint/parser": "^8.8.0",
|
|
81
82
|
"c8": "^9.1.0",
|
|
82
83
|
"cross-env": "^7.0.3",
|
|
83
|
-
"eslint": "^
|
|
84
|
-
"eslint-config-airbnb-base": "^15.0.0",
|
|
84
|
+
"eslint": "^9.11.1",
|
|
85
85
|
"eslint-config-prettier": "^9.1.0",
|
|
86
|
-
"eslint-plugin-import": "^2.29.1",
|
|
87
86
|
"esm-utils": "^4.3.0",
|
|
88
87
|
"esmock": "^2.6.7",
|
|
89
88
|
"jest": "^29.7.0",
|
|
@@ -94,15 +93,15 @@
|
|
|
94
93
|
"lines-and-columns": "^2.0.4",
|
|
95
94
|
"prettier": "^3.3.3",
|
|
96
95
|
"proxyquire": "^2.1.3",
|
|
97
|
-
"solc": "^0.8.
|
|
96
|
+
"solc": "^0.8.27",
|
|
98
97
|
"ts-loader": "^9.5.1",
|
|
99
98
|
"ts-node": "^10.9.2",
|
|
100
99
|
"typescript": "^5.5.4",
|
|
101
|
-
"webpack": "^5.
|
|
100
|
+
"webpack": "^5.95.0",
|
|
102
101
|
"webpack-cli": "^5.1.4"
|
|
103
102
|
},
|
|
104
103
|
"dependencies": {
|
|
105
|
-
"@nomicfoundation/slang": "
|
|
104
|
+
"@nomicfoundation/slang": "0.17.0",
|
|
106
105
|
"@solidity-parser/parser": "^0.18.0",
|
|
107
106
|
"semver": "^7.6.3"
|
|
108
107
|
},
|
|
@@ -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
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -3,9 +3,9 @@ import { handleComments, printComment } from './slang-comments/index.js';
|
|
|
3
3
|
import massageAstNode from './clean.js';
|
|
4
4
|
import loc from './loc.js';
|
|
5
5
|
import options from './options.js';
|
|
6
|
-
import
|
|
7
|
-
import
|
|
8
|
-
import
|
|
6
|
+
import antlrParse from './parser.js';
|
|
7
|
+
import antlrPrint from './printer.js';
|
|
8
|
+
import slangParse from './slangSolidityParser.js';
|
|
9
9
|
import slangPrint from './slangPrinter.js';
|
|
10
10
|
import { isComment, isBlockComment } from './slang-utils/is-comment.js';
|
|
11
11
|
import { locEnd, locStart } from './slang-utils/loc.js';
|
|
@@ -18,8 +18,10 @@ import type {
|
|
|
18
18
|
} from 'prettier';
|
|
19
19
|
import type { AstNode } from './slang-nodes';
|
|
20
20
|
|
|
21
|
-
const
|
|
22
|
-
const
|
|
21
|
+
const slangParserId = 'slang-solidity';
|
|
22
|
+
const antlrParserId = 'solidity-parse';
|
|
23
|
+
const slangAstId = 'slang-ast';
|
|
24
|
+
const antlrAstId = 'solidity-ast';
|
|
23
25
|
|
|
24
26
|
// https://prettier.io/docs/en/plugins.html#languages
|
|
25
27
|
// https://github.com/github-linguist/linguist/blob/master/lib/linguist/languages.yml
|
|
@@ -30,23 +32,23 @@ const languages: SupportLanguage[] = [
|
|
|
30
32
|
aceMode: 'text',
|
|
31
33
|
tmScope: 'source.solidity',
|
|
32
34
|
extensions: ['.sol'],
|
|
33
|
-
parsers: [
|
|
35
|
+
parsers: [slangParserId, antlrParserId],
|
|
34
36
|
vscodeLanguageIds: ['solidity']
|
|
35
37
|
}
|
|
36
38
|
];
|
|
37
39
|
|
|
38
40
|
// https://prettier.io/docs/en/plugins.html#parsers
|
|
39
|
-
const
|
|
40
|
-
const
|
|
41
|
-
astFormat,
|
|
42
|
-
parse:
|
|
41
|
+
const antlrParser = { astFormat: antlrAstId, parse: antlrParse, ...loc };
|
|
42
|
+
const slangParser: Parser<AstNode> = {
|
|
43
|
+
astFormat: slangAstId,
|
|
44
|
+
parse: slangParse,
|
|
43
45
|
locStart,
|
|
44
46
|
locEnd
|
|
45
47
|
};
|
|
46
48
|
|
|
47
49
|
const parsers = {
|
|
48
|
-
[
|
|
49
|
-
|
|
50
|
+
[slangParserId]: slangParser,
|
|
51
|
+
[antlrParserId]: antlrParser
|
|
50
52
|
};
|
|
51
53
|
|
|
52
54
|
const solidityCanAttachComment = (node: { type: string }): boolean =>
|
|
@@ -60,7 +62,7 @@ const canAttachComment = (node: AstNode): boolean =>
|
|
|
60
62
|
!isComment(node);
|
|
61
63
|
|
|
62
64
|
// https://prettier.io/docs/en/plugins.html#printers
|
|
63
|
-
const
|
|
65
|
+
const antlrPrinter = {
|
|
64
66
|
canAttachComment: solidityCanAttachComment,
|
|
65
67
|
handleComments: {
|
|
66
68
|
ownLine: comments.solidityHandleOwnLineComment,
|
|
@@ -69,7 +71,7 @@ const printer = {
|
|
|
69
71
|
},
|
|
70
72
|
isBlockComment: comments.isBlockComment,
|
|
71
73
|
massageAstNode,
|
|
72
|
-
print,
|
|
74
|
+
print: antlrPrint,
|
|
73
75
|
printComment: comments.printComment
|
|
74
76
|
};
|
|
75
77
|
const slangPrinter: Printer<AstNode> = {
|
|
@@ -82,8 +84,8 @@ const slangPrinter: Printer<AstNode> = {
|
|
|
82
84
|
};
|
|
83
85
|
|
|
84
86
|
const printers = {
|
|
85
|
-
[
|
|
86
|
-
|
|
87
|
+
[slangAstId]: slangPrinter,
|
|
88
|
+
[antlrAstId]: antlrPrinter
|
|
87
89
|
};
|
|
88
90
|
|
|
89
91
|
// https://prettier.io/docs/en/plugins.html#defaultoptions
|
package/src/parser.js
CHANGED
|
@@ -3,6 +3,8 @@ import parser from '@solidity-parser/parser';
|
|
|
3
3
|
import coerce from 'semver/functions/coerce.js';
|
|
4
4
|
import satisfies from 'semver/functions/satisfies.js';
|
|
5
5
|
|
|
6
|
+
import { printWarning } from './common/print-warning.js';
|
|
7
|
+
|
|
6
8
|
const tryHug = (node, operators) => {
|
|
7
9
|
if (node.type === 'BinaryOperation' && operators.includes(node.operator))
|
|
8
10
|
return {
|
|
@@ -27,8 +29,8 @@ function parse(text, _parsers, options = _parsers) {
|
|
|
27
29
|
if (!satisfies(compiler, ctx.value)) {
|
|
28
30
|
// @TODO: investigate the best way to warn that would apply to
|
|
29
31
|
// different editors.
|
|
30
|
-
|
|
31
|
-
`
|
|
32
|
+
printWarning(
|
|
33
|
+
`The compiler option is set to '${options.compiler}', which does not satisfy 'pragma solidity ${ctx.value}'.`
|
|
32
34
|
);
|
|
33
35
|
}
|
|
34
36
|
},
|
|
@@ -41,7 +43,6 @@ function parse(text, _parsers, options = _parsers) {
|
|
|
41
43
|
if (!ctx.isConstructor) {
|
|
42
44
|
ctx.modifiers.forEach((modifier) => {
|
|
43
45
|
if (modifier.arguments?.length === 0) {
|
|
44
|
-
// eslint-disable-next-line no-param-reassign
|
|
45
46
|
modifier.arguments = null;
|
|
46
47
|
}
|
|
47
48
|
});
|
package/src/printer.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import * as nodes from './nodes/index.js';
|
|
2
|
+
import { printWarning } from './common/print-warning.js';
|
|
2
3
|
import {
|
|
3
4
|
hasNodeIgnoreComment,
|
|
4
5
|
prettierVersionSatisfies
|
|
@@ -16,7 +17,7 @@ function once(factory) {
|
|
|
16
17
|
}
|
|
17
18
|
|
|
18
19
|
const warnDeprecation = once(() => {
|
|
19
|
-
|
|
20
|
+
printWarning(
|
|
20
21
|
`'solidity-parse' has been deprecated, please use 'slang-solidity'.`
|
|
21
22
|
);
|
|
22
23
|
return true;
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { NonterminalKind } from '@nomicfoundation/slang/kinds/index.js';
|
|
2
2
|
import { util } from 'prettier';
|
|
3
3
|
import { getNextNonSpaceNonCommentCharacter } from '../../slang-utils/backward-compatibility.js';
|
|
4
|
-
import addCollectionNodeFirstComment from './add-collection-node-first-comment.js';
|
|
5
4
|
import addCollectionNodeLastComment from './add-collection-node-last-comment.js';
|
|
6
5
|
|
|
7
6
|
import type { HandlerParams } from './types';
|
|
@@ -52,13 +51,6 @@ export default function handleContractDefinitionComments({
|
|
|
52
51
|
);
|
|
53
52
|
return true;
|
|
54
53
|
}
|
|
55
|
-
|
|
56
|
-
// If there's no InheritanceSpecifier, the comment before the body is
|
|
57
|
-
// assumed to be intended at the beginning of the body.
|
|
58
|
-
if (followingNode?.kind === NonterminalKind.ContractMembers) {
|
|
59
|
-
addCollectionNodeFirstComment(followingNode, comment);
|
|
60
|
-
return true;
|
|
61
|
-
}
|
|
62
54
|
}
|
|
63
55
|
|
|
64
56
|
return false;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { NonterminalKind } from '@nomicfoundation/slang/kinds/index.js';
|
|
2
|
+
import { util } from 'prettier';
|
|
3
|
+
import { getNextNonSpaceNonCommentCharacter } from '../../slang-utils/backward-compatibility.js';
|
|
4
|
+
import addCollectionNodeFirstComment from './add-collection-node-first-comment.js';
|
|
5
|
+
|
|
6
|
+
import type { HandlerParams } from './types';
|
|
7
|
+
|
|
8
|
+
const { addTrailingComment } = util;
|
|
9
|
+
|
|
10
|
+
export default function handleModifierInvocationComments({
|
|
11
|
+
text,
|
|
12
|
+
precedingNode,
|
|
13
|
+
enclosingNode,
|
|
14
|
+
followingNode,
|
|
15
|
+
comment
|
|
16
|
+
}: HandlerParams): boolean {
|
|
17
|
+
if (enclosingNode?.kind !== NonterminalKind.ModifierInvocation) {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const nextCharacter = getNextNonSpaceNonCommentCharacter(text, comment);
|
|
22
|
+
|
|
23
|
+
// The last comments before the body.
|
|
24
|
+
if (
|
|
25
|
+
precedingNode?.kind === NonterminalKind.IdentifierPath &&
|
|
26
|
+
nextCharacter === '(' &&
|
|
27
|
+
followingNode?.kind === NonterminalKind.ArgumentsDeclaration &&
|
|
28
|
+
followingNode.variant.kind ===
|
|
29
|
+
NonterminalKind.PositionalArgumentsDeclaration
|
|
30
|
+
) {
|
|
31
|
+
if (followingNode.variant.arguments.items.length === 0) {
|
|
32
|
+
addTrailingComment(enclosingNode, comment);
|
|
33
|
+
} else {
|
|
34
|
+
addCollectionNodeFirstComment(followingNode.variant.arguments, comment);
|
|
35
|
+
}
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
@@ -4,6 +4,7 @@ import handleElseBranchComments from './handle-else-branch-comments.js';
|
|
|
4
4
|
import handleIfStatementComments from './handle-if-statement-comments.js';
|
|
5
5
|
import handleInterfaceDefinitionComments from './handle-interface-definition-comments.js';
|
|
6
6
|
import handleLibraryDefinitionComments from './handle-library-definition-comments.js';
|
|
7
|
+
import handleModifierInvocationComments from './handle-modifier-invocation-comments.js';
|
|
7
8
|
import handleParametersDeclarationComments from './handle-parameters-declaration-comments.js';
|
|
8
9
|
import handlePositionalArgumentsDeclarationComments from './handle-positional-arguments-declaration-comments.js';
|
|
9
10
|
import handleWhileStatementComments from './handle-while-statement-comments.js';
|
|
@@ -16,6 +17,7 @@ export default [
|
|
|
16
17
|
handleIfStatementComments,
|
|
17
18
|
handleInterfaceDefinitionComments,
|
|
18
19
|
handleLibraryDefinitionComments,
|
|
20
|
+
handleModifierInvocationComments,
|
|
19
21
|
handleParametersDeclarationComments,
|
|
20
22
|
handlePositionalArgumentsDeclarationComments,
|
|
21
23
|
handleWhileStatementComments,
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { doc } from 'prettier';
|
|
2
|
-
import coerce from 'semver
|
|
3
|
-
import satisfies from 'semver/functions/satisfies.js';
|
|
2
|
+
import { coerce, satisfies } from 'semver';
|
|
4
3
|
import { NonterminalKind } from '@nomicfoundation/slang/kinds/index.js';
|
|
5
4
|
import { getNodeMetadata, updateMetadata } from '../slang-utils/metadata.js';
|
|
6
5
|
import { Identifier } from './Identifier.js';
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import coerce from 'semver
|
|
2
|
-
import satisfies from 'semver/functions/satisfies.js';
|
|
1
|
+
import { coerce, satisfies } from 'semver';
|
|
3
2
|
import { NonterminalKind } from '@nomicfoundation/slang/kinds/index.js';
|
|
4
3
|
import { printFunction } from '../slang-printers/print-function.js';
|
|
5
4
|
import { getNodeMetadata, updateMetadata } from '../slang-utils/metadata.js';
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { doc } from 'prettier';
|
|
2
|
-
import coerce from 'semver
|
|
3
|
-
import satisfies from 'semver/functions/satisfies.js';
|
|
2
|
+
import { coerce, satisfies } from 'semver';
|
|
4
3
|
import { NonterminalKind } from '@nomicfoundation/slang/kinds/index.js';
|
|
5
4
|
import { printSeparatedList } from '../slang-printers/print-separated-list.js';
|
|
6
5
|
import { getNodeMetadata, updateMetadata } from '../slang-utils/metadata.js';
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { NonterminalKind } from '@nomicfoundation/slang/kinds/index.js';
|
|
2
|
-
import {
|
|
2
|
+
import { isBlockComment } from '../slang-utils/is-comment.js';
|
|
3
3
|
import { getNodeMetadata, updateMetadata } from '../slang-utils/metadata.js';
|
|
4
4
|
import { IdentifierPath } from './IdentifierPath.js';
|
|
5
5
|
import { ArgumentsDeclaration } from './ArgumentsDeclaration.js';
|
|
@@ -50,7 +50,9 @@ export class ModifierInvocation implements SlangNode {
|
|
|
50
50
|
this.arguments.variant.kind ===
|
|
51
51
|
NonterminalKind.PositionalArgumentsDeclaration &&
|
|
52
52
|
this.arguments.variant.arguments.items.length === 0 && // no arguments
|
|
53
|
-
!ast
|
|
53
|
+
!ast
|
|
54
|
+
.arguments!.variant.cst.children()
|
|
55
|
+
.some((child) => isBlockComment(child)) // no comments, at this point we need to check the CST
|
|
54
56
|
) {
|
|
55
57
|
delete this.arguments;
|
|
56
58
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { NonterminalKind } from '@nomicfoundation/slang/kinds/index.js';
|
|
2
2
|
import { getNodeMetadata, updateMetadata } from '../slang-utils/metadata.js';
|
|
3
|
+
import { joinExisting } from '../slang-utils/join-existing.js';
|
|
3
4
|
import { IdentifierPath } from './IdentifierPath.js';
|
|
4
5
|
import { ArgumentsDeclaration } from './ArgumentsDeclaration.js';
|
|
5
6
|
|
|
@@ -46,8 +47,7 @@ export class RevertStatement implements SlangNode {
|
|
|
46
47
|
|
|
47
48
|
print(path: AstPath<RevertStatement>, print: PrintFunction): Doc {
|
|
48
49
|
return [
|
|
49
|
-
'revert ',
|
|
50
|
-
path.call(print, 'error'),
|
|
50
|
+
joinExisting(' ', ['revert', path.call(print, 'error')]),
|
|
51
51
|
path.call(print, 'arguments'),
|
|
52
52
|
';'
|
|
53
53
|
];
|
|
@@ -5,8 +5,8 @@ import type * as ast from '@nomicfoundation/slang/ast';
|
|
|
5
5
|
import type { Doc } from 'prettier';
|
|
6
6
|
import type { SlangNode } from '../types';
|
|
7
7
|
|
|
8
|
-
export class
|
|
9
|
-
readonly kind = NonterminalKind.
|
|
8
|
+
export class SimpleVersionLiteral implements SlangNode {
|
|
9
|
+
readonly kind = NonterminalKind.SimpleVersionLiteral;
|
|
10
10
|
|
|
11
11
|
comments;
|
|
12
12
|
|
|
@@ -16,7 +16,7 @@ export class VersionSpecifiers implements SlangNode {
|
|
|
16
16
|
|
|
17
17
|
separators: string[];
|
|
18
18
|
|
|
19
|
-
constructor(ast: ast.
|
|
19
|
+
constructor(ast: ast.SimpleVersionLiteral, offset: number) {
|
|
20
20
|
const metadata = getNodeMetadata(ast, offset, true);
|
|
21
21
|
|
|
22
22
|
this.items = ast.items.map((item) => item.text);
|
|
@@ -31,7 +31,9 @@ export class SourceUnit implements SlangNode {
|
|
|
31
31
|
|
|
32
32
|
metadata = updateMetadata(metadata, [this.members]);
|
|
33
33
|
|
|
34
|
-
|
|
34
|
+
// Because of comments being extracted like a russian doll, the order needs
|
|
35
|
+
// to be fixed at the end.
|
|
36
|
+
this.comments = metadata.comments.sort((a, b) => a.loc.start - b.loc.start);
|
|
35
37
|
this.loc = metadata.loc;
|
|
36
38
|
}
|
|
37
39
|
|
|
@@ -40,6 +42,13 @@ export class SourceUnit implements SlangNode {
|
|
|
40
42
|
print: PrintFunction,
|
|
41
43
|
options: ParserOptions<AstNode>
|
|
42
44
|
): Doc {
|
|
43
|
-
return [
|
|
45
|
+
return [
|
|
46
|
+
path.call(print, 'members'),
|
|
47
|
+
// Prettier's Markdown formatter already appends a new line on code
|
|
48
|
+
// blocks, therefore we avoid trailing with a new line at the end of
|
|
49
|
+
// a file in this case.
|
|
50
|
+
// https://github.com/prettier-solidity/prettier-plugin-solidity/issues/764
|
|
51
|
+
options.parentParser ? '' : line
|
|
52
|
+
];
|
|
44
53
|
}
|
|
45
54
|
}
|