Skip to content

Commit bd189bd

Browse files
committed
config tweaks
1 parent 1e1b586 commit bd189bd

File tree

6 files changed

+155
-165
lines changed

6 files changed

+155
-165
lines changed

.vscode/launch.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "node",
9+
"request": "launch",
10+
"name": "Launch Program",
11+
"skipFiles": ["<node_internals>/**", "**/node_modules/**"],
12+
"program": "${workspaceFolder}/dist/tests/perf-plugin.test.js",
13+
"outFiles": ["${workspaceFolder}/dist/**/*.js"]
14+
}
15+
]
16+
}

eslint.config.mjs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,31 @@
11
// Import necessary modules for flat config
2-
import tsParser from "@typescript-eslint/parser";
2+
import tsParser from '@typescript-eslint/parser';
33
// Import the local plugin using ES Module syntax
4-
import localPlugin from "./index.js"; // Assumes index.js is the entry point
4+
import localPlugin from './index.js'; // Assumes index.js is the entry point
55

66
const allPluginRules = {};
7-
const pluginName = "assemblyscript"; // Use the plugin name defined in bash
7+
const pluginName = 'assemblyscript'; // Use the plugin name defined in bash
88

99
// Access rules from the default export of the plugin module
1010
if (localPlugin && localPlugin.rules) {
1111
for (const ruleName in localPlugin.rules) {
1212
// Construct the full rule name: 'plugin-name/rule-name'
13-
allPluginRules[`${pluginName}/${ruleName}`] = "warn"; // Set default severity
13+
allPluginRules[`${pluginName}/${ruleName}`] = 'warn'; // Set default severity
1414
}
1515
} else {
16-
console.warn(
17-
`Plugin '${pluginName}' loaded from ./index.js does not seem to export rules correctly.`
18-
);
16+
console.warn(`Plugin '${pluginName}' loaded from ./index.js does not seem to export rules correctly.`);
1917
}
2018

2119
// Export the flat config array
2220
export default [
2321
{
2422
// Apply to TypeScript files in the target directory
25-
files: ["**/*.ts"],
23+
files: ["sample_cases/usecase-wasm-vehicle-status-25/source/**/*.ts"],
2624
languageOptions: {
2725
parser: tsParser,
2826
parserOptions: {
29-
ecmaVersion: "latest",
30-
sourceType: "module",
27+
ecmaVersion: 'latest',
28+
sourceType: 'module',
3129
},
3230
},
3331
// Define the plugin using the imported object
@@ -36,5 +34,5 @@ export default [
3634
},
3735
// Apply the dynamically generated rules
3836
rules: allPluginRules,
39-
},
37+
}
4038
];

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
"scripts": {
2929
"build": "npx tsc --build ./tsconfig.json",
3030
"test": "./test-lint.sh",
31-
"lint": "npx eslint test_case/hello.ts"
31+
"lint": "npx eslint sample_cases/",
32+
"watch": "npx tsc --build ./tsconfig.json --watch"
3233
},
3334
"peerDependencies": {
3435
"eslint": ">=8.0.0"

plugins/perf-plugin.ts

Lines changed: 8 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import {
22
AST_NODE_TYPES,
33
ESLintUtils,
44
TSESTree,
5-
TSESLint,
65
} from "@typescript-eslint/utils";
76

87
/**
@@ -98,7 +97,6 @@ const arrayInitStyle: ESLintUtils.RuleModule<
9897
* Limitations:
9998
* - Only static properties and indices are supported
10099
* - Dynamic properties (obj[variable]) are ignored
101-
* - Constants, enums, and imports are skipped
102100
*
103101
* Example:
104102
* // Bad - repeated access
@@ -125,7 +123,7 @@ const noRepeatedMemberAccess: ESLintUtils.RuleModule<
125123
ESLintUtils.RuleListener // Listener type
126124
> = createRule({
127125
name: "no-repeated-member-access",
128-
defaultOptions: [{ minOccurrences: 0 }], // Provide a default object matching the options structure
126+
defaultOptions: [{ minOccurrences: 2 }], // Provide a default object matching the options structure
129127
meta: {
130128
type: "suggestion",
131129
docs: {
@@ -147,9 +145,6 @@ const noRepeatedMemberAccess: ESLintUtils.RuleModule<
147145
],
148146
},
149147
create(context) {
150-
// Store nodes for each object chain in each scope for auto-fixing
151-
const chainNodesMap = new Map<string, TSESTree.MemberExpression[]>();
152-
153148
function getObjectChain(node: TSESTree.Node) {
154149
// node is the outermost MemberExpression, e.g. ctx.data.v1
155150
let current = node;
@@ -263,6 +258,10 @@ const noRepeatedMemberAccess: ESLintUtils.RuleModule<
263258
}
264259
return null;
265260
}
261+
262+
// Store nodes for each object chain in each scope for auto-fixing
263+
const chainNodesMap = new Map<string, TSESTree.MemberExpression[]>();
264+
266265
const occurrences = new Map();
267266
const minOccurrences = context.options[0]?.minOccurrences || 2;
268267

@@ -275,60 +274,13 @@ const noRepeatedMemberAccess: ESLintUtils.RuleModule<
275274
if (!objectChain) return;
276275

277276
const baseObjectName = objectChain.split(/[.[]/)[0];
277+
// no need to continue if what we extract is the same as the base object
278+
if (objectChain === baseObjectName) return;
279+
278280
// Use scope range as part of the key
279281
const scope = context.sourceCode.getScope(node);
280282
if (!scope || !scope.block || !scope.block.range) return;
281283

282-
// Find variable in scope chain
283-
const variable = findVariableInScopeChain(scope, baseObjectName);
284-
285-
// Skip certain variable types that shouldn't be extracted
286-
if (
287-
variable &&
288-
(isConstVariable(variable) ||
289-
isEnumVariable(variable) ||
290-
isImportVariable(variable))
291-
) {
292-
return;
293-
}
294-
295-
// Helper functions
296-
function findVariableInScopeChain(scope: TSESLint.Scope.Scope, name: string) {
297-
let currentScope: TSESLint.Scope.Scope | null = scope;
298-
while (currentScope) {
299-
const variable = currentScope.variables.find(
300-
(v) => v.name === name
301-
);
302-
if (variable) return variable;
303-
currentScope = currentScope.upper;
304-
}
305-
return null;
306-
}
307-
308-
function isConstVariable(variable: TSESLint.Scope.Variable) {
309-
return variable.defs.every(
310-
(def) => def.node && "kind" in def.node && def.node.kind === "const"
311-
);
312-
}
313-
314-
function isEnumVariable(variable: TSESLint.Scope.Variable) {
315-
return variable.defs.some(
316-
(def) =>
317-
(def.parent as TSESTree.Node)?.type ===
318-
AST_NODE_TYPES.TSEnumDeclaration
319-
);
320-
}
321-
322-
function isImportVariable(variable: TSESLint.Scope.Variable) {
323-
return variable.defs.some(
324-
(def) =>
325-
def.type === "ImportBinding" ||
326-
(def.node &&
327-
"type" in def.node &&
328-
def.node.type === AST_NODE_TYPES.ImportDeclaration)
329-
);
330-
}
331-
332284
const key = `${scope.block.range.join("-")}-${objectChain}`;
333285

334286
// Store node for auto-fixing

0 commit comments

Comments
 (0)