vscode/.eslint-plugin-local/code-parameter-properties-must-have-explicit-accessibility.ts
Matt Bierner b8329a3ffc
Run TS eslint rules directly with strip-types
Wth node 20.18, we can now run these typescript files directly instead of having to use ts-node
2025-11-14 14:38:15 -08:00

41 lines
1.3 KiB
TypeScript

/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { TSESTree } from '@typescript-eslint/utils';
import * as eslint from 'eslint';
/**
* Enforces that all parameter properties have an explicit access modifier (public, protected, private).
*
* This catches a common bug where a service is accidentally made public by simply writing: `readonly prop: Foo`
*/
export default new class implements eslint.Rule.RuleModule {
create(context: eslint.Rule.RuleContext): eslint.Rule.RuleListener {
function check(node: TSESTree.TSParameterProperty) {
// For now, only apply to injected services
const firstDecorator = node.decorators?.at(0);
if (
firstDecorator?.expression.type !== 'Identifier'
|| !firstDecorator.expression.name.endsWith('Service')
) {
return;
}
if (!node.accessibility) {
context.report({
node: node,
message: 'Parameter properties must have an explicit access modifier.'
});
}
}
return {
['TSParameterProperty']: check,
};
}
};