[File] lua file detected as javascript

Vadim vad.mailingfile at vad.cx
Wed Apr 16 13:13:51 UTC 2025


Thanks. You would have seen the rule if you ran file with "-d".

The rule is too broad and only needs function[( ] to declare JS. I would 
make async mandatory to keep this rule.

$ file -d defaults.lua

*unknown*, 42: > 0 regex,=\((async )?function[( ],"JavaScript source"]
0 == 0 = 1 strength=68
[try ascmagic 1]
defaults.lua: JavaScript source, Unicode text, UTF-8 text


https://github.com/file/file/blob/f77a1092e1862c2295a21077c9e28c2614a0eede/magic/Magdir/javascript#L42 


!:ext     js
0    regex    \\((async\x20)?function[(\x20]    JavaScript source
!:strength +20
!:mime    application/javascript
!:ext     js


I attached 3 patches to fix this problem and add detection for similar 
Lua scripts that make use of "require". Christos, please let me know if 
these are OK for merging.


On 4/14/25 20:02, Avid Seeker wrote:
> $ wget 
> 'https://raw.githubusercontent.com/ibhagwan/fzf-lua/refs/heads/main/lua/fzf-lua/defaults.lua'
> && file defaults.lua
>
> Expected text/plain but file reads that as: `defaults.lua: JavaScript 
> source,
> Unicode text, UTF-8 text`
>
> I reached the following as a minimal reproducible example. If the last
> function is deleted, the file is detected as text/plain.
>
> ```
> local path = require "fzf-lua.path"
> local utils = require "fzf-lua.utils"
> local actions = require "fzf-lua.actions"
> local previewers = require "fzf-lua.previewer"
>
> local M = {}
>
> function M._default_previewer_fn()
>  local winopts = M.globals.winopts
>  if type(winopts) == "function" then
>    winopts = winopts() or {}
>    winopts.preview = type(winopts.preview) == "table" and 
> winopts.preview or {}
>    winopts.preview.default = winopts.preview.default or 
> M.defaults.winopts.preview.default
>  end
>  local previewer = M.globals.default_previewer or winopts.preview.default
>  -- the setup function cannot have a custom previewer as deepcopy
>  -- fails with stack overflow while trying to copy the custom class
>  -- the workaround is to define the previewer as a function instead
>  -- https://github.com/ibhagwan/fzf-lua/issues/677
>  return type(previewer) == "function" and previewer() or previewer
> end
>
> function M._preview_pager_fn()
>  return vim.fn.executable("delta") == 1 and ("delta --width=$COLUMNS 
> --%s"):format(vim.o.bg) or
>      nil
> end
>
> function M._man_cmd_fn(bat_pager)
>  local cmd = utils.is_darwin() and "man -P cat"
>      or vim.fn.executable("mandb") == 1 and "man"
>      or "man -c"
>  local bat_cmd = bat_pager and (function()
>    for _, bin in ipairs({ "batcat", "bat" }) do
>      if vim.fn.executable(bin) == 1 then
>        return string.format("%s --color=always -p -l man", bin)
>      end
>    end
>  end)()
>  local pager = bat_cmd or "col -bx"
>  return string.format("%s %%s 2>/dev/null | %s", cmd, pager)
> end
> ```
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-Fix-false-positive-for-Lua-detected-as-Javascript.patch
Type: text/x-patch
Size: 1049 bytes
Desc: not available
URL: <https://mailman.astron.com/pipermail/file/attachments/20250416/05d972dd/attachment.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0002-Add-file-extension-to-previous-Lua-magic-rules.patch
Type: text/x-patch
Size: 1000 bytes
Desc: not available
URL: <https://mailman.astron.com/pipermail/file/attachments/20250416/05d972dd/attachment-0001.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-Add-regex-magic-for-Lua-to-match-require.patch
Type: text/x-patch
Size: 1626 bytes
Desc: not available
URL: <https://mailman.astron.com/pipermail/file/attachments/20250416/05d972dd/attachment-0002.bin>


More information about the File mailing list