Repository: TypeScript
Issue: ts.createJsxOpeningElement throws `Debug Failure. False expression.`
Labels: Bug,API

<!-- 🚨 STOP 🚨 𝗦𝗧𝗢𝗣 🚨 𝑺𝑻𝑶𝑷 🚨

Half of all issues filed here are duplicates, answered in the FAQ, or not appropriate for the bug tracker. Even if you think you've found a *bug*, please read the FAQ first, especially the Common "Bugs" That Aren't Bugs section!

Please help us by doing the following steps before logging an issue:
  * Search: https://github.com/Microsoft/TypeScript/search?type=Issues
  * Read the FAQ: https://github.com/Microsoft/TypeScript/wiki/FAQ

Please fill in the *entire* template below.
-->

<!-- Please try to reproduce the issue with `typescript@next`. It may have already been fixed. -->
**TypeScript Version:** 2.8.3 (I used this version because I was getting a completely different error when I updated to the latest @next version. Probably caused by ttypescript or some other dependency that's not updated to the latest version)

<!-- Search terms you tried before logging this (so others can find this issue more easily) -->
**Search Terms:** compiler API, transform

**Code**

```ts
import * as ts from 'typescript'

export default function transformer() {
  return (context: ts.TransformationContext) => {
    return (sourceFile: ts.SourceFile) => {
      return visitNodeAndChildren(sourceFile)

      function visitNodeAndChildren(node: ts.Node): ts.VisitResult<ts.Node> {
        return ts.visitEachChild(visitNode(node), (childNode) => visitNodeAndChildren(childNode), context)
      }

      function visitNode(node: ts.Node): ts.Node {
        switch (node.kind) {
          case ts.SyntaxKind.JsxElement:
            const element = node as ts.JsxElement

            return ts.createJsxElement(ts.createJsxOpeningElement(element.openingElement.tagName, element.openingElement.attributes), element.children, element.closingElement)
          default:
            return node
        }
      }
    }
  }
}
```

I'm running this using [ttsc](https://github.com/cevek/ttypescript/tree/master) but I don't think that's the issue.

**Expected behavior:**
No error.

**Actual behavior:**
```(node:23136) UnhandledPromiseRejectionWarning: Error: Debug Failure. False expression.
    at resolveNameHelper (node_modules\typescript\lib\typescript.js:26726:30)
    at resolveName (node_modules\typescript\lib\typescript.js:26489:20)
    at getReferencedValueSymbol (node_modules\typescript\lib\typescript.js:48917:20)
    at Object.getReferencedDeclarationWithCollidingName (node_modules\typescript\lib\typescript.js:48672:34)
    at substituteExpressionIdentifier (node_modules\typescript\lib\typescript.js:63756:44)
    at substituteExpression (node_modules\typescript\lib\typescript.js:63743:28)
    at onSubstituteNode (node_modules\typescript\lib\typescript.js:63696:24)
    at onSubstituteNode (node_modules\typescript\lib\typescript.js:65555:20)
    at onSubstituteNode (node_modules\typescript\lib\typescript.js:69452:20)
    at substituteNode (node_modules\typescript\lib\typescript.js:69643:59)
    at trySubstituteNode (node_modules\typescript\lib\typescript.js:73151:46)
    at pipelineEmitWithComments (node_modules\typescript\lib\typescript.js:72765:20)
    at emitNodeWithNotification (node_modules\typescript\lib\typescript.js:69674:21)
    at pipelineEmitWithNotification (node_modules\typescript\lib\typescript.js:72758:17)
    at emitExpression (node_modules\typescript\lib\typescript.js:72754:13)
    at emitPropertyAccessExpression (node_modules\typescript\lib\typescript.js:73547:13)
    at pipelineEmitExpression (node_modules\typescript\lib\typescript.js:73087:28)
    at pipelineEmitWithHint (node_modules\typescript\lib\typescript.js:72785:49)
    at emitNodeWithSourceMap (node_modules\typescript\lib\typescript.js:70051:21)
    at pipelineEmitWithSourceMap (node_modules\typescript\lib\typescript.js:72775:17)
    at emitNodeWithNestedComments (node_modules\typescript\lib\typescript.js:70340:17)
    at emitNodeWithSynthesizedComments (node_modules\typescript\lib\typescript.js:70290:13)
    at emitNodeWithComments (node_modules\typescript\lib\typescript.js:70226:21)
    at pipelineEmitWithComments (node_modules\typescript\lib\typescript.js:72767:17)
    at emitNodeWithNotification (node_modules\typescript\lib\typescript.js:69674:21)
    at pipelineEmitWithNotification (node_modules\typescript\lib\typescript.js:72758:17)
    at emitExpression (node_modules\typescript\lib\typescript.js:72754:13)
    at emitCallExpression (node_modules\typescript\lib\typescript.js:73584:13)
    at pipelineEmitExpression (node_modules\typescript\lib\typescript.js:73091:28)
    at pipelineEmitWithHint (node_modules\typescript\lib\typescript.js:72785:49)
(node:23136) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a
 promise which was not handled with .catch(). (rejection id: 1)
```

**Workaround**: This seems to actually error sometimes and I can't figure out how to reproduce the fail so I don't recommend to use this workaround. To work around this, I had to remove the ts.createJsxOpeningElement call and alter the properties myself. Example:
```diff
import * as ts from 'typescript'

export default function transformer() {
  return (context: ts.TransformationContext) => {
    return (sourceFile: ts.SourceFile) => {
      return visitNodeAndChildren(sourceFile)

      function visitNodeAndChildren(node: ts.Node): ts.VisitResult<ts.Node> {
        return ts.visitEachChild(visitNode(node), (childNode) => visitNodeAndChildren(childNode), context)
      }

      function visitNode(node: ts.Node): ts.Node {
        switch (node.kind) {
          case ts.SyntaxKind.JsxElement:
            const element = node as ts.JsxElement

+           element.openingElement.tagName = ts.createLiteral('whatever') // I wanted to alter the tag name in my case but should be the same for altering the attributes

-           return ts.createJsxElement(ts.createJsxOpeningElement(element.openingElement.tagName, element.openingElement.attributes), element.children, element.closingElement)
+           return ts.createJsxElement(element.openingElement, element.children, element.closingElement)
          default:
            return node
        }
      }
    }
  }
}
```