Skip to content

prefer-instanceof autofix produces invalid .instanceOf() method calls instead of standard instanceof operator #154

@zsviczian

Description

@zsviczian

Description

The prefer-instanceof rule's autofix produces broken code that fails at runtime. When --fix is run on TypeScript files, the rule transforms valid instanceof operator checks into invalid .instanceOf() method calls, which don't exist in standard JavaScript/TypeScript.

Steps to Reproduce

  1. Create a TypeScript file with a standard instanceof check:

    if (target instanceof HTMLElement) {
      console.log("target is an HTMLElement");
    }
  2. Run eslint --fix on this file with eslint-plugin-obsidianmd enabled and prefer-instanceof rule enabled for TypeScript files

  3. The code is transformed to:

    if (target.instanceOf HTMLElement) {  // ❌ INVALID - .instanceOf is not a method
      console.log("target is an HTMLElement");
    }

Expected Behavior

  • Either the autofix should be removed/disabled
  • Or the autofix should provide a valid alternative (e.g., a custom helper function)
  • The rule should not produce code that throws runtime errors like TypeError: ...instanceOf is not a function

Actual Behavior

The autofix corrupts valid code by replacing the standard instanceof operator with a nonexistent .instanceOf() method call. This breaks type checking and causes runtime failures in any code path that executes the check.

Environment

  • eslint-plugin-obsidianmd: ^0.3.0
  • eslint: ^9.27.0
  • TypeScript: 5.9.3

Impact

Projects using this plugin with --fix enabled will have their instanceof checks corrupted, leading to:

  • Runtime TypeError: X is not a function errors
  • Broken type guards that fail silently or crash
  • Breaking changes on auto-fix runs that require manual cleanup

Additional Context

The issue appears to stem from the rule being intended for linting only, not for autofixing. The transformation doesn't map to any valid JavaScript/TypeScript API.

Workaround

Until this is fixed, the rule should be disabled in eslint config:

{
  files: ["**/*.{ts,tsx}"],
  rules: {
    "obsidianmd/prefer-instanceof": "off",
  },
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions