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 2 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",
"@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
10 changes: 10 additions & 0 deletions packages/eslint-plugin-local-rules/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const { name, version } = require("./package.json");
module.exports = {
meta: {
name: name,
version: version,
},
rules: {
"no-export-symbol": require("./rules/no-export-symbol"),
},
};
6 changes: 6 additions & 0 deletions packages/eslint-plugin-local-rules/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A scary experiment, but we have #1045 and have to start somewhere...

"name": "@tact-lang/eslint-plugin-local-rules",
"version": "0.0.0",
"private": true,
"main": "index.js"
}
39 changes: 39 additions & 0 deletions packages/eslint-plugin-local-rules/rules/no-export-symbol.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const ts = require("typescript");

module.exports = {
meta: {
type: "problem",
docs: {
url: "https://github.com/tact-lang/tact/issues/1695",
recommended: true,
},
},

create(context) {
const parserServices = context.sourceCode.parserServices;
const checker = parserServices.program.getTypeChecker();

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

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

const name = context.sourceCode.getText(node);
context.report({
node,
message: "Forbidden to export the '{{name}}' symbol",
data: { name },
});
},
};

function isSymbolType(type) {
const flags = type.getFlags();
return flags === ts.TypeFlags.UniqueESSymbol;
}
},
};
3 changes: 3 additions & 0 deletions src/utils/example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const exampleSymbol = Symbol("example");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The primary reason I postponed doing this is that I remember there was an official template for eslint rule definitions, and I couldn't find it.

It required positive tests, negative tests and documentation, and also rules were implemented in TypeScript.

It's nice to have this error message in CI, but
a) we'll have to remove it
b) I also wonder whether it works with export const, and can't easily see it in tests

Finding/making the rule template is considerably more important :/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. updated to match the template
  2. added tests


export { exampleSymbol }; // error: Forbidden to export the 'exampleSymbol' symbol

Check failure on line 3 in src/utils/example.ts

View workflow job for this annotation

GitHub Actions / linter

Forbidden to export the 'exampleSymbol' symbol
3 changes: 3 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1284,6 +1284,9 @@
teslabot "^1.5.0"
zod "^3.20.2"

"@tact-lang/eslint-plugin-local-rules@file:packages/eslint-plugin-local-rules":
version "0.0.0"

"@tact-lang/opcode@^0.0.14":
version "0.0.14"
resolved "https://registry.npmjs.org/@tact-lang/opcode/-/opcode-0.0.14.tgz"
Expand Down
Loading