Skip to content
This repository has been archived by the owner on Jan 19, 2019. It is now read-only.

Core indent rule reports false positives in doc blocks #422

Closed
kaicataldo opened this issue Dec 23, 2017 · 3 comments
Closed

Core indent rule reports false positives in doc blocks #422

kaicataldo opened this issue Dec 23, 2017 · 3 comments

Comments

@kaicataldo
Copy link
Member

kaicataldo commented Dec 23, 2017

What version of TypeScript are you using?
typescript@2.6.2

What version of typescript-eslint-parser are you using?
typescript-eslint-parser@11.0.0

What code were you trying to parse?

/**
 * This is a function.
 * @param {String} text
 * @returns {String}
 */
function foo(text: string): string {}

The minimal repro case is:

/**
 * This is a function.
 * @param {String} text
 * @returns {String}
 */
function foo() {}

Running the autofixer generates the following code (which then passes):

 /**
 * This is a function.
{String} text
{String}
 */
function foo(text: string): string {}

What did you expect to happen?
No errors! This only occurs with the typescript-eslint-parser. It does not happen with the default parser.

What happened?

/Users/cataldo/Code/test/eslint-test/index.js
  3:1  error  Expected indentation of 0 spaces but found 3  indent
  4:1  error  Expected indentation of 0 spaces but found 3  indent

I'll try to look into the cause if I get a chance, but not sure if and when that'll be. Took the bug label off because I'm not sure what the established practice is in this repo - happy to update though :)

@kaicataldo
Copy link
Member Author

kaicataldo commented Dec 24, 2017

Digging into this a little more, it looks like this is because typescript-eslint-parser includes doc block components in the token list (I'm guessing this is because of Typescript's ability to parse doc blocks for type information when checking JS files). For example, the token list for the above is:

[ { type: 'Punctuator',
    value: '{',
    range: [ 37, 38 ],
    loc: { start: [Object], end: [Object] } },
  { type: 'Identifier',
    value: 'String',
    range: [ 38, 44 ],
    loc: { start: [Object], end: [Object] } },
  { type: 'Punctuator',
    value: '}',
    range: [ 44, 45 ],
    loc: { start: [Object], end: [Object] } },
  { type: 'Identifier',
    value: 'text',
    range: [ 46, 50 ],
    loc: { start: [Object], end: [Object] } },
  { type: 'Punctuator',
    value: '{',
    range: [ 63, 64 ],
    loc: { start: [Object], end: [Object] } },
  { type: 'Identifier',
    value: 'String',
    range: [ 64, 70 ],
    loc: { start: [Object], end: [Object] } },
  { type: 'Punctuator',
    value: '}',
    range: [ 70, 71 ],
    loc: { start: [Object], end: [Object] } },
  { type: 'Keyword',
    value: 'function',
    range: [ 76, 84 ],
    loc: { start: [Object], end: [Object] } },
  { type: 'Identifier',
    value: 'foo',
    range: [ 85, 88 ],
    loc: { start: [Object], end: [Object] } },
  { type: 'Punctuator',
    value: '(',
    range: [ 88, 89 ],
    loc: { start: [Object], end: [Object] } },
  { type: 'Identifier',
    value: 'text',
    range: [ 89, 93 ],
    loc: { start: [Object], end: [Object] } },
  { type: 'Punctuator',
    value: ':',
    range: [ 93, 94 ],
    loc: { start: [Object], end: [Object] } },
  { type: 'Identifier',
    value: 'string',
    range: [ 95, 101 ],
    loc: { start: [Object], end: [Object] } },
  { type: 'Punctuator',
    value: ')',
    range: [ 101, 102 ],
    loc: { start: [Object], end: [Object] } },
  { type: 'Punctuator',
    value: ':',
    range: [ 102, 103 ],
    loc: { start: [Object], end: [Object] } },
  { type: 'Identifier',
    value: 'string',
    range: [ 104, 110 ],
    loc: { start: [Object], end: [Object] } },
  { type: 'Punctuator',
    value: '{',
    range: [ 111, 112 ],
    loc: { start: [Object], end: [Object] } },
  { type: 'Punctuator',
    value: '}',
    range: [ 112, 113 ],
    loc: { start: [Object], end: [Object] } } ]

It seems like the fix in this case would be to align with Espree's behavior when it comes to handling comment tokens - since there isn't a spec for comments in ESTree - but I'll look into this a little more.

kaicataldo added a commit to kaicataldo/typescript-eslint-parser that referenced this issue Dec 24, 2017
@kaicataldo
Copy link
Member Author

PR up: #423

kaicataldo added a commit to kaicataldo/typescript-eslint-parser that referenced this issue Dec 24, 2017
kaicataldo added a commit to kaicataldo/typescript-eslint-parser that referenced this issue Dec 24, 2017
@kaicataldo
Copy link
Member Author

Please feel free to change the label if this is incorrect :) It's technically not the AST - it's actually the tokens array - that is causing the issue here.

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

1 participant