Skip to content

Commit 514bdf1

Browse files
committed
fix: creating ast transformer
1 parent 471becb commit 514bdf1

File tree

12 files changed

+480
-197
lines changed

12 files changed

+480
-197
lines changed

.vscode/settings.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"editor.codeActionsOnSave": {
3-
"source.fixAll": true,
4-
"tslint.autoFixOnSave": true
3+
"source.fixAll": "explicit",
4+
"tslint.autoFixOnSave": "explicit"
55
},
66
"editor.formatOnSave": true
77
}

README.md

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,59 @@
77

88
# extension-methods
99

10-
With this library, you can create 'proxy references' for your objects and access many methods that actually doesn't exists in them!
10+
This library brings to Typescript a really useful functionality that other languages already have, like C# or Kotlin: extension methods!
11+
12+
Extension methods are a way to declare static methods that are aeasily referenced in specific types through the dot notation, like if they belong to a class itself! Let me give you a example:
13+
14+
```typescript
15+
class StringExtensions {
16+
@ExtensionMethod
17+
static size(str: string) {
18+
return str.length;
19+
}
20+
}
21+
22+
interface String {
23+
Size(): number;
24+
}
25+
26+
'my-string'.size();
27+
```
28+
29+
Easy enough, right? If you already know this feature, I'm sure you miss it in Javascript/Typescript, but with this library no-more!
30+
31+
Also, this library works with two modes:
32+
33+
- AST Transformer
34+
- Proxy reference
35+
36+
Each of them are explained below and you can use the one that fits you better!
37+
38+
# AST Transformer mode
39+
40+
In this mode, everything is done at transpiling time, making you able to access each of the extensions methods just by importing them where you need, like this:
41+
42+
```typescript
43+
import './string-extensions';
44+
45+
console.log('my-strign'.size());
46+
```
47+
48+
Due to typescript limitation, not only you have to declare the extension method, but also the additional methods to merge into the type you want to add them, like the example more above. Now, to apply this transformation, you need to use a compiler command that supports adding ast transformers to the pipe. We suggest you to use **nest build.**
49+
50+
You'll have to add the following plugin to your **nest-cli.json**:
51+
52+
```typescript
53+
"plugins": [
54+
"extension-methods/plugin"
55+
],
56+
```
57+
58+
That's it! this is enough to get the extensions you want!
59+
60+
# Proxy reference mode
61+
62+
In this mode, you can create 'proxy references' for your objects and access many methods that actually doesn't exists in them!
1163
This is pretty useful in the following scenarios:
1264

1365
- where you need to create a method that returns an object with a lot of methods, but the code that'll use that result will only access a few of them;
@@ -16,8 +68,6 @@ This is pretty useful in the following scenarios:
1668
Depending on the number of methods you'll proxy through **extension-methods**, you can achieve a 99% faster operation than a simple **new Class()**
1769
<br>
1870

19-
## How to use it
20-
2171
First, you need to obtain your extension object, like this:
2272
<br>
2373

@@ -116,5 +166,5 @@ And that's it, it'll just work!
116166
## Important
117167
118168
- If some method exists in the original object and also is declared in the Extender, the original method will be used;
119-
- **extension-methods** can't be used with primitive values like **string**, **number** and **boolean**;
169+
- **extension-methods** can't be used in proxy mode with primitive values like **string**, **number** and **boolean**, but it'll work with the ast mode!
120170
- **extend** will naturally returns a type that is a join between the real object and the extension methods declared, but it is recommendable, if you want a cleaner type or to return such value as a result of a function, to create an interface that represent it, as you can see in the examples above;

package-lock.json

Lines changed: 110 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"description": "This library allows you to create extension methods for a given object the fatest way",
55
"main": "dist/index.js",
66
"files": [
7-
"dist"
7+
"dist",
8+
"plugin.js"
89
],
910
"scripts": {
1011
"test": "mocha",
@@ -32,12 +33,17 @@
3233
"extension",
3334
"methods",
3435
"extended",
35-
"static extension methods"
36+
"static extension methods",
37+
"ast",
38+
"transformer"
3639
],
3740
"author": "Thiago O Santos <tos.oliveira@gmail.com>",
3841
"license": "MIT",
3942
"devDependencies": {
40-
"@codibre/confs": "0.0.6"
43+
"@codibre/confs": "0.0.6",
44+
"@types/node": "^22.10.7",
45+
"prettier": "^3.4.2",
46+
"typescript": "^5.7.3"
4147
},
4248
"directories": {
4349
"test": "test"
@@ -50,4 +56,4 @@
5056
"url": "https://github.com/Codibre/extension-methods/issues"
5157
},
5258
"homepage": "https://github.com/Codibre/extension-methods#readme"
53-
}
59+
}

plugin.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module.exports = require('./dist/plugin/index');
2+
module.exports.default = module.exports;

0 commit comments

Comments
 (0)