From fed81803acbf4b632648b1e9e36f757923890068 Mon Sep 17 00:00:00 2001 From: "yndu13(agent)" Date: Tue, 18 Aug 2026 11:08:11 +0800 Subject: [PATCH] fix: map inner export dara filenames to snake_case Python modules Keep websocketUtils.dara imports on websocket_utils regardless of path.sep, and fail codegen if a module name is still camelCase. --- ChangeLog.md | 3 ++ lib/generator.js | 8 ++++-- lib/helper.js | 23 +++++++++++++++ package.json | 2 +- tests/helper.tests.js | 66 +++++++++++++++++++++++++++++++++++++++++++ tests/main.tests.js | 8 ++++++ 6 files changed, 107 insertions(+), 3 deletions(-) create mode 100644 tests/helper.tests.js diff --git a/ChangeLog.md b/ChangeLog.md index e0436eb..f95abcb 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -1,5 +1,8 @@ # ChangeLog +# 2.0.16 +- Resolve inner export dara paths with POSIX/Windows separators and reject camelCase Python module names (websocketUtils → websocket_utils). + # 2.0.15 - Fixed `$Error.message` codegen to avoid accessing non-existent `Exception.message` in Python 3. diff --git a/lib/generator.js b/lib/generator.js index 5ffe914..fecd700 100644 --- a/lib/generator.js +++ b/lib/generator.js @@ -9,6 +9,7 @@ const DSL = require('@darabonba/parser'); const { _name, _vid, _string, _upperFirst, _type, _escape, _subModelName, _camelCase, _snakeCase, _getImport, + _daraPathSegments, _daraModuleStem, _assertPythonModule, _isBinaryOp, _adaptedQuotes, CORE, REQUEST, RESPONSE, _avoidKeywords, _removeFilesInDirectory, _sortImports, _importsToString, @@ -652,10 +653,13 @@ setup( let exceptions = 'exceptions'; if (inner && pkg.exports[inner]) { let pyPath = pkg.exports[inner]; - filename = _snakeCase(pyPath.split(path.sep).slice(-1)[0].replace(/(\.tea)$|(\.spec)$|(\.dara)$/gi, ''), this.config.keywords); + filename = _assertPythonModule( + _snakeCase(_daraModuleStem(pyPath), this.config.keywords), + `export ${inner}` + ); models = filename + '_models'; exceptions = filename + '_exceptions'; - const arr = pyPath.split(path.sep).slice(1, -1); + const arr = _daraPathSegments(pyPath).slice(1, -1); arr.map(key => { classNamespace += '.' + _snakeCase(key, this.config.keywords); }); diff --git a/lib/helper.js b/lib/helper.js index 62a8c93..f980fe7 100644 --- a/lib/helper.js +++ b/lib/helper.js @@ -298,6 +298,26 @@ function _isSnakeCase(str) { return true; } +function _daraPathSegments(daraPath) { + if (!daraPath) { + return []; + } + return String(daraPath).split(/[/\\]/); +} + +function _daraModuleStem(daraPath) { + const segments = _daraPathSegments(daraPath).filter(Boolean); + const base = segments.length ? segments[segments.length - 1] : ''; + return base.replace(/(\.tea)$|(\.spec)$|(\.dara)$/gi, ''); +} + +function _assertPythonModule(name, context) { + if (name && !_isSnakeCase(name)) { + throw new Error(`Python module name must be snake_case (${context}): ${name}`); + } + return name; +} + function _isBasicType(type) { return DSL.util.isBasicType(type); } @@ -689,6 +709,9 @@ module.exports = { _avoidKeywords, _camelCase, _snakeCase, + _daraPathSegments, + _daraModuleStem, + _assertPythonModule, _deepClone, _isSnakeCase, _isBasicType, diff --git a/package.json b/package.json index b6ccd74..82061fd 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@darabonba/python-generator", - "version": "2.0.15", + "version": "2.0.16", "description": "The darabonba generator for Python", "main": "lib/generator.js", "directories": { diff --git a/tests/helper.tests.js b/tests/helper.tests.js new file mode 100644 index 0000000..c23be4e --- /dev/null +++ b/tests/helper.tests.js @@ -0,0 +1,66 @@ +'use strict'; + +const assert = require('assert'); +const { + _snakeCase, + _daraPathSegments, + _daraModuleStem, + _assertPythonModule, + _isSnakeCase, +} = require('../lib/helper'); + +describe('Python module path helpers', function () { + it('_snakeCase maps websocketUtils to websocket_utils', function () { + assert.strictEqual(_snakeCase('websocketUtils'), 'websocket_utils'); + assert.strictEqual(_snakeCase('WebSocketUtils'), 'web_socket_utils'); + }); + + it('_daraPathSegments splits POSIX and Windows separators', function () { + assert.deepStrictEqual(_daraPathSegments(), []); + assert.deepStrictEqual(_daraPathSegments(''), []); + assert.deepStrictEqual( + _daraPathSegments('./websocketUtils.dara'), + ['.', 'websocketUtils.dara'] + ); + assert.deepStrictEqual( + _daraPathSegments('.\\websocketUtils.dara'), + ['.', 'websocketUtils.dara'] + ); + assert.deepStrictEqual( + _daraPathSegments('foo/bar/websocketUtils.dara'), + ['foo', 'bar', 'websocketUtils.dara'] + ); + }); + + it('_daraModuleStem strips dara/tea/spec suffix from any separator', function () { + assert.strictEqual(_daraModuleStem(), ''); + assert.strictEqual(_daraModuleStem('/'), ''); + assert.strictEqual(_daraModuleStem('./websocketUtils.dara'), 'websocketUtils'); + assert.strictEqual(_daraModuleStem('.\\websocketUtils.dara'), 'websocketUtils'); + assert.strictEqual(_daraModuleStem('websocketUtils.tea'), 'websocketUtils'); + assert.strictEqual(_daraModuleStem('websocketUtils.spec'), 'websocketUtils'); + assert.strictEqual(_daraModuleStem('dir/websocketUtils.DARA'), 'websocketUtils'); + assert.strictEqual(_daraModuleStem('plain'), 'plain'); + }); + + it('exported dara filename becomes a snake_case Python module', function () { + const filename = _snakeCase(_daraModuleStem('./websocketUtils.dara')); + assert.strictEqual(filename, 'websocket_utils'); + assert.strictEqual(`${filename}_models`, 'websocket_utils_models'); + assert.ok(_isSnakeCase(filename)); + }); + + it('_assertPythonModule accepts snake_case and rejects camelCase', function () { + assert.strictEqual(_assertPythonModule('', 'empty'), ''); + assert.strictEqual( + _assertPythonModule('websocket_utils', 'export WebSocketUtils'), + 'websocket_utils' + ); + assert.throws(() => { + _assertPythonModule('websocketUtils', 'export WebSocketUtils'); + }, /snake_case/); + assert.throws(() => { + _assertPythonModule('1abc', 'export'); + }, /snake_case/); + }); +}); diff --git a/tests/main.tests.js b/tests/main.tests.js index 62a2aea..cc39841 100644 --- a/tests/main.tests.js +++ b/tests/main.tests.js @@ -349,5 +349,13 @@ describe('Python Generator', function () { clientName: 'client' } }); + const clientSrc = fs.readFileSync( + path.join(outputDir, 'websocket', 'tea_python_tests', 'client.py'), + 'utf8' + ); + assert.ok(clientSrc.includes('alibabacloud_tea_openapi.websocket_utils')); + assert.ok(clientSrc.includes('websocket_utils_models')); + assert.ok(!clientSrc.includes('websocketUtils')); + assert.ok(!clientSrc.includes('websocketUtils_models')); }); }); \ No newline at end of file