ether-code 0.9.5 → 0.9.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cli/ether.js +1 -1
- package/generators/php-generator.js +51 -11
- package/package.json +1 -1
package/cli/ether.js
CHANGED
|
@@ -325,15 +325,30 @@ class PHPGenerator {
|
|
|
325
325
|
|
|
326
326
|
extractTypeName(typeNode) {
|
|
327
327
|
if (!typeNode) return ''
|
|
328
|
-
if (typeof typeNode === 'string')
|
|
328
|
+
if (typeof typeNode === 'string') {
|
|
329
|
+
const translated = this.translateKeyword(typeNode)
|
|
330
|
+
return this.prefixGlobalClass(translated)
|
|
331
|
+
}
|
|
329
332
|
if (typeNode.type === 'TypeHint') {
|
|
330
|
-
const types = (typeNode.types || []).map(t =>
|
|
333
|
+
const types = (typeNode.types || []).map(t => {
|
|
334
|
+
const translated = this.translateKeyword(t)
|
|
335
|
+
return this.prefixGlobalClass(translated)
|
|
336
|
+
})
|
|
331
337
|
const result = types.join('|')
|
|
332
338
|
return typeNode.nullable ? '?' + result : result
|
|
333
339
|
}
|
|
334
|
-
if (typeNode.name)
|
|
335
|
-
|
|
336
|
-
|
|
340
|
+
if (typeNode.name) {
|
|
341
|
+
const translated = this.translateKeyword(typeNode.name)
|
|
342
|
+
return this.prefixGlobalClass(translated)
|
|
343
|
+
}
|
|
344
|
+
if (typeNode.type === 'Identifier') {
|
|
345
|
+
const translated = this.translateKeyword(typeNode.name || typeNode.value || '')
|
|
346
|
+
return this.prefixGlobalClass(translated)
|
|
347
|
+
}
|
|
348
|
+
if (typeNode.value) {
|
|
349
|
+
const translated = this.translateKeyword(typeNode.value)
|
|
350
|
+
return this.prefixGlobalClass(translated)
|
|
351
|
+
}
|
|
337
352
|
return ''
|
|
338
353
|
}
|
|
339
354
|
|
|
@@ -664,7 +679,7 @@ class PHPGenerator {
|
|
|
664
679
|
generateClassMember(node) {
|
|
665
680
|
if (!node) return
|
|
666
681
|
|
|
667
|
-
if (node.type === 'UseTraitStatement') {
|
|
682
|
+
if (node.type === 'UseTraitStatement' || node.type === 'TraitUse') {
|
|
668
683
|
this.writeLine(`use ${node.traits.join(', ')};`)
|
|
669
684
|
return
|
|
670
685
|
}
|
|
@@ -923,9 +938,9 @@ class PHPGenerator {
|
|
|
923
938
|
const catches = node.catches || (node.handler ? [node.handler] : [])
|
|
924
939
|
for (const catchClause of catches) {
|
|
925
940
|
const types = catchClause.types || []
|
|
926
|
-
|
|
927
|
-
? types.join('|')
|
|
928
|
-
: (catchClause.exceptionType || catchClause.type || 'Exception')
|
|
941
|
+
let exceptionType = types.length > 0
|
|
942
|
+
? types.map(t => this.prefixGlobalClass(t)).join('|')
|
|
943
|
+
: this.prefixGlobalClass(catchClause.exceptionType || catchClause.type || 'Exception')
|
|
929
944
|
const param = catchClause.param
|
|
930
945
|
? this.generateVariable(catchClause.param)
|
|
931
946
|
: '$e'
|
|
@@ -947,6 +962,31 @@ class PHPGenerator {
|
|
|
947
962
|
return ''
|
|
948
963
|
}
|
|
949
964
|
|
|
965
|
+
prefixGlobalClass(name) {
|
|
966
|
+
const phpGlobalClasses = [
|
|
967
|
+
'Exception', 'Error', 'TypeError', 'ValueError', 'ArgumentCountError',
|
|
968
|
+
'ArithmeticError', 'DivisionByZeroError', 'ParseError', 'AssertionError',
|
|
969
|
+
'CompileError', 'UnhandledMatchError', 'FiberError',
|
|
970
|
+
'ErrorException', 'BadFunctionCallException', 'BadMethodCallException',
|
|
971
|
+
'DomainException', 'InvalidArgumentException', 'LengthException',
|
|
972
|
+
'LogicException', 'OutOfBoundsException', 'OutOfRangeException',
|
|
973
|
+
'OverflowException', 'RangeException', 'RuntimeException',
|
|
974
|
+
'UnderflowException', 'UnexpectedValueException',
|
|
975
|
+
'PDOException', 'JsonException', 'IntlException',
|
|
976
|
+
'Generator', 'Iterator', 'IteratorAggregate', 'Traversable',
|
|
977
|
+
'ArrayAccess', 'Countable', 'Serializable', 'Stringable',
|
|
978
|
+
'DateTime', 'DateTimeImmutable', 'DateTimeInterface', 'DateInterval', 'DatePeriod',
|
|
979
|
+
'PDO', 'PDOStatement', 'Closure', 'stdClass', 'WeakReference', 'WeakMap',
|
|
980
|
+
'SplFileInfo', 'SplFileObject', 'SplTempFileObject',
|
|
981
|
+
'ArrayObject', 'ArrayIterator', 'RecursiveArrayIterator',
|
|
982
|
+
'Fiber', 'ReflectionClass', 'ReflectionMethod', 'ReflectionProperty'
|
|
983
|
+
]
|
|
984
|
+
if (phpGlobalClasses.includes(name) && !name.startsWith('\\')) {
|
|
985
|
+
return '\\' + name
|
|
986
|
+
}
|
|
987
|
+
return name
|
|
988
|
+
}
|
|
989
|
+
|
|
950
990
|
generateReturnStatement(node) {
|
|
951
991
|
if (node.argument) {
|
|
952
992
|
const arg = this.generateNode(node.argument)
|
|
@@ -1241,9 +1281,9 @@ class PHPGenerator {
|
|
|
1241
1281
|
generateNewExpression(node) {
|
|
1242
1282
|
let callee
|
|
1243
1283
|
if (typeof node.callee === 'string') {
|
|
1244
|
-
callee = node.callee
|
|
1284
|
+
callee = this.prefixGlobalClass(node.callee)
|
|
1245
1285
|
} else if (node.callee.type === 'Identifier') {
|
|
1246
|
-
callee = node.callee.name
|
|
1286
|
+
callee = this.prefixGlobalClass(node.callee.name)
|
|
1247
1287
|
} else {
|
|
1248
1288
|
callee = this.generateNode(node.callee)
|
|
1249
1289
|
}
|