Jump to content

Module talk:Languagename

From translatewiki.net

Module:Languagename Code Review

LLM review.

Priority 1: Bug - Unhandled error propagation

Line 46 uses error() which will throw an uncaught exception and produce an ugly script error in wikitext. For a template-facing module, this should return a trackable error category or use mw.addWarning() instead. (Note: the return keyword before error() is dead code, since error() never returns.)

-- Current (line 46):
return error( 'Missing language code "' .. mwLang:toBcp47Code() .. '"' )

-- Suggested:
return '<span class="error">Missing language code "' .. mwLang:toBcp47Code() .. '"</span>'
    .. '[[Category:Pages with invalid language codes]]'

Priority 2: Logic issue - Fallback data lookup uses wrong comparison

Line 35 compares lang ~= mwCode to decide between English name and autonym. However, lang can be nil at this point (if the caller omitted it). In Lua, nil ~= mwCode is always true, so the autonym branch is unreachable when lang is nil. This is inconsistent with the built-in path (line 26) where nil lang returns the autonym.

-- Current (lines 35-39):
if lang ~= mwCode then
    return data[ mwCode ][ 'en' ]
end
return data[ mwCode ][ 'autonym' ]

-- Suggested:
if isEmpty( lang ) or lang == mwCode then
    return data[ mwCode ][ 'autonym' ]
end
return data[ mwCode ][ 'en' ]

Priority 3: Potential nil access - data structure not validated

Lines 35-39 access data[mwCode]['en'] and data[mwCode]['autonym'] without verifying those nested keys exist. If the JSON entry lacks an 'en' or 'autonym' field, this will return nil silently.

-- Add a guard:
local entry = data[ mwCode ]
if entry == nil then
    -- fall through to error handling below
elseif isEmpty( lang ) or lang == mwCode then
    return entry[ 'autonym' ] or entry[ 'en' ] or mwLang:toBcp47Code()
else
    return entry[ 'en' ] or entry[ 'autonym' ] or mwLang:toBcp47Code()
end

Priority 4: Minor - mw.language.new() with invalid codes

Line 21 calls mw.language.new( code ) before validating the code. Beyond unexpected normalization in getCode(), a malformed code (e.g. one containing characters disallowed in language codes) can cause mw.language.new() itself to raise an error. That error is thrown before reaching the graceful error handling at the bottom of _main, so some invalid inputs bypass the intended tracking/error path entirely. Consider validating the code shape (or wrapping the call in pcall) before creating the object.

Priority 5: Style - Module header comment

The header says "Port Template:Languagename to Lua" but provides no documentation on parameters, return values, or usage. Adding a brief LDoc-style comment would improve maintainability.

Summary

PriorityIssueSeverity
1Unhandled error() (line 46) causes script errors in articlesHigh
2Autonym fallback unreachable when lang is nil (line 35)Medium
3No nil-guard on nested JSON data access (lines 35-39)Low
4Language object created before validation; malformed codes can throw early (line 21)Low
5Missing module documentationInfo