Specify the original source location for generated or imported code, providing traceability and attribution for code origins.
Syntax
---@source <source_location> [additional_info]
Source locations can reference:
File paths
Line numbers in files
External libraries and packages
Code generation tools
URLs and repositories
Version control commits
Examples
Reference to Original File
---@source lib/external/math_utils.lua
function calculateDistance ( x1 , y1 , x2 , y2 )
return math.sqrt (( x2 - x1 ) ^ 2 + ( y2 - y1 ) ^ 2 )
end
Specific Line Reference
---@source utils/helpers.lua:42
function formatCurrency ( amount )
return string.format ( "$%.2f" , amount )
end
External Library Reference
---@source npm:lodash/isEqual
function deepEqual ( a , b )
-- Implementation copied from lodash
return isEqual ( a , b )
end
Code Generation
---@source generated/api_client.lua
---@source generator: openapi-codegen v1.2.3
--- @class APIClient
local APIClient = {}
--- @param endpoint string API endpoint
--- @return table Response data
function APIClient : get ( endpoint )
return http . get ( self . baseUrl .. endpoint )
end
Template Source
---@source templates/model.lua.template
--- @class Model
local Model = {}
function Model : save ()
return database . save ( self )
end
function Model : delete ()
return database . delete ( self )
end
Macro Expansion
---@source macros/property.lua
---@macro PROPERTY(name, type)
function defineProperty ( obj , name , type )
obj [ "get" .. name ] = function ( self )
return self [ "_" .. name ]
end
obj [ "set" .. name ] = function ( self , value )
self [ "_" .. name ] = value
end
end
Third-Party Source
---@source github:user/repo/file.lua
---@source commit: abc123def456
function externalFunction ()
-- Code from external repository
end
Stack Overflow Attribution
---@source Stack Overflow answer #12345678
---@source https://stackoverflow.com/questions/12345678
function quickSort ( arr , low , high )
if low < high then
local pi = partition ( arr , low , high )
quickSort ( arr , low , pi - 1 )
quickSort ( arr , pi + 1 , high )
end
end
Specification Implementation
---@source RFC 3986 URI Generic Syntax
---@source https://tools.ietf.org/html/rfc3986
function parseURI ( uri )
local scheme , authority , path , query , fragment =
uri : match ( "^([^:/?#]+):?([^/?#]*)([^?#]*)%??([^#]*)#?(.*)" )
return {
scheme = scheme ,
authority = authority ,
path = path ,
query = query ,
fragment = fragment
}
end
Auto-Generated Code
---@source auto-generated by protoc
---@source proto/user.proto
--- @class UserProto
--- @field id number
--- @field name string
--- @field email string
local UserProto = {}
Transpiled Code
---@source TypeScript: src/utils.ts
---@source transpiler: lua-typescript v2.1.0
function forEach ( array , callback )
for i , item in ipairs ( array ) do
callback ( item , i , array )
end
end
Vendor Code
---@source vendor/json.lua
---@source version: 1.4.2
---@source license: MIT
local json = require ( "json" )
Build-Time Generation
---@source build/generated/constants.lua
---@source build script: scripts/generate_constants.sh
---@source build time: 2024-01-15 14:30:22
local constants = {
VERSION = "1.0.0" ,
BUILD_NUMBER = 42 ,
COMMIT_HASH = "abc123def"
}
AI-Generated Code
---@source AI Assistant: GPT-4
---@source prompt: "Generate a binary search function"
function binarySearch ( arr , target )
local left , right = 1 , # arr
while left <= right do
local mid = math.floor (( left + right ) / 2 )
if arr [ mid ] == target then
return mid
elseif arr [ mid ] < target then
left = mid + 1
else
right = mid - 1
end
end
return nil
end
Migration Scripts
---@source migration: v1.2.3 -> v1.3.0
---@source migration script: scripts/migrate_config.lua
function migrateConfig ( oldConfig )
local newConfig = {}
newConfig . database = oldConfig . db
newConfig . server = {
port = oldConfig . port or 8080 ,
host = oldConfig . host or "localhost"
}
return newConfig
end
Use Cases
Code Provenance
Track where code originated:
---@source lib/utils.lua:line 150-175
---@source author: John Doe
---@source date: 2024-01-15
function helperFunction ()
-- Implementation
end
License Compliance
Document third-party code usage:
---@source https://github.com/example/library
---@source license: MIT License
---@source copyright: Copyright (c) 2024 Example Corp
function thirdPartyFunction ()
-- Licensed code
end
Debugging Generated Code
Trace generated code back to source:
---@source template: templates/dao.lua.j2
---@source generated from: schema.yaml
---@source generator version: 2.1.0
function generatedDAO ()
-- Auto-generated DAO methods
end
Use @source annotations consistently for all generated or imported code to maintain clear traceability throughout your project.
Source annotations are particularly valuable in large projects with multiple code generation tools or extensive third-party dependencies.
Features
Code Traceability Track code origins and provenance
Attribution Credit original authors and sources
Generation Tracking Document generated code sources
License Info Reference licenses and copyrights