palantir / tslint-react

:orange_book: Lint rules related to React & JSX for TSLint.
Apache License 2.0
749 stars 76 forks source link

TS 2.3 API change breaks jsx-alignment #87

Closed Jessidhia closed 7 years ago

Jessidhia commented 7 years ago

The above error seems to happen when using tslint 5.1.0, typescript 2.3.0; not sure how to reduce the input to a code snippet.

The unique part of the stack trace, for which I had to edit tslint/lib/linter.js to be able to obtain at all, looks like this:

TypeError: Cannot read property 'getStart' of undefined
  at JsxAlignmentWalker.getLineAndCharacter (.../node_modules/tslint-react/rules/jsxAlignmentRule.js:128:61)
  at JsxAlignmentWalker._this.getCharacter (.../node_modules/tslint-react/rules/jsxAlignmentRule.js:52:61)
  at JsxAlignmentWalker.checkElement (.../node_modules/tslint-react/rules/jsxAlignmentRule.js:80:39)
  at JsxAlignmentWalker.visitJsxElement (.../node_modules/tslint-react/rules/jsxAlignmentRule.js:60:18)
  at JsxAlignmentWalker.SyntaxWalker.visitNode (.../node_modules/tslint/lib/language/walker/syntaxWalker.js:401:22)
  at .../node_modules/tslint/lib/language/walker/syntaxWalker.js:530:63
  at visitEachNode (.../node_modules/typescript/lib/typescript.js:14674:30)
  at Object.forEachChild (.../node_modules/typescript/lib/typescript.js:14998:21)
  at JsxAlignmentWalker.SyntaxWalker.walkChildren (.../node_modules/tslint/lib/language/walker/syntaxWalker.js:530:12)
  at JsxAlignmentWalker.SyntaxWalker.visitJsxElement (.../node_modules/tslint/lib/language/walker/syntaxWalker.js:153:14)
  at JsxAlignmentWalker.visitJsxElement (.../node_modules/tslint-react/rules/jsxAlignmentRule.js:62:42)
  at JsxAlignmentWalker.SyntaxWalker.visitNode (.../node_modules/tslint/lib/language/walker/syntaxWalker.js:401:22)
Jessidhia commented 7 years ago

It looks like TypeScript 2.3 has a new JSXAttributes kind (253), which is the new type of the opening element attributes property, instead of being a just an array of JSXAttribute.

The commit to blame is probably https://github.com/Microsoft/TypeScript/commit/41108dbaaec656b3bfedc77be63ac143c56c76ee

Jessidhia commented 7 years ago

I worked around it locally by editing node_modules/tslint-react/rules/jsxAlignmentRule.js like so:

     JsxAlignmentWalker.prototype.checkElement = function (elementOpen, attributes, elementClose, closingTag) {
+        attributes = attributes && attributes.properties ? attributes.properties : attributes;
         if (attributes == null || attributes.length === 0) {

But this change would be invalid in the typescript source.