From 1bea9c106c7efc22ba5c73f60d1c7ca67f32f687 Mon Sep 17 00:00:00 2001 From: Meinhardt Klaus Date: Sun, 6 Dec 2020 18:29:51 +0100 Subject: [PATCH] new rule: initialize-static-property Fixes: #557 --- baselines/packages/mimir/api/packlist.txt | 3 ++ .../src/rules/initialize-static-property.d.ts | 4 +++ packages/mimir/recommended.yaml | 1 + .../src/rules/initialize-static-property.ts | 28 +++++++++++++++++++ 4 files changed, 36 insertions(+) create mode 100644 baselines/packages/mimir/api/src/rules/initialize-static-property.d.ts create mode 100644 packages/mimir/src/rules/initialize-static-property.ts diff --git a/baselines/packages/mimir/api/packlist.txt b/baselines/packages/mimir/api/packlist.txt index aa0694829..4834fe47f 100644 --- a/baselines/packages/mimir/api/packlist.txt +++ b/baselines/packages/mimir/api/packlist.txt @@ -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 diff --git a/baselines/packages/mimir/api/src/rules/initialize-static-property.d.ts b/baselines/packages/mimir/api/src/rules/initialize-static-property.d.ts new file mode 100644 index 000000000..ebd9e13b8 --- /dev/null +++ b/baselines/packages/mimir/api/src/rules/initialize-static-property.d.ts @@ -0,0 +1,4 @@ +import { AbstractRule } from '@fimbul/ymir'; +export declare class Rule extends AbstractRule { + apply(): void; +} diff --git a/packages/mimir/recommended.yaml b/packages/mimir/recommended.yaml index 90664329d..40e85ec98 100644 --- a/packages/mimir/recommended.yaml +++ b/packages/mimir/recommended.yaml @@ -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 diff --git a/packages/mimir/src/rules/initialize-static-property.ts b/packages/mimir/src/rules/initialize-static-property.ts new file mode 100644 index 000000000..a32f355cf --- /dev/null +++ b/packages/mimir/src/rules/initialize-static-property.ts @@ -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."); + } + } +}