-
Notifications
You must be signed in to change notification settings - Fork 77
Slang 1.1.0 #1123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Slang 1.1.0 #1123
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/slang-comments/handlers/handle-contract-specifiers-comments.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { NonterminalKind } from '@nomicfoundation/slang/cst'; | ||
import { util } from 'prettier'; | ||
import addCollectionNodeLastComment from './add-collection-node-last-comment.js'; | ||
|
||
import type { HandlerParams } from './types.d.ts'; | ||
|
||
const { addTrailingComment } = util; | ||
|
||
export default function handleContractSpecifiersComments({ | ||
precedingNode, | ||
enclosingNode, | ||
comment | ||
}: HandlerParams): boolean { | ||
if (enclosingNode?.kind !== NonterminalKind.ContractSpecifiers) { | ||
return false; | ||
} | ||
|
||
if ( | ||
precedingNode && | ||
precedingNode.kind === NonterminalKind.ContractSpecifier | ||
) { | ||
if (precedingNode.variant.kind === NonterminalKind.InheritanceSpecifier) { | ||
addCollectionNodeLastComment(precedingNode.variant.types, comment); | ||
return true; | ||
} | ||
if (precedingNode.variant.kind === NonterminalKind.StorageLayoutSpecifier) { | ||
addTrailingComment(precedingNode.variant.expression, comment); | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} |
23 changes: 23 additions & 0 deletions
23
src/slang-comments/handlers/handle-storage-layout-specifier-comments.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { NonterminalKind } from '@nomicfoundation/slang/cst'; | ||
import { util } from 'prettier'; | ||
|
||
import type { HandlerParams } from './types.d.ts'; | ||
|
||
const { addLeadingComment } = util; | ||
|
||
export default function handleStorageLayoutSpecifierComments({ | ||
enclosingNode, | ||
followingNode, | ||
comment | ||
}: HandlerParams): boolean { | ||
if (enclosingNode?.kind !== NonterminalKind.StorageLayoutSpecifier) { | ||
return false; | ||
} | ||
|
||
if (followingNode?.kind === NonterminalKind.Expression) { | ||
addLeadingComment(followingNode, comment); | ||
return true; | ||
} | ||
|
||
return false; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,29 @@ | ||
import handleBlockComments from './handle-block-comments.js'; | ||
import handleContractDefinitionComments from './handle-contract-definition-comments.js'; | ||
import handleContractSpecifiersComments from './handle-contract-specifiers-comments.js'; | ||
import handleElseBranchComments from './handle-else-branch-comments.js'; | ||
import handleIfStatementComments from './handle-if-statement-comments.js'; | ||
import handleInterfaceDefinitionComments from './handle-interface-definition-comments.js'; | ||
import handleLibraryDefinitionComments from './handle-library-definition-comments.js'; | ||
import handleModifierInvocationComments from './handle-modifier-invocation-comments.js'; | ||
import handleParametersDeclarationComments from './handle-parameters-declaration-comments.js'; | ||
import handlePositionalArgumentsDeclarationComments from './handle-positional-arguments-declaration-comments.js'; | ||
import handleStorageLayoutSpecifierComments from './handle-storage-layout-specifier-comments.js'; | ||
import handleWhileStatementComments from './handle-while-statement-comments.js'; | ||
import handleYulBlockComments from './handle-yul-block-comments.js'; | ||
|
||
export default [ | ||
handleBlockComments, | ||
handleContractDefinitionComments, | ||
handleContractSpecifiersComments, | ||
handleElseBranchComments, | ||
handleIfStatementComments, | ||
handleInterfaceDefinitionComments, | ||
handleLibraryDefinitionComments, | ||
handleModifierInvocationComments, | ||
handleParametersDeclarationComments, | ||
handlePositionalArgumentsDeclarationComments, | ||
handleStorageLayoutSpecifierComments, | ||
handleWhileStatementComments, | ||
handleYulBlockComments | ||
]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { NonterminalKind } from '@nomicfoundation/slang/cst'; | ||
import { getNodeMetadata, updateMetadata } from '../slang-utils/metadata.js'; | ||
import { InheritanceSpecifier } from './InheritanceSpecifier.js'; | ||
import { StorageLayoutSpecifier } from './StorageLayoutSpecifier.js'; | ||
|
||
import type * as ast from '@nomicfoundation/slang/ast'; | ||
import type { AstPath, Doc, ParserOptions } from 'prettier'; | ||
import type { AstNode } from './types.js'; | ||
import type { PrintFunction, SlangNode } from '../types.js'; | ||
|
||
export class ContractSpecifier implements SlangNode { | ||
readonly kind = NonterminalKind.ContractSpecifier; | ||
|
||
comments; | ||
|
||
loc; | ||
|
||
variant: InheritanceSpecifier | StorageLayoutSpecifier; | ||
|
||
constructor(ast: ast.ContractSpecifier, options: ParserOptions<AstNode>) { | ||
let metadata = getNodeMetadata(ast); | ||
|
||
switch (ast.variant.cst.kind) { | ||
case NonterminalKind.InheritanceSpecifier: | ||
this.variant = new InheritanceSpecifier( | ||
ast.variant as ast.InheritanceSpecifier, | ||
options | ||
); | ||
break; | ||
case NonterminalKind.StorageLayoutSpecifier: | ||
this.variant = new StorageLayoutSpecifier( | ||
ast.variant as ast.StorageLayoutSpecifier, | ||
options | ||
); | ||
break; | ||
default: | ||
throw new Error(`Unexpected variant: ${ast.variant.cst.kind}`); | ||
} | ||
metadata = updateMetadata(metadata, [this.variant]); | ||
|
||
this.comments = metadata.comments; | ||
this.loc = metadata.loc; | ||
} | ||
|
||
print(path: AstPath<ContractSpecifier>, print: PrintFunction): Doc { | ||
return path.call(print, 'variant'); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { doc } from 'prettier'; | ||
import { NonterminalKind } from '@nomicfoundation/slang/cst'; | ||
import { sortContractSpecifiers } from '../slang-utils/sort-contract-specifiers.js'; | ||
import { printSeparatedList } from '../slang-printers/print-separated-list.js'; | ||
import { getNodeMetadata, updateMetadata } from '../slang-utils/metadata.js'; | ||
import { ContractSpecifier } from './ContractSpecifier.js'; | ||
|
||
import type * as ast from '@nomicfoundation/slang/ast'; | ||
import type { AstPath, Doc, ParserOptions } from 'prettier'; | ||
import type { AstNode } from './types.js'; | ||
import type { PrintFunction, SlangNode } from '../types.js'; | ||
|
||
const { group, ifBreak, line, softline } = doc.builders; | ||
|
||
export class ContractSpecifiers implements SlangNode { | ||
readonly kind = NonterminalKind.ContractSpecifiers; | ||
|
||
comments; | ||
|
||
loc; | ||
|
||
items: ContractSpecifier[]; | ||
|
||
constructor(ast: ast.ContractSpecifiers, options: ParserOptions<AstNode>) { | ||
let metadata = getNodeMetadata(ast, true); | ||
|
||
this.items = ast.items.map((item) => new ContractSpecifier(item, options)); | ||
|
||
metadata = updateMetadata(metadata, [this.items]); | ||
|
||
this.comments = metadata.comments; | ||
this.loc = metadata.loc; | ||
|
||
this.items = this.items.sort(sortContractSpecifiers); | ||
} | ||
|
||
print(path: AstPath<ContractSpecifiers>, print: PrintFunction): Doc { | ||
if (this.items.length === 0) return ''; | ||
|
||
const [specifier1, specifier2] = path.map(print, 'items'); | ||
if (typeof specifier2 === 'undefined') return [' ', specifier1]; | ||
|
||
const groupId = Symbol('Slang.ContractSpecifiers.inheritance'); | ||
return printSeparatedList( | ||
[group(specifier1, { id: groupId }), specifier2], | ||
{ | ||
firstSeparator: line, | ||
separator: ifBreak('', softline, { groupId }) | ||
} | ||
); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { doc } from 'prettier'; | ||
import { NonterminalKind } from '@nomicfoundation/slang/cst'; | ||
import { printSeparatedItem } from '../slang-printers/print-separated-item.js'; | ||
import { getNodeMetadata, updateMetadata } from '../slang-utils/metadata.js'; | ||
import { Expression } from './Expression.js'; | ||
|
||
import type * as ast from '@nomicfoundation/slang/ast'; | ||
import type { AstPath, Doc, ParserOptions } from 'prettier'; | ||
import type { AstNode } from './types.js'; | ||
import type { PrintFunction, SlangNode } from '../types.js'; | ||
|
||
const { line } = doc.builders; | ||
|
||
export class StorageLayoutSpecifier implements SlangNode { | ||
readonly kind = NonterminalKind.StorageLayoutSpecifier; | ||
|
||
comments; | ||
|
||
loc; | ||
|
||
expression: Expression; | ||
|
||
constructor( | ||
ast: ast.StorageLayoutSpecifier, | ||
options: ParserOptions<AstNode> | ||
) { | ||
let metadata = getNodeMetadata(ast); | ||
|
||
this.expression = new Expression(ast.expression, options); | ||
|
||
metadata = updateMetadata(metadata, [this.expression]); | ||
|
||
this.comments = metadata.comments; | ||
this.loc = metadata.loc; | ||
} | ||
|
||
print(path: AstPath<StorageLayoutSpecifier>, print: PrintFunction): Doc { | ||
return [ | ||
'layout at', | ||
printSeparatedItem(path.call(print, 'expression'), { | ||
firstSeparator: line | ||
}) | ||
]; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if the parser names might confuse users here:
slang
? since it is the project/NPM package/repository name..deprecated-solidity-parser
instead ofsolidity-parse
? this matches the exact package name, and highlights that it is deprecated.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My thought here is to keep the same historical name for the old parser in order for nothing to be break (while at least triggering a warning) and allow developers to slowly adopt the new parser.
I'm open to change this if @fvictorio thinks it's a better approach.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as for the name
slang
there is a possibility that version2.1.0
could release withslang-yul
alongsideslang-solidity
. if we feel this is a good feature.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. I now remember the conversation about
slang-yul
. Let's keep this as-is then. Thanks!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, since this is a major version, and I don't think we'll have a 3.0 soon, I'd rather have that breaking change now than keep names that are not great. Especially because setting the parser is more likely to happen now. I'll work on this on a separate PR.