Skip to content

Commit cbac7f1

Browse files
committed
Added examples in readme
1 parent 3f70cba commit cbac7f1

File tree

1 file changed

+110
-3
lines changed

1 file changed

+110
-3
lines changed

README.md

Lines changed: 110 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,113 @@ I cannot guarantee you that the transformer covers all possible cases, but it ha
2121

2222
Also, it might not work as expected with composite projects, because the project should contain an entry points you set in options, but it might be in other sub-project.
2323

24+
## Example
25+
26+
Let's say we have the following source code in entry point:
27+
28+
```typescript
29+
// yeah, it's especially without the `export` keyword here
30+
interface Options {
31+
fooBar: number;
32+
}
33+
34+
interface InternalInterface {
35+
fooBar: number;
36+
}
37+
38+
export function getOptions(fooBar: number): Options {
39+
const result: Options = { fooBar };
40+
const internalOptions: InternalInterface = { fooBar };
41+
console.log(internalOptions.fooBar);
42+
return result;
43+
}
44+
```
45+
46+
After applying this transformer you'll get the next result:
47+
48+
```javascript
49+
"use strict";
50+
Object.defineProperty(exports, "__esModule", { value: true });
51+
function getOptions(fooBar) {
52+
var result = { fooBar: fooBar };
53+
var internalOptions = { _internal_fooBar: fooBar };
54+
console.log(internalOptions._internal_fooBar);
55+
return result;
56+
}
57+
exports.getOptions = getOptions;
58+
```
59+
60+
Even if both `Options` and `InternalInterface` have the same property `fooBar`, the only `InternalInterface`'s `fooBar` has been renamed into `_internal_fooBar`.
61+
That's done because this interface isn't exported from the entry point (and even isn't used in exports' types), so it isn't used anywhere outside and could be safely renamed (within all it's properties).
62+
63+
## Example 2
64+
65+
Let's see more tricky example with classes and interfaces. Let's say we have the following code:
66+
67+
```typescript
68+
export interface Interface {
69+
publicMethod(opts: Options, b: number): void;
70+
publicProperty: number;
71+
}
72+
73+
export interface Options {
74+
prop: number;
75+
}
76+
77+
class Class implements Interface {
78+
public publicProperty: number = 123;
79+
80+
public publicMethod(opts: Partial<Options>): void {
81+
console.log(opts.prop, this.publicProperty);
82+
this.anotherPublicMethod();
83+
}
84+
85+
public anotherPublicMethod(): void {}
86+
}
87+
88+
export function interfaceFactory(): Interface {
89+
return new Class();
90+
}
91+
```
92+
93+
Here we can a class `Class` which implements an interface `Interface` and a factory, which creates a class and returns it as `Interface`.
94+
95+
After processing you'll get the next result:
96+
97+
```javascript
98+
"use strict";
99+
Object.defineProperty(exports, "__esModule", { value: true });
100+
var Class = /** @class */ (function () {
101+
function Class() {
102+
this.publicProperty = 123;
103+
}
104+
Class.prototype.publicMethod = function (opts) {
105+
console.log(opts.prop, this.publicProperty);
106+
this._internal_anotherPublicMethod();
107+
};
108+
Class.prototype._internal_anotherPublicMethod = function () { };
109+
return Class;
110+
}());
111+
function interfaceFactory() {
112+
return new Class();
113+
}
114+
exports.interfaceFactory = interfaceFactory;
115+
```
116+
117+
Here we have some interesting results:
118+
119+
1. `publicProperty`, declared in `Interface` interface and implemented in `Class` class hasn't been renamed,
120+
because it's accessible from an object, returned from `interfaceFactory` function.
121+
122+
1. `publicMethod` hasn't been renamed as well the same as `publicProperty`.
123+
124+
1. `prop` from `Options` interface also hasn't been renamed as soon it's part of "public API" of the module.
125+
126+
1. `anotherPublicMethod` (and all calls) has been renamed into `_internal_anotherPublicMethod`,
127+
because it isn't declared in `Interface` and cannot be accessible from an object, returned from `interfaceFactory` publicly (TypeScript didn't even generate types for that!).
128+
129+
More examples you can see [in test-cases folder](https://github.com/timocov/ts-transformer-properties-rename/tree/master/tests/test-cases/).
130+
24131
## Installation
25132

26133
1. Install the package `npm i -D ts-transformer-properties-rename`
@@ -113,7 +220,7 @@ module.exports = {
113220
options: {
114221
getCustomTransformers: program => ({
115222
before: [
116-
propertiesRenameTransformer(program)
223+
propertiesRenameTransformer(program, { entrySourceFiles: ['./src/index.ts'] })
117224
]
118225
})
119226
}
@@ -135,7 +242,7 @@ export default {
135242
// ...
136243
plugins: [
137244
typescript({ transformers: [service => ({
138-
before: [ propertiesRenameTransformer(service.getProgram()) ],
245+
before: [ propertiesRenameTransformer(service.getProgram(), { entrySourceFiles: ['./src/index.ts'] }) ],
139246
after: []
140247
})] })
141248
]
@@ -153,7 +260,7 @@ See [ttypescript's README](https://github.com/cevek/ttypescript/blob/master/READ
153260
"compilerOptions": {
154261
// ...
155262
"plugins": [
156-
{ "transform": "ts-transformer-properties-rename" }
263+
{ "transform": "ts-transformer-properties-rename", "entrySourceFiles": ["./src/index.ts"] }
157264
]
158265
},
159266
// ...

0 commit comments

Comments
 (0)