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

new rule: initialize-static-property #734

Open
wants to merge 1 commit into
base: master
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: 3 additions & 0 deletions baselines/packages/mimir/api/packlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ src/rules/delete-only-optional-property.js.map
src/rules/generator-require-yield.d.ts
src/rules/generator-require-yield.js
src/rules/generator-require-yield.js.map
src/rules/initialize-static-property.d.ts
src/rules/initialize-static-property.js
src/rules/initialize-static-property.js.map
src/rules/new-parens.d.ts
src/rules/new-parens.js
src/rules/new-parens.js.map
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { AbstractRule } from '@fimbul/ymir';
export declare class Rule extends AbstractRule {
apply(): void;
}
1 change: 1 addition & 0 deletions packages/mimir/recommended.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ rules:
ban-dom-globals: error
delete-only-optional-property: error
generator-require-yield: error
initialize-static-property: error
new-parens: error
no-case-declaration: error
no-debugger: error
Expand Down
28 changes: 28 additions & 0 deletions packages/mimir/src/rules/initialize-static-property.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { AbstractRule, excludeDeclarationFiles } from '@fimbul/ymir';
import * as ts from 'typescript';
import { WrappedAst, getWrappedNodeAtPosition, isPropertyDeclaration, isStatementInAmbientContext, hasModifier } from 'tsutils';

@excludeDeclarationFiles
export class Rule extends AbstractRule {
public apply() {
let wrappedAst: WrappedAst | undefined;
const {text} = this.sourceFile;
const re = /\bstatic\b/g;

for (let match = re.exec(text); match !== null; match = re.exec(text)) {
const {node} = getWrappedNodeAtPosition(wrappedAst || (wrappedAst = this.context.getWrappedAst()), match.index)!;
if (node.kind !== ts.SyntaxKind.StaticKeyword)
continue;
const member = node.parent!;
if (
isPropertyDeclaration(member) &&
member.initializer === undefined &&
member.questionToken === undefined &&
// TODO move to tsutils as isAmbientPropertyDeclaration
!hasModifier(member.modifiers, ts.SyntaxKind.DeclareKeyword) &&
!(member.parent!.kind === ts.SyntaxKind.ClassDeclaration && isStatementInAmbientContext(member.parent!))
)
this.addFindingAtNode(member.name, "Non-optional 'static' property must be initialized.");
}
}
}