Skip to content
This repository was archived by the owner on Dec 6, 2023. It is now read-only.

Commit 5061b34

Browse files
committed
Initial push
1 parent 47f3932 commit 5061b34

File tree

10 files changed

+286
-0
lines changed

10 files changed

+286
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
.DS_Store
3+
Thumbs.db

.prettierignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
yarn.lock

.prettierrc.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"$schema": "https://json.schemastore.org/prettierrc",
3+
"semi": true,
4+
"singleQuote": true,
5+
"useTabs": true
6+
}

.vscode/settings.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"editor.formatOnSave": true,
3+
"editor.insertSpaces": false,
4+
"editor.formatOnPaste": true,
5+
"editor.tabSize": 2,
6+
"editor.detectIndentation": false,
7+
"[typescript]": {
8+
"editor.defaultFormatter": "esbenp.prettier-vscode"
9+
},
10+
"[md]": {
11+
"editor.defaultFormatter": "esbenp.prettier-vscode"
12+
}
13+
}

README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# tstl-nullify-function
2+
3+
TypeScriptToLua plugin that adds a statement to change local functions to `nil` if you annotate them with `@nullify`. This is only useful if you are using [another plugin](https://github.com/Cheatoid/TSTL-extensions/tree/main) to inline the function, making the original function declaration redundant.
4+
5+
## Example
6+
7+
```ts
8+
/**
9+
* @nullify
10+
*/
11+
function foo() {
12+
...
13+
}
14+
```
15+
16+
Becomes:
17+
18+
```lua
19+
local function foo
20+
...
21+
end
22+
foo = nil
23+
```
24+
25+
## Installation
26+
27+
1. Install this plugin
28+
29+
```bash
30+
yarn add git+https://git@github.com/thinknathan/tstl-nullify-function.git#^1.0.0 -D
31+
# or
32+
npm install git+https://git@github.com/thinknathan/tstl-nullify-function.git#^1.0.0 --save-dev
33+
```
34+
35+
2. Add `tstl-nullify-function` to `tstl.luaPlugins` in `tsconfig.json`
36+
37+
```diff
38+
{
39+
"tstl": {
40+
"luaPlugins": [
41+
+ { "name": "tstl-nullify-function" }
42+
],
43+
}
44+
}
45+
```
46+
47+
## License
48+
49+
CC0

dist/tstl-nullify-function.cjs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict';
2+
Object.defineProperty(exports, '__esModule', { value: true });
3+
const ts = require('typescript');
4+
const tstl = require('typescript-to-lua');
5+
const inlineComment = '@nullify';
6+
const plugin = {
7+
visitors: {
8+
[ts.SyntaxKind.FunctionDeclaration](node, context) {
9+
const result = context.superTransformStatements(node);
10+
// Check if the function has the comment
11+
const comments = ts.getLeadingCommentRanges(
12+
context.sourceFile.text,
13+
node.pos,
14+
);
15+
if (comments) {
16+
const comment = comments.find((comment) =>
17+
context.sourceFile.text
18+
.substring(comment.pos, comment.end)
19+
.includes(inlineComment),
20+
);
21+
if (comment) {
22+
// Add a new statement after the function that replaces it with nil
23+
const replacementStatement = tstl.createAssignmentStatement(
24+
tstl.createIdentifier(node.name.getText()),
25+
tstl.createNilLiteral(),
26+
);
27+
result.push(replacementStatement);
28+
}
29+
}
30+
return result;
31+
},
32+
},
33+
};
34+
exports.default = plugin;

package.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "tstl-nullify-function",
3+
"version": "1.0.0",
4+
"description": "TypeScriptToLua plugin that sets annotated local functions to nil",
5+
"repository": {
6+
"type": "git",
7+
"url": "https://github.com/thinknathan/tstl-nullify-function.git"
8+
},
9+
"author": "Nathan Bolton (https://thinknathan.ca/)",
10+
"license": "CC0-1.0",
11+
"main": "dist/tstl-nullify-function.cjs",
12+
"type": "commonjs",
13+
"scripts": {
14+
"build": "tsc",
15+
"prettier": "prettier --write ./"
16+
},
17+
"devDependencies": {
18+
"prettier": "^3.1.0",
19+
"tsc": "^2.0.4",
20+
"typescript": "~5.2.2",
21+
"typescript-to-lua": "~1.21.0"
22+
}
23+
}

tsconfig.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/TypeScriptToLua/TypeScriptToLua/master/tsconfig-schema.json",
3+
"compilerOptions": {
4+
"lib": ["esnext"],
5+
"module": "CommonJS",
6+
"outDir": "./dist",
7+
"rootDir": ".",
8+
"sourceMap": false,
9+
"strict": true,
10+
"target": "ES2020",
11+
"typeRoots": [
12+
"./@types",
13+
"./node_modules/@types",
14+
"./node_modules/@typescript-to-lua"
15+
]
16+
},
17+
"exclude": ["./node_modules/*", "./dist/*"]
18+
}

tstl-nullify-function.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import * as ts from 'typescript';
2+
import * as tstl from 'typescript-to-lua';
3+
4+
const inlineComment = '@nullify';
5+
6+
const plugin: tstl.Plugin = {
7+
visitors: {
8+
[ts.SyntaxKind.FunctionDeclaration](node, context) {
9+
const result = context.superTransformStatements(node);
10+
11+
// Check if the function has the comment
12+
const comments = ts.getLeadingCommentRanges(
13+
context.sourceFile.text,
14+
node.pos,
15+
);
16+
if (comments) {
17+
const comment = comments.find((comment) =>
18+
context.sourceFile.text
19+
.substring(comment.pos, comment.end)
20+
.includes(inlineComment),
21+
);
22+
23+
if (comment) {
24+
// Add a new statement after the function that replaces it with nil
25+
const replacementStatement = tstl.createAssignmentStatement(
26+
tstl.createIdentifier(node.name!.getText()),
27+
tstl.createNilLiteral(),
28+
);
29+
result.push(replacementStatement);
30+
}
31+
}
32+
33+
return result;
34+
},
35+
},
36+
};
37+
38+
export default plugin;

yarn.lock

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2+
# yarn lockfile v1
3+
4+
5+
"@typescript-to-lua/language-extensions@1.19.0":
6+
version "1.19.0"
7+
resolved "https://registry.yarnpkg.com/@typescript-to-lua/language-extensions/-/language-extensions-1.19.0.tgz#e7535ed343bed68580dd96e1a8101a1dbb2dc771"
8+
integrity sha512-Os5wOKwviTD4LeqI29N0btYOjokSJ97iCf45EOjIABlb5IwNQy7AE/AqZJobRw3ywHH8+KzJUMkEirWPzh2tUA==
9+
10+
enhanced-resolve@^5.8.2:
11+
version "5.15.0"
12+
resolved "https://registry.yarnpkg.com/enhanced-resolve/-/enhanced-resolve-5.15.0.tgz#1af946c7d93603eb88e9896cee4904dc012e9c35"
13+
integrity sha512-LXYT42KJ7lpIKECr2mAXIaMldcNCh/7E0KBKOu4KSfkHmP+mZmSs+8V5gBAqisWBy0OO4W5Oyys0GO1Y8KtdKg==
14+
dependencies:
15+
graceful-fs "^4.2.4"
16+
tapable "^2.2.0"
17+
18+
function-bind@^1.1.2:
19+
version "1.1.2"
20+
resolved "https://registry.yarnpkg.com/function-bind/-/function-bind-1.1.2.tgz#2c02d864d97f3ea6c8830c464cbd11ab6eab7a1c"
21+
integrity sha512-7XHNxH7qX9xG5mIwxkhumTox/MIRNcOgDrxWsMt2pAr23WHp6MrRlN7FBSFpCpr+oVO0F744iUgR82nJMfG2SA==
22+
23+
graceful-fs@^4.2.4:
24+
version "4.2.11"
25+
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.11.tgz#4183e4e8bf08bb6e05bbb2f7d2e0c8f712ca40e3"
26+
integrity sha512-RbJ5/jmFcNNCcDV5o9eTnBLJ/HszWV0P73bc+Ff4nS/rJj+YaS6IGyiOL0VoBYX+l1Wrl3k63h/KrH+nhJ0XvQ==
27+
28+
hasown@^2.0.0:
29+
version "2.0.0"
30+
resolved "https://registry.yarnpkg.com/hasown/-/hasown-2.0.0.tgz#f4c513d454a57b7c7e1650778de226b11700546c"
31+
integrity sha512-vUptKVTpIJhcczKBbgnS+RtcuYMB8+oNzPK2/Hp3hanz8JmpATdmmgLgSaadVREkDm+e2giHwY3ZRkyjSIDDFA==
32+
dependencies:
33+
function-bind "^1.1.2"
34+
35+
is-core-module@^2.13.0:
36+
version "2.13.1"
37+
resolved "https://registry.yarnpkg.com/is-core-module/-/is-core-module-2.13.1.tgz#ad0d7532c6fea9da1ebdc82742d74525c6273384"
38+
integrity sha512-hHrIjvZsftOsvKSn2TRYl63zvxsgE0K+0mYMoH6gD4omR5IWB2KynivBQczo3+wF1cCkjzvptnI9Q0sPU66ilw==
39+
dependencies:
40+
hasown "^2.0.0"
41+
42+
path-parse@^1.0.7:
43+
version "1.0.7"
44+
resolved "https://registry.yarnpkg.com/path-parse/-/path-parse-1.0.7.tgz#fbc114b60ca42b30d9daf5858e4bd68bbedb6735"
45+
integrity sha512-LDJzPVEEEPR+y48z93A0Ed0yXb8pAByGWo/k5YYdYgpY2/2EsOsksJrq7lOHxryrVOn1ejG6oAp8ahvOIQD8sw==
46+
47+
picomatch@^2.3.1:
48+
version "2.3.1"
49+
resolved "https://registry.yarnpkg.com/picomatch/-/picomatch-2.3.1.tgz#3ba3833733646d9d3e4995946c1365a67fb07a42"
50+
integrity sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA==
51+
52+
prettier@^3.1.0:
53+
version "3.1.0"
54+
resolved "https://registry.yarnpkg.com/prettier/-/prettier-3.1.0.tgz#c6d16474a5f764ea1a4a373c593b779697744d5e"
55+
integrity sha512-TQLvXjq5IAibjh8EpBIkNKxO749UEWABoiIZehEPiY4GNpVdhaFKqSTu+QrlU6D2dPAfubRmtJTi4K4YkQ5eXw==
56+
57+
resolve@^1.15.1:
58+
version "1.22.8"
59+
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.22.8.tgz#b6c87a9f2aa06dfab52e3d70ac8cde321fa5a48d"
60+
integrity sha512-oKWePCxqpd6FlLvGV1VU0x7bkPmmCNolxzjMf4NczoDnQcIWrAF+cPtZn5i6n+RfD2d9i0tzpKnG6Yk168yIyw==
61+
dependencies:
62+
is-core-module "^2.13.0"
63+
path-parse "^1.0.7"
64+
supports-preserve-symlinks-flag "^1.0.0"
65+
66+
source-map@^0.7.3:
67+
version "0.7.4"
68+
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.7.4.tgz#a9bbe705c9d8846f4e08ff6765acf0f1b0898656"
69+
integrity sha512-l3BikUxvPOcn5E74dZiq5BGsTb5yEwhaTSzccU6t4sDOH8NWJCstKO5QT2CvtFoK6F0saL7p9xHAqHOlCPJygA==
70+
71+
supports-preserve-symlinks-flag@^1.0.0:
72+
version "1.0.0"
73+
resolved "https://registry.yarnpkg.com/supports-preserve-symlinks-flag/-/supports-preserve-symlinks-flag-1.0.0.tgz#6eda4bd344a3c94aea376d4cc31bc77311039e09"
74+
integrity sha512-ot0WnXS9fgdkgIcePe6RHNk1WA8+muPa6cSjeR3V8K27q9BB1rTE3R1p7Hv0z1ZyAc8s6Vvv8DIyWf681MAt0w==
75+
76+
tapable@^2.2.0:
77+
version "2.2.1"
78+
resolved "https://registry.yarnpkg.com/tapable/-/tapable-2.2.1.tgz#1967a73ef4060a82f12ab96af86d52fdb76eeca0"
79+
integrity sha512-GNzQvQTOIP6RyTfE2Qxb8ZVlNmw0n88vp1szwWRimP02mnTsx3Wtn5qRdqY9w2XduFNUgvOwhNnQsjwCp+kqaQ==
80+
81+
tsc@^2.0.4:
82+
version "2.0.4"
83+
resolved "https://registry.yarnpkg.com/tsc/-/tsc-2.0.4.tgz#5f6499146abea5dca4420b451fa4f2f9345238f5"
84+
integrity sha512-fzoSieZI5KKJVBYGvwbVZs/J5za84f2lSTLPYf6AGiIf43tZ3GNrI1QzTLcjtyDDP4aLxd46RTZq1nQxe7+k5Q==
85+
86+
typescript-to-lua@~1.21.0:
87+
version "1.21.0"
88+
resolved "https://registry.yarnpkg.com/typescript-to-lua/-/typescript-to-lua-1.21.0.tgz#09534b5633d60abed0690677ad846c5ba87b55f5"
89+
integrity sha512-xvr7bxeQ3uTIs5yU1IocB5wIgnNzpkELKHu7Y1Die7+xzrk87/NGU0I+cQX6i1t64Ml/1O6rVi23Yc9jRkD0Rw==
90+
dependencies:
91+
"@typescript-to-lua/language-extensions" "1.19.0"
92+
enhanced-resolve "^5.8.2"
93+
picomatch "^2.3.1"
94+
resolve "^1.15.1"
95+
source-map "^0.7.3"
96+
97+
typescript@~5.2.2:
98+
version "5.2.2"
99+
resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.2.2.tgz#5ebb5e5a5b75f085f22bc3f8460fba308310fa78"
100+
integrity sha512-mI4WrpHsbCIcwT9cF4FZvr80QUeKvsUsUvKDoR+X/7XHQH98xYD8YHZg7ANtz2GtZt/CBq2QJ0thkGJMHfqc1w==

0 commit comments

Comments
 (0)