-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: refactor non-constant property initializer for php, fixes #16
- Loading branch information
Showing
10 changed files
with
133 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/target-php/transformers/refactor-member-initializer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { ClassDeclaration, TypeGuards, Expression, PropertyDeclaration } from 'ts-morph' | ||
|
||
export function refactorMemberInitializer (clazz: ClassDeclaration, prop: PropertyDeclaration) { | ||
const initializer = prop.getInitializer() | ||
if (!initializer || isConstant(initializer)) return | ||
|
||
if (prop.isStatic()) { | ||
const statement = clazz.getName() + '.' + prop.getName() + ' = ' + initializer.getFullText() | ||
clazz.getSourceFile().addStatements(statement) | ||
} else { | ||
let statement = 'this.' + prop.getName() + ' = ' + initializer.getFullText() | ||
if (!clazz.getConstructors().length) { | ||
clazz.addConstructor() | ||
statement = 'super()\n' + statement | ||
} | ||
const init = clazz.getConstructors()[0] | ||
init.setBodyText(init.getBodyText() + '\n' + statement) | ||
} | ||
prop.removeInitializer() | ||
} | ||
|
||
function isConstant (expr: Expression) { | ||
if (TypeGuards.isLiteralExpression(expr)) return true | ||
return false | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const reservedNames = [/^list$/i] | ||
|
||
export function isReserved (name: string) { | ||
for (const reserved of reservedNames) { | ||
if (reserved.test(name)) return true | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Component } from 'san' | ||
|
||
const TEMPLATE = '<div><b title="{{title | real}}">{{title | real}}</b></div>' | ||
|
||
export default class MyComponent extends Component { | ||
static template = TEMPLATE | ||
static filters = { | ||
real: function (x) { | ||
return 'real' + x | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"title": "1" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<div><!--s-data:{"title":"1"}--><b title="real1">real1</b></div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
it('method', function () { | ||
// [inject] init | ||
|
||
const b = wrap.getElementsByTagName('b')[0] | ||
|
||
expect(b.title).toBe('real1') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { refactorMemberInitializer } from '../../../src/target-php/transformers/refactor-member-initializer' | ||
import { Project } from 'ts-morph' | ||
|
||
describe('refactorMemberInitializer()', function () { | ||
const project = new Project() | ||
|
||
it('should skip string literal static initializer', function () { | ||
const sourceFile = project.createSourceFile('/tmp/static-str-literal', ` | ||
class Foo { | ||
static staticLiteralStr: string = "foo" | ||
}`) | ||
const clazz = sourceFile.getClass('Foo') | ||
for (const prop of clazz.getProperties()) { | ||
refactorMemberInitializer(clazz, prop) | ||
} | ||
sourceFile.formatText() | ||
const text = sourceFile.getFullText() | ||
|
||
expect(text).toContain(`static staticLiteralStr: string = "foo"`) | ||
expect(text).not.toContain(`Foo.staticLiteralStr =`) | ||
}) | ||
|
||
it('should refactor string variable static initializer', function () { | ||
const sourceFile = project.createSourceFile('/tmp/static-str-var', ` | ||
const str = "foo" | ||
class Foo { | ||
static staticLiteralStr: string = str | ||
}`) | ||
const clazz = sourceFile.getClass('Foo') | ||
for (const prop of clazz.getProperties()) { | ||
refactorMemberInitializer(clazz, prop) | ||
} | ||
sourceFile.formatText() | ||
const text = sourceFile.getFullText() | ||
|
||
expect(text).toContain(`static staticLiteralStr: string`) | ||
expect(text).toContain(`Foo.staticLiteralStr = str`) | ||
}) | ||
|
||
it('should skip string literal initializer', function () { | ||
const sourceFile = project.createSourceFile('/tmp/str-literal', ` | ||
class Foo { | ||
literalStr: string = "str" | ||
}`) | ||
const clazz = sourceFile.getClass('Foo') | ||
for (const prop of clazz.getProperties()) { | ||
refactorMemberInitializer(clazz, prop) | ||
} | ||
const text = sourceFile.getFullText() | ||
|
||
expect(text).toContain(`literalStr: string = "str"`) | ||
expect(text).not.toContain(`constructor()`) | ||
}) | ||
|
||
it('should refactor string literal initializer', function () { | ||
const sourceFile = project.createSourceFile('/tmp/str-var', ` | ||
const str = "foo" | ||
class Foo { | ||
literalStr: string = str | ||
}`) | ||
const clazz = sourceFile.getClass('Foo') | ||
for (const prop of clazz.getProperties()) { | ||
refactorMemberInitializer(clazz, prop) | ||
} | ||
sourceFile.formatText() | ||
const text = sourceFile.getFullText() | ||
|
||
expect(text).toContain(`literalStr: string\n`) | ||
expect(text).toContain(`constructor() {`) | ||
expect(text).toContain(`super()`) | ||
expect(text).toContain(`this.literalStr = str`) | ||
}) | ||
}) |