Skip to content

Commit 9ca90f7

Browse files
authored
update JuliaSyntax to v1 (#121)
* update JuliaSyntax to v1 * fix bug and add test * fix random typos * fix test setup * fix deprecation
1 parent 9ad890e commit 9ca90f7

File tree

7 files changed

+33
-15
lines changed

7 files changed

+33
-15
lines changed

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "PackageAnalyzer"
22
uuid = "e713c705-17e4-4cec-abe0-95bf5bf3e10c"
33
authors = ["Mosè Giordano <[email protected]>"]
4-
version = "3.1.0"
4+
version = "3.2.0"
55

66
[deps]
77
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
@@ -28,7 +28,7 @@ Git = "1.2.1"
2828
GitHub = "5.4"
2929
Legolas = "0.5"
3030
JSON3 = "1.5.1"
31-
JuliaSyntax = "0.4"
31+
JuliaSyntax = "1"
3232
LicenseCheck = "0.2"
3333
RegistryInstances = "0.1"
3434
julia = "1.10"

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ Package Flux:
5454

5555
See the [docs](https://JuliaEcosystem.github.io/PackageAnalyzer.jl/dev/) for more!
5656

57+
One special feature of PackageAnalyzer is a custom JuliaSyntax-based line-of-code counting implementation which correctly distinguishes between Julia docstrings, comments, and code.
58+
5759
## Talks and blog posts
5860

5961
Check out our JuliaCon 2023 talk:

src/LineCategories.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
module CategorizeLines
44
export LineCategories, LineCategory, Blank, Code, Docstring, Comment, categorize_lines!
55

6-
using JuliaSyntax: haschildren, children, SourceFile, kind, @K_str, source_line
6+
using JuliaSyntax: JuliaSyntax, children, SourceFile, kind, @K_str, source_line
77

88
# Every line will have a single category. This way the total number across all categories
99
# equals the total number of lines. This is useful for debugging and is reassuring to users.
@@ -32,7 +32,7 @@ Currently:
3232
@enum LineCategory Blank Code Comment Docstring
3333

3434
# We will store the categories assigned to each line in a file with the following structure.
35-
# This keeps the `SourceFile` to facillitate printing.
35+
# This keeps the `SourceFile` to facilitate printing.
3636
"""
3737
LineCategories(path)
3838
@@ -84,11 +84,11 @@ function categorize_lines!(d::LineCategories, node, source, nesting=0, pos=1, pa
8484
k = kind(node)
8585

8686
# Recurse over children
87-
is_leaf = !haschildren(node)
87+
is_leaf = JuliaSyntax.is_leaf(node)
8888
if !is_leaf
8989
new_nesting = nesting + 1
9090
p = pos
91-
for x in children(node)
91+
for x in something(children(node), ())
9292
categorize_lines!(d, x, source, new_nesting, p, k)
9393
p += x.span
9494
end

src/analyze.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ PackageV1 Pluto:
4141
* tree hash: db1306745717d127037c5697436b04cfb9d7b3dd
4242
* Julia code in `src`: 8337 lines
4343
* Julia code in `test`: 5448 lines (39.5% of `test` + `src`)
44-
* documention in `docs`: 0 lines (0.0% of `docs` + `src`)
45-
* documention in README: 118 lines
44+
* documentation in `docs`: 0 lines (0.0% of `docs` + `src`)
45+
* documentation in README: 118 lines
4646
* has license(s) in file: MIT
4747
* filename: LICENSE
4848
* OSI approved: true
@@ -102,8 +102,8 @@ PackageV1 DataFrames:
102102
* tree hash: db2a9cb664fcea7836da4b414c3278d71dd602d2
103103
* Julia code in `src`: 15628 lines
104104
* Julia code in `test`: 21089 lines (57.4% of `test` + `src`)
105-
* documention in `docs`: 6270 lines (28.6% of `docs` + `src`)
106-
* documention in README: 21 lines
105+
* documentation in `docs`: 6270 lines (28.6% of `docs` + `src`)
106+
* documentation in README: 21 lines
107107
* has license(s) in file: MIT
108108
* filename: LICENSE.md
109109
* OSI approved: true
@@ -138,7 +138,7 @@ end
138138
analyze_manifest([path_to_manifest]; registries=reachable_registries(),
139139
auth=github_auth(), sleep=0)
140140
141-
Convienence function to run [`find_packages_in_manifest`](@ref) then [`analyze`](@ref) on the results. Positional argument `path_to_manifest` defaults to `joinpath(dirname(Base.active_project()), "Manifest.toml")`.
141+
Convenience function to run [`find_packages_in_manifest`](@ref) then [`analyze`](@ref) on the results. Positional argument `path_to_manifest` defaults to `joinpath(dirname(Base.active_project()), "Manifest.toml")`.
142142
"""
143143
function analyze_manifest(args...; registries=reachable_registries(),
144144
auth=github_auth(), sleep=0)

src/count_loc.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,16 @@ struct GreenNodeWrapper
9494
end
9595

9696
function AbstractTrees.children(wrapper::GreenNodeWrapper)
97-
return map(n -> GreenNodeWrapper(n, wrapper.source), JuliaSyntax.children(wrapper.node))
97+
return map(n -> GreenNodeWrapper(n, wrapper.source), something(JuliaSyntax.children(wrapper.node), ()))
9898
end
9999

100+
const EMPTY_TREE = JuliaSyntax.GreenNode(JuliaSyntax.SyntaxHead(K"toplevel", 0), 0, JuliaSyntax.GreenNode{JuliaSyntax.SyntaxHead}[])
101+
100102
function parse_green_node(file_path)
101103
file = read(file_path, String)
102104
parsed = @maybecatch(JuliaSyntax.parseall(JuliaSyntax.GreenNode, file; ignore_trivia=false, filename=file_path),
103105
"Error during `JuliaSyntax.parseall` of $(file_path)",
104-
JuliaSyntax.GreenNode(JuliaSyntax.SyntaxHead(K"toplevel", 0), 0, ()))
106+
EMPTY_TREE)
105107
return GreenNodeWrapper(parsed, JuliaSyntax.SourceFile(file; filename=file_path))
106108
end
107109

@@ -127,7 +129,7 @@ function count_julia_lines_of_code(dir)
127129
table = LinesOfCodeV2[]
128130
for path in readdir(dir; join=true)
129131
n_files = 0
130-
# Could be a LittleDict for efficency, but that would require
132+
# Could be a LittleDict for efficiency, but that would require
131133
# a dependency on OrderedCollections.jl
132134
counts = Dict{LineCategory,Int}(Comment => 0,
133135
Blank => 0,

src/utilities.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"""
66
PackageAnalyzer.github_auth(token::String="")
77
8-
Obtain a GitHub authetication. Use the `token` argument if it is non-empty,
8+
Obtain a GitHub authentication. Use the `token` argument if it is non-empty,
99
otherwise use the `GITHUB_TOKEN` and `GITHUB_AUTH` environment variables, if set
1010
and of length 40. If all these methods fail, return an anonymous
1111
authentication.

test/runtests.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,3 +451,17 @@ end
451451
finally
452452
PackageAnalyzer.CATCH_EXCEPTIONS[] = catch_exceptions_value
453453
end
454+
455+
# This test relies on catching exceptions
456+
try
457+
PackageAnalyzer.CATCH_EXCEPTIONS[] = true
458+
@testset "Graceful handling of unparsable files" begin
459+
# This codebase includes an uncommented `TODO` which fails to parse
460+
# in `sample/test1.jl` we should just skip that file instead of erroing out
461+
pkg = analyze("Pluto"; version=v"0.18.0")
462+
l_src = PackageAnalyzer.sum_julia_loc(pkg, "src")
463+
@test l_src > 6000
464+
end
465+
catch
466+
PackageAnalyzer.CATCH_EXCEPTIONS[] = catch_exceptions_value
467+
end

0 commit comments

Comments
 (0)