File tree Expand file tree Collapse file tree 6 files changed +26
-19
lines changed
Expand file tree Collapse file tree 6 files changed +26
-19
lines changed Original file line number Diff line number Diff line change 11import { doc } from 'prettier' ;
2- import { coerce , satisfies } from 'semver' ;
2+ import { satisfies } from 'semver' ;
33import { NonterminalKind } from '@nomicfoundation/slang/cst' ;
44import { getNodeMetadata , updateMetadata } from '../slang-utils/metadata.js' ;
55import { Identifier } from './Identifier.js' ;
@@ -44,10 +44,9 @@ export class ContractDefinition implements SlangNode {
4444 this . cleanModifierInvocationArguments ( options ) ;
4545 }
4646
47- cleanModifierInvocationArguments ( options : ParserOptions < AstNode > ) : void {
47+ cleanModifierInvocationArguments ( { compiler } : ParserOptions < AstNode > ) : void {
4848 // Older versions of Solidity defined a constructor as a function having
4949 // the same name as the contract.
50- const compiler = coerce ( options . compiler ) ;
5150 if ( compiler && ! satisfies ( compiler , '>=0.5.0' ) ) {
5251 for ( const member of this . members . items ) {
5352 if (
Original file line number Diff line number Diff line change 1- import { coerce , satisfies } from 'semver' ;
1+ import { satisfies } from 'semver' ;
22import { NonterminalKind } from '@nomicfoundation/slang/cst' ;
33import { printFunction } from '../slang-printers/print-function.js' ;
44import { getNodeMetadata , updateMetadata } from '../slang-utils/metadata.js' ;
@@ -54,7 +54,7 @@ export class FunctionDefinition implements SlangNode {
5454
5555 // Older versions of Solidity defined a constructor as a function having
5656 // the same name as the contract.
57- const compiler = coerce ( options . compiler ) ;
57+ const compiler = options . compiler ;
5858 if ( compiler && satisfies ( compiler , '>=0.5.0' ) ) {
5959 this . cleanModifierInvocationArguments ( ) ;
6060 }
Original file line number Diff line number Diff line change 11import { doc } from 'prettier' ;
2- import { coerce , satisfies } from 'semver' ;
2+ import { satisfies } from 'semver' ;
33import { NonterminalKind } from '@nomicfoundation/slang/cst' ;
44import { printSeparatedList } from '../slang-printers/print-separated-list.js' ;
55import { getNodeMetadata , updateMetadata } from '../slang-utils/metadata.js' ;
@@ -40,7 +40,7 @@ export class ImportDeconstructionSymbols implements SlangNode {
4040 print : PrintFunction ,
4141 options : ParserOptions < AstNode >
4242 ) : Doc {
43- const compiler = coerce ( options . compiler ) ;
43+ const compiler = options . compiler ;
4444 return printSeparatedList (
4545 path . map ( print , 'items' ) ,
4646 compiler && satisfies ( compiler , '>=0.7.4' )
Original file line number Diff line number Diff line change @@ -34,19 +34,25 @@ export function createParser(
3434 text : string ,
3535 options : ParserOptions < AstNode >
3636) : { parser : Parser ; parseOutput : ParseOutput } {
37- const compiler = minSatisfying ( supportedVersions , options . compiler ) ;
37+ const compiler = options . compiler ;
3838 if ( compiler ) {
39- const result = parserAndOutput ( text , compiler ) ;
39+ const version = minSatisfying (
40+ supportedVersions ,
41+ typeof compiler === 'string' ? compiler : compiler . version
42+ ) ;
43+ if ( version ) {
44+ const result = parserAndOutput ( text , version ) ;
4045
41- if ( ! result . parseOutput . isValid ( ) )
42- throw createError (
43- result ,
44- `Based on the compiler option provided, we inferred your code to be using Solidity version ${
45- result . parser . languageVersion
46- } . If you would like to change that, specify a different version in your \`.prettierrc\` file.`
47- ) ;
46+ if ( ! result . parseOutput . isValid ( ) )
47+ throw createError (
48+ result ,
49+ `Based on the compiler option provided, we inferred your code to be using Solidity version ${
50+ result . parser . languageVersion
51+ } . If you would like to change that, specify a different version in your \`.prettierrc\` file.`
52+ ) ;
4853
49- return result ;
54+ return result ;
55+ }
5056 }
5157
5258 const inferredRanges : string [ ] = LanguageFacts . inferLanguageVersions ( text ) ;
Original file line number Diff line number Diff line change 11// https://prettier.io/docs/en/plugins.html#parsers
22import { SourceUnit as SlangSourceUnit } from '@nomicfoundation/slang/ast' ;
3+ import { coerce } from 'semver' ;
34import { clearOffsets } from './slang-utils/metadata.js' ;
45import { createParser } from './slang-utils/create-parser.js' ;
56import { SourceUnit } from './slang-nodes/SourceUnit.js' ;
@@ -14,7 +15,7 @@ export default function parse(
1415 const { parser, parseOutput } = createParser ( text , options ) ;
1516
1617 // We update the compiler version by the inferred one.
17- options . compiler = parser . languageVersion ;
18+ options . compiler = coerce ( parser . languageVersion ) ;
1819 const parsed = new SourceUnit (
1920 new SlangSourceUnit ( parseOutput . tree . asNonterminalNode ( ) ) ,
2021 options
Original file line number Diff line number Diff line change 11import type { NonterminalKind , TerminalKind } from '@nomicfoundation/slang/cst' ;
22import type * as ast from '@nomicfoundation/slang/ast' ;
33import type { AstPath , Doc , ParserOptions } from 'prettier' ;
4+ import type { SemVer } from 'semver' ;
45import type { AstNode , Comment , StrictAstNode } from './slang-nodes/types.d.ts' ;
56
67// Adding our own options to prettier's `ParserOptions` interface.
78declare module 'prettier' {
89 interface ParserOptions {
9- compiler : string ;
10+ compiler : string | SemVer | null ;
1011 }
1112}
1213
You can’t perform that action at this time.
0 commit comments