Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implemented a rule to forbid the export of symbols #1729

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = {
project: "./tsconfig.eslint.json",
},
ignorePatterns: ["*.cjs", "*.js"],
plugins: ["@typescript-eslint"],
plugins: ["@typescript-eslint", "@tact-lang/local-rules"],
root: true,
env: {
node: true,
Expand Down Expand Up @@ -93,5 +93,6 @@ module.exports = {
"require-await": "off",
"@typescript-eslint/require-await": "error",
"@typescript-eslint/unified-signatures": "error",
"@tact-lang/local-rules/no-export-symbol": "error",
},
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
"@swc/core": "^1.10.7",
"@swc/jest": "^0.2.37",
"@tact-lang/coverage": "^0.0.8",
"@tact-lang/eslint-plugin-local-rules": "file:packages/eslint-plugin-local-rules/lib",
"@tact-lang/ton-abi": "^0.0.3",
"@tact-lang/ton-jest": "^0.0.4",
"@ton/sandbox": "^0.24.0",
Expand All @@ -94,8 +95,8 @@
"@types/node": "^22.5.0",
"@typescript-eslint/eslint-plugin": "^8.21.0",
"@typescript-eslint/parser": "^8.21.0",
"cli-table3": "^0.6.5",
"chalk": "4.1.2",
"cli-table3": "^0.6.5",
"cross-env": "^7.0.3",
"cspell": "^8.8.3",
"diff": "^7.0.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Ban exporting witness Symbol (`@tact-lang/local-rules/no-export-symbol`)

<!-- end auto-generated rule header -->

Please describe the origin of the rule here.

## Rule Details

This rule aims to...

Examples of **incorrect** code for this rule:

```ts
const exampleSymbol = Symbol("example");
export {exampleSymbol};
```

Examples of **correct** code for this rule:

```ts
const exampleSymbol = Symbol("example");
```

## When Not To Use It

Give a short description of when it would be appropriate to turn off this rule.

## Further Reading

If there are other links that describe the issue this rule addresses, please include them here in a bulleted list.
7 changes: 7 additions & 0 deletions packages/eslint-plugin-local-rules/lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"use strict";

module.exports = {
rules: {
"no-export-symbol": require("./rules/no-export-symbol"),
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"use strict";
const ts = require("typescript");
const { ESLintUtils } = require("@typescript-eslint/utils");

/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: "problem",
docs: {
description: "Ban exporting witness Symbol",
recommended: true,
url: "https://github.com/tact-lang/tact/issues/1695",
},
messages: {
forbiddenExportSymbol: "Forbidden to export the '{{name}}' symbol",
},
fixable: null,
schema: [],
},

create(context) {
const { esTreeNodeToTSNodeMap, program } =
ESLintUtils.getParserServices(context);
const checker = program.getTypeChecker();

return {
ExportSpecifier(node) {
const tsNode = esTreeNodeToTSNodeMap.get(node);
const type = checker.getTypeAtLocation(tsNode);

if (!isSymbolType(type)) {
return;
}

const name = context.sourceCode.getText(node);
context.report({
node,
messageId: "forbiddenExportSymbol",
data: { name },
});
},
};

function isSymbolType(type) {
const flags = type.getFlags();
return flags === ts.TypeFlags.UniqueESSymbol;
}
},
};
Loading
Loading