Skip to content

Commit

Permalink
fix issues with the unused selections CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
zth committed Feb 24, 2024
1 parent 23731b8 commit 6c2fc11
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 25 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# master

- Fix issue with unused selections CLI.

# 0.6.0

- Up peer dependencies to support ReScript `11.1.0-rc.2` and Core `1.0.0`.
Expand Down
51 changes: 26 additions & 25 deletions cli/UnusedSelections.res
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
let adapter = EdgeDbGenerator__Utils.adapter

let extractFileNameRegExp = %re("/\/([^\/]*?)__edgeql\.\w+\"/")
let extractFileNameRegExp = %re("/\/([^\/]*?)__edgeql\.res/")

let extractFileName = line => {
if line->String.startsWith(" File") {
switch line->String.match(extractFileNameRegExp) {
| Some([_, fileName]) => Some(fileName)
| _ => None
}
} else {
None
switch line->String.trim->String.match(extractFileNameRegExp) {
| Some([_, fileName]) => Some(fileName)
| _ => None
}
}

Expand All @@ -21,7 +17,7 @@ type extractedLineInfo = {
let extractLineInfo = line => {
switch line->String.trim->String.split(" ")->List.fromArray {
| list{info, ...rest} =>
let restText = rest->List.toArray->Array.joinWith(" ")
let restText = rest->List.toArray->Array.joinWith(" ")->String.trim
switch (info->String.split("."), restText) {
| ([queryName, recordName, fieldName], "is a record label never used to read a value")
if !(recordName->String.startsWith("args")) =>
Expand All @@ -43,20 +39,25 @@ let extractFromReanalyzeOutput = (output: string) => {
output
->String.split("\n\n")
->Array.filterMap(text => {
let lines = text->String.split("\n")
let index = ref(0)
lines->Array.findMap(line => {
let currentIndex = index.contents
index := currentIndex + 1
switch (line->String.startsWith(" File \""), lines[currentIndex + 1]) {
| (true, Some(nextLine)) =>
switch (extractFileName(line), extractLineInfo(nextLine)) {
| (Some(fileName), Some(fileInfo)) => Some((fileName, fileInfo))
if text->String.includes("__edgeql.res") {
let lines = text->String.split("\n")
let index = ref(0)
lines->Array.findMap(line => {
let currentIndex = index.contents
index := currentIndex + 1

switch (line->String.includes("__edgeql.res"), lines[currentIndex + 1]) {
| (true, Some(nextLine)) =>
switch (extractFileName(line), extractLineInfo(nextLine)) {
| (Some(fileName), Some(fileInfo)) => Some((fileName, fileInfo))
| _ => None
}
| _ => None
}
| _ => None
}
})
})
} else {
None
}
})
}

Expand All @@ -65,7 +66,7 @@ external childProcess: 'a = "default"

let readReanalyzeOutput = () => {
Promise.make((resolve, reject) => {
let p = childProcess["spawn"]("npx", ["--yes", "reanalyze", "-dce"])
let p = childProcess["spawn"]("npx", ["--yes", "@rescript/tools", "reanalyze", "-dce"])

switch p["stdout"]->Nullable.toOption {
| None =>
Expand Down Expand Up @@ -114,7 +115,7 @@ let reportResults = results => {
byFile
->Dict.toArray
->Array.forEach(((fileName, fileInfos)) => {
Console.log(`\nFile "${fileName}":`)
Console.log(`\nFile "${fileName}.res":`)
let byQuery = Dict.make()
fileInfos->Array.forEach(fileInfo => {
switch byQuery->Dict.get(fileInfo.queryName) {
Expand All @@ -130,9 +131,9 @@ let reportResults = results => {
fileInfo => {
let contextMessage = switch fileInfo.recordPath {
| None | Some([]) => ""
| Some(path) => `${path->Array.joinWith(".")}: `
| Some(path) => `${path->Array.joinWith(".")}`
}
Console.log(` - ${contextMessage}${fileInfo.fieldName}`)
Console.log(` - ${contextMessage}.${fileInfo.fieldName}`)
},
)
})
Expand Down

0 comments on commit 6c2fc11

Please sign in to comment.