Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Reencode flat result to always have unix line endings (#146)
Browse files Browse the repository at this point in the history
* Reencode flat result to always have unix line endings

* Changeset

* Tests

* Formatting and linting fix
  • Loading branch information
mateuszradomski authored Mar 7, 2024
1 parent b70f609 commit 6429980
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
6 changes: 6 additions & 0 deletions packages/discovery/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @l2beat/discovery

## 0.44.3

### Patch Changes

- Flattening results always have unix line encodings

## 0.44.2

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/discovery/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@l2beat/discovery",
"description": "L2Beat discovery - engine & tooling utilized for keeping an eye on L2s",
"version": "0.44.2",
"version": "0.44.3",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"bin": {
Expand Down
17 changes: 13 additions & 4 deletions packages/discovery/src/flatten/flattenStartingFrom.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,21 @@ import {

describe(flattenStartingFrom.name, () => {
const DUMMY_AST_NODE = {}
const LIBRARY_SOURCE = 'library Contract2 { function lf() public {} }'
const BASE_SOURCE = 'contract Contract3 { function bcf() public {} }'
const LIBRARY_SOURCE =
'library Contract2\r\n{\r\n function lf() public {}\r\n }'
const BASE_SOURCE =
'contract Contract3\r\n{\r\n function bcf() public {}\r\n }'
const ROOT_SOURCE =
'contract Contract1 is Contract3 { function cf() public { Contract2.lf(); this.bcf(); } }'
'contract Contract1 is Contract3\r\n {\r\n function cf() public {\r\n Contract2.lf(); this.bcf();\r\n }\r\n }'
const ROOT_FILE_SOURCE = `${LIBRARY_SOURCE}${BASE_SOURCE}${ROOT_SOURCE}`

const LIBRARY_SOURCE_UNIX =
'library Contract2\n{\n function lf() public {}\n }'
const BASE_SOURCE_UNIX =
'contract Contract3\n{\n function bcf() public {}\n }'
const ROOT_SOURCE_UNIX =
'contract Contract1 is Contract3\n {\n function cf() public {\n Contract2.lf(); this.bcf();\n }\n }'

const LIBRARY_CONTRACT: ContractDeclaration = {
name: 'Contract2',
type: 'library',
Expand Down Expand Up @@ -92,7 +101,7 @@ describe(flattenStartingFrom.name, () => {
const result = flattenStartingFrom(rootContractName, parsedFileManager)

expect(result).toEqual(
[LIBRARY_SOURCE, BASE_SOURCE, ROOT_SOURCE].join('\n\n'),
[LIBRARY_SOURCE_UNIX, BASE_SOURCE_UNIX, ROOT_SOURCE_UNIX].join('\n\n'),
)
})

Expand Down
6 changes: 5 additions & 1 deletion packages/discovery/src/flatten/flattenStartingFrom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,17 @@ export function flattenStartingFrom(
stack.push(...getStackEntries(foundContract))
}

return flatSource.trimEnd()
return changeLineEndingsToUnix(flatSource.trimEnd())
}

function formatSource(source: string, byteRange: ByteRange): string {
return source.slice(byteRange.start, byteRange.end + 1) + '\n\n'
}

function changeLineEndingsToUnix(source: string): string {
return source.replace(/\r\n/g, '\n')
}

function getUniqueContractId(entry: ContractFilePair): string {
const hasher = createHash('sha1')
const source = formatSource(entry.file.content, entry.contract.byteRange)
Expand Down

0 comments on commit 6429980

Please sign in to comment.