Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

callable-types auto fix produces invalid results #3334

Closed
mrweiss opened this issue Oct 17, 2017 · 2 comments
Closed

callable-types auto fix produces invalid results #3334

mrweiss opened this issue Oct 17, 2017 · 2 comments

Comments

@mrweiss
Copy link

mrweiss commented Oct 17, 2017

Bug Report

  • TSLint version: 5.7.0
  • TypeScript version: 2.5.2
  • Running TSLint via: VSCode

TypeScript code being linted

export function issue(foo: any | { (param: any): any }) { foo = 'bar'; }

with tslint.json configuration:

{
  "extends": ["tslint:recommended", "tslint-react"],
  "rulesDirectory": [
    "../../../../../../../../node_modules/tslint-eslint-rules/dist/rules",
    "../../../../../../../../node_modules/tslint-microsoft-contrib",
    "../../../../../../../../node_modules/tslint-misc-rules/rules"
  ],
  "rules": {
    "quotemark": [true, "single"],
    "ter-indent": ["error", 2, { "SwitchCase": true }],
    "object-curly-spacing": ["error", "always"],
    "member-access": [false],
    "ordered-imports": [false],
    "object-literal-sort-keys": false,
    "trailing-comma": [false],
    "arrow-parens": [false],
    "object-literal-key-quotes": [false],
    "variable-name": [false],
    "no-multi-spaces": ["error", "always"],
    "no-console": [false],
    "no-bitwise": false,
    "no-empty": false,
    "no-reference": false,
    "interface-over-type-literal": false,
    "react-tsx-curly-spacing": [true, "never", { "allowMultiline": true }],
    "array-bracket-spacing": [true, "never"],
    "block-spacing": [true, "always"],
    "no-unexpected-multiline": "error",
    "curly": true,
    "jsx-alignment": true,
    "jsx-wrap-multiline": true,
    "jsx-curly-spacing": ["never"],
    "jsx-no-multiline-js": false,
    "jsx-no-lambda": false,
    "class-method-newlines": true,
    "no-consecutive-blank-lines": [true],
    "max-classes-per-file": [
      false
    ]
  }
}

Actual behavior

severity: 'Error'
message: 'Type literal has only a call signature — use `(param: any) => any` instead. (callable-types)'
at: '2,36'

on auto fix changes to:

export function issue(foo: any | (param: any) => any) { foo = 'bar'; }

which is not valid:

severity: 'Error'
message: 'missing whitespace (one-line)'
at: '2,46'
source: 'tslint'

severity: 'Error'
message: ''{' or ';' expected.'
at: '2,47'
source: 'ts'

severity: 'Error'
message: ''any' only refers to a type, but is being used as a value here.'
at: '2,50'
source: 'ts'

severity: 'Error'
message: 'unused expression, expected an assignment or function call (no-unused-expression)'
at: '2,50'
source: 'tslint'

severity: 'Error'
message: 'Missing semicolon (semicolon)'
at: '2,53'
source: 'tslint'

severity: 'Error'
message: '';' expected.'
at: '2,53'
source: 'ts'

severity: 'Error'
message: 'Cannot find name 'foo'.'
at: '2,57'
source: 'ts'

Expected behavior

Auto fixes to a valid syntax. The suggested fix by the rule definition is:

export function issue(foo: any | { (param: any) => any }) { foo = 'bar'; }

but that is not a valid TypeScript, either:

severity: 'Error'
message: '':' expected.'
at: '2,49'
source: 'ts'
@ajafff
Copy link
Contributor

ajafff commented Oct 17, 2017

I can confirm that this is a bug. The fixer needs to add parens around the function signature if it is the child of a union or intersection type.

@ajafff
Copy link
Contributor

ajafff commented Oct 18, 2017

Fixed by #3342

@ajafff ajafff closed this as completed Oct 18, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants