[PR #68] [MERGED] enhance: lua code parse and format dump #69

Closed
opened 2026-02-28 01:20:46 +03:00 by kerem · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/tufanbarisyildirim/gonginx/pull/68
Author: @0xJacky
Created: 4/15/2025
Status: Merged
Merged: 4/29/2025
Merged by: @tufanbarisyildirim

Base: masterHead: master


📝 Commits (4)

  • af8a79b enhance: lua code parse and dump
  • d0874db enhance: improve variable reference handling in lexer
  • d120448 chore: update Go version to 1.24 and adjust workflow for Ubuntu 24.04
  • 3587724 chore(go): return to v1.22

📊 Changes

13 files changed (+633 additions, -41 deletions)

View changed files

📝 .github/workflows/check.yml (+2 -2)
📝 .gitignore (+2 -0)
📝 config/lua_block.go (+6 -4)
📝 dumper/dumper.go (+2 -11)
dumper/lua.go (+63 -0)
📝 go.mod (+10 -3)
📝 go.sum (+303 -0)
📝 parser/lexer.go (+52 -11)
📝 parser/lexer_test.go (+28 -0)
📝 parser/parser.go (+54 -4)
📝 parser/parser_test.go (+76 -6)
📝 parser/token/token.go (+3 -0)
testdata/issues/20.conf (+32 -0)

📄 Description

Fixed #20, #26.

This PR introduces Lua code parsing and formatting capabilities by integrating imega/luaformatter. The implementation addresses comment positioning through line break detection for lua code and ensures stable output through multiple serialization cycles.

Key Changes:

  1. Added lexer rules with EndOfLine token type for lua comment line handling
  2. Integrated luaformatter for Lua code formatting
  3. Updated test cases to verify comment position preservation
  4. Ensured compatibility with existing NGINX configuration formatting

Sample Formatting:

-- Input
location / { content_by_lua_block { -- Initialize
local foo = "bar" -- Define variable }

-- Formatted Output
location / {
    content_by_lua_block {
        -- Initialize
        local foo = "bar" -- Define variable
    }
}

Verification:

  • Passed all unit tests
  • Manually verified complex Lua use cases
  • Validated compatibility with existing NGINX configurations
  • Verified stable output across multiple dump cycles
  • Conducted fuzz testing on format-parse-format workflows

Original Content:

server {
    listen 80;
    listen [::]:80;
    server_name _;
    location / { 
        content_by_lua_block { -- comment
local foo = "bar" -- comment } }
    location = /random  {
        set_by_lua_block $file_name {
# comment contained unexpect '{'
            local t = ngx.var.uri 
            local query = string.find(t, "?", 1) 
            if query ~= nil then 
                t = string.sub(t, 1, query-1) 
            end
            return t;
        }
        set_by_lua_block $random {
            # comment contained unexpect '{'
            return math.random(1, 100)
        }
        return 403 "Random number: $random";
   }
}

Parse and formatted content:

server {
    listen 80;
    listen [::]:80;
    server_name _;
    location / {
        content_by_lua_block {
            # comment
            local foo = "bar" # comment
        }
    }
    location = /random {
        set_by_lua_block $file_name {
            # comment contained unexpect '{'
            local t = ngx.var.uri
            local query = string.find(t, "?", 1)

            if query ~= nil then
             t = string.sub(t, 1, query - 1)
            end

            return t
        }
        set_by_lua_block $random {
            # comment contained unexpect '{'
            return math.random(1, 100)
        }
        return 403 "Random number: $random";
    }
}

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/tufanbarisyildirim/gonginx/pull/68 **Author:** [@0xJacky](https://github.com/0xJacky) **Created:** 4/15/2025 **Status:** ✅ Merged **Merged:** 4/29/2025 **Merged by:** [@tufanbarisyildirim](https://github.com/tufanbarisyildirim) **Base:** `master` ← **Head:** `master` --- ### 📝 Commits (4) - [`af8a79b`](https://github.com/tufanbarisyildirim/gonginx/commit/af8a79b2217e7757e800ad82ff396ab25d060eb1) enhance: lua code parse and dump - [`d0874db`](https://github.com/tufanbarisyildirim/gonginx/commit/d0874db68b7ff69b143d765c262934349baee942) enhance: improve variable reference handling in lexer - [`d120448`](https://github.com/tufanbarisyildirim/gonginx/commit/d120448e89a4e9ac8b62b1acf058f68b29b9b572) chore: update Go version to 1.24 and adjust workflow for Ubuntu 24.04 - [`3587724`](https://github.com/tufanbarisyildirim/gonginx/commit/35877245a3534d016b59a02c0b517d32291e52f5) chore(go): return to v1.22 ### 📊 Changes **13 files changed** (+633 additions, -41 deletions) <details> <summary>View changed files</summary> 📝 `.github/workflows/check.yml` (+2 -2) 📝 `.gitignore` (+2 -0) 📝 `config/lua_block.go` (+6 -4) 📝 `dumper/dumper.go` (+2 -11) ➕ `dumper/lua.go` (+63 -0) 📝 `go.mod` (+10 -3) 📝 `go.sum` (+303 -0) 📝 `parser/lexer.go` (+52 -11) 📝 `parser/lexer_test.go` (+28 -0) 📝 `parser/parser.go` (+54 -4) 📝 `parser/parser_test.go` (+76 -6) 📝 `parser/token/token.go` (+3 -0) ➕ `testdata/issues/20.conf` (+32 -0) </details> ### 📄 Description Fixed #20, #26. This PR introduces Lua code parsing and formatting capabilities by integrating [imega/luaformatter](https://github.com/imega/luaformatter). The implementation addresses comment positioning through line break detection for lua code and ensures stable output through multiple serialization cycles. **Key Changes:** 1. Added lexer rules with `EndOfLine` token type for lua comment line handling 2. Integrated luaformatter for Lua code formatting 3. Updated test cases to verify comment position preservation 4. Ensured compatibility with existing NGINX configuration formatting **Sample Formatting:** ```lua -- Input location / { content_by_lua_block { -- Initialize local foo = "bar" -- Define variable } -- Formatted Output location / { content_by_lua_block { -- Initialize local foo = "bar" -- Define variable } } ``` **Verification:** - [x] Passed all unit tests - [x] Manually verified complex Lua use cases - [x] Validated compatibility with existing NGINX configurations - [x] Verified stable output across multiple dump cycles - [x] Conducted fuzz testing on format-parse-format workflows Original Content: ```nginx server { listen 80; listen [::]:80; server_name _; location / { content_by_lua_block { -- comment local foo = "bar" -- comment } } location = /random { set_by_lua_block $file_name { # comment contained unexpect '{' local t = ngx.var.uri local query = string.find(t, "?", 1) if query ~= nil then t = string.sub(t, 1, query-1) end return t; } set_by_lua_block $random { # comment contained unexpect '{' return math.random(1, 100) } return 403 "Random number: $random"; } } ``` Parse and formatted content: ```nginx server { listen 80; listen [::]:80; server_name _; location / { content_by_lua_block { # comment local foo = "bar" # comment } } location = /random { set_by_lua_block $file_name { # comment contained unexpect '{' local t = ngx.var.uri local query = string.find(t, "?", 1) if query ~= nil then t = string.sub(t, 1, query - 1) end return t } set_by_lua_block $random { # comment contained unexpect '{' return math.random(1, 100) } return 403 "Random number: $random"; } } ``` --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
kerem 2026-02-28 01:20:46 +03:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/gonginx#69
No description provided.