diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index fb4af5d729..e4ad333988 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -7954,31 +7954,12 @@ namespace ts { return jsxElementType || anyType; } - function tagNamesAreEquivalent(lhs: EntityName, rhs: EntityName): boolean { - if (lhs.kind !== rhs.kind) { - return false; - } - - if (lhs.kind === SyntaxKind.Identifier) { - return (lhs).text === (rhs).text; - } - - return (lhs).right.text === (rhs).right.text && - tagNamesAreEquivalent((lhs).left, (rhs).left); - } - function checkJsxElement(node: JsxElement) { // Check attributes checkJsxOpeningLikeElement(node.openingElement); - // Check that the closing tag matches - if (!tagNamesAreEquivalent(node.openingElement.tagName, node.closingElement.tagName)) { - error(node.closingElement, Diagnostics.Expected_corresponding_JSX_closing_tag_for_0, getTextOfNode(node.openingElement.tagName)); - } - else { - // Perform resolution on the closing tag so that rename/go to definition/etc work - getJsxElementTagSymbol(node.closingElement); - } + // Perform resolution on the closing tag so that rename/go to definition/etc work + getJsxElementTagSymbol(node.closingElement); // Check children for (const child of node.children) { diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index a43f3534df..f7268d6d99 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -2586,5 +2586,9 @@ "A type assertion expression is not allowed in the left-hand side of an exponentiation expression. Consider enclosing the expression in parentheses.": { "category": "Error", "code": 17007 + }, + "JSX element '{0}' has no corresponding closing tag.": { + "category": "Error", + "code": 17008 } } diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 7f5d052a7e..390ab21054 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -3497,6 +3497,20 @@ namespace ts { return finishNode(node); } + function tagNamesAreEquivalent(lhs: EntityName, rhs: EntityName): boolean { + if (lhs.kind !== rhs.kind) { + return false; + } + + if (lhs.kind === SyntaxKind.Identifier) { + return (lhs).text === (rhs).text; + } + + return (lhs).right.text === (rhs).right.text && + tagNamesAreEquivalent((lhs).left, (rhs).left); + } + + function parseJsxElementOrSelfClosingElement(inExpressionContext: boolean): JsxElement | JsxSelfClosingElement { const opening = parseJsxOpeningOrSelfClosingElement(inExpressionContext); let result: JsxElement | JsxSelfClosingElement; @@ -3506,6 +3520,11 @@ namespace ts { node.children = parseJsxChildren(node.openingElement.tagName); node.closingElement = parseJsxClosingElement(inExpressionContext); + + if (!tagNamesAreEquivalent(node.openingElement.tagName, node.closingElement.tagName)) { + parseErrorAtPosition(node.closingElement.pos, node.closingElement.end - node.closingElement.pos, Diagnostics.Expected_corresponding_JSX_closing_tag_for_0, getTextOfNodeFromSourceText(sourceText, node.openingElement.tagName)); + } + result = finishNode(node); } else { @@ -3565,10 +3584,13 @@ namespace ts { while (true) { token = scanner.reScanJsxToken(); if (token === SyntaxKind.LessThanSlashToken) { + // Closing tag break; } else if (token === SyntaxKind.EndOfFileToken) { - parseErrorAtCurrentToken(Diagnostics.Expected_corresponding_JSX_closing_tag_for_0, getTextOfNodeFromSourceText(sourceText, openingTagName)); + // If we hit EOF, issue the error at the tag that lacks the closing element + // rather than at the end of the file (which is useless) + parseErrorAtPosition(openingTagName.pos, openingTagName.end - openingTagName.pos, Diagnostics.JSX_element_0_has_no_corresponding_closing_tag, getTextOfNodeFromSourceText(sourceText, openingTagName)); break; } result.push(parseJsxChild()); diff --git a/tests/baselines/reference/jsxAndTypeAssertion.errors.txt b/tests/baselines/reference/jsxAndTypeAssertion.errors.txt index aebabf1513..d0e9bcf145 100644 --- a/tests/baselines/reference/jsxAndTypeAssertion.errors.txt +++ b/tests/baselines/reference/jsxAndTypeAssertion.errors.txt @@ -1,14 +1,20 @@ +tests/cases/conformance/jsx/jsxAndTypeAssertion.tsx(7,6): error TS17008: JSX element 'any' has no corresponding closing tag. tests/cases/conformance/jsx/jsxAndTypeAssertion.tsx(7,13): error TS2304: Cannot find name 'test'. tests/cases/conformance/jsx/jsxAndTypeAssertion.tsx(7,17): error TS1005: '}' expected. +tests/cases/conformance/jsx/jsxAndTypeAssertion.tsx(9,6): error TS17008: JSX element 'any' has no corresponding closing tag. +tests/cases/conformance/jsx/jsxAndTypeAssertion.tsx(11,6): error TS17008: JSX element 'foo' has no corresponding closing tag. tests/cases/conformance/jsx/jsxAndTypeAssertion.tsx(11,32): error TS1005: '}' expected. tests/cases/conformance/jsx/jsxAndTypeAssertion.tsx(13,36): error TS1005: '}' expected. +tests/cases/conformance/jsx/jsxAndTypeAssertion.tsx(15,17): error TS17008: JSX element 'foo' has no corresponding closing tag. tests/cases/conformance/jsx/jsxAndTypeAssertion.tsx(15,45): error TS1005: '}' expected. +tests/cases/conformance/jsx/jsxAndTypeAssertion.tsx(19,2): error TS17008: JSX element 'foo' has no corresponding closing tag. +tests/cases/conformance/jsx/jsxAndTypeAssertion.tsx(19,8): error TS17008: JSX element 'foo' has no corresponding closing tag. +tests/cases/conformance/jsx/jsxAndTypeAssertion.tsx(19,13): error TS17008: JSX element 'foo' has no corresponding closing tag. tests/cases/conformance/jsx/jsxAndTypeAssertion.tsx(22,1): error TS1005: ':' expected. -tests/cases/conformance/jsx/jsxAndTypeAssertion.tsx(22,1): error TS17002: Expected corresponding JSX closing tag for 'any'. -tests/cases/conformance/jsx/jsxAndTypeAssertion.tsx(22,1): error TS17002: Expected corresponding JSX closing tag for 'foo'. +tests/cases/conformance/jsx/jsxAndTypeAssertion.tsx(22,1): error TS1005: ' { test: }; + ~~~ +!!! error TS17008: JSX element 'any' has no corresponding closing tag. ~~~~ !!! error TS2304: Cannot find name 'test'. ~ !!! error TS1005: '}' expected. x = ; + ~~~ +!!! error TS17008: JSX element 'any' has no corresponding closing tag. x = hello {{}} ; + ~~~ +!!! error TS17008: JSX element 'foo' has no corresponding closing tag. ~ !!! error TS1005: '}' expected. @@ -32,18 +44,24 @@ tests/cases/conformance/jsx/jsxAndTypeAssertion.tsx(22,1): error TS17002: Expect !!! error TS1005: '}' expected. x = {}}>hello{{}}; + ~~~ +!!! error TS17008: JSX element 'foo' has no corresponding closing tag. ~ !!! error TS1005: '}' expected. x = x, x = ; {{/foo/.test(x) ? : }} + ~~~ +!!! error TS17008: JSX element 'foo' has no corresponding closing tag. + ~~~ +!!! error TS17008: JSX element 'foo' has no corresponding closing tag. + ~~~ +!!! error TS17008: JSX element 'foo' has no corresponding closing tag. !!! error TS1005: ':' expected. -!!! error TS17002: Expected corresponding JSX closing tag for 'any'. - -!!! error TS17002: Expected corresponding JSX closing tag for 'foo'. \ No newline at end of file +!!! error TS1005: '' expected. tests/cases/conformance/jsx/jsxInvalidEsprimaTestSuite.tsx(28,10): error TS2304: Cannot find name 'props'. tests/cases/conformance/jsx/jsxInvalidEsprimaTestSuite.tsx(28,28): error TS1005: '>' expected. +tests/cases/conformance/jsx/jsxInvalidEsprimaTestSuite.tsx(32,2): error TS17008: JSX element 'a' has no corresponding closing tag. tests/cases/conformance/jsx/jsxInvalidEsprimaTestSuite.tsx(32,6): error TS1005: '{' expected. +tests/cases/conformance/jsx/jsxInvalidEsprimaTestSuite.tsx(33,2): error TS17008: JSX element 'a' has no corresponding closing tag. tests/cases/conformance/jsx/jsxInvalidEsprimaTestSuite.tsx(33,6): error TS1005: '{' expected. tests/cases/conformance/jsx/jsxInvalidEsprimaTestSuite.tsx(33,7): error TS1109: Expression expected. tests/cases/conformance/jsx/jsxInvalidEsprimaTestSuite.tsx(35,4): error TS1003: Identifier expected. -tests/cases/conformance/jsx/jsxInvalidEsprimaTestSuite.tsx(35,21): error TS17002: Expected corresponding JSX closing tag for 'a'. +tests/cases/conformance/jsx/jsxInvalidEsprimaTestSuite.tsx(35,21): error TS1005: '; @@ -188,7 +192,11 @@ tests/cases/conformance/jsx/jsxInvalidEsprimaTestSuite.tsx(35,21): error TS17002 ~ !!! error TS1109: Expression expected. ; + ~ +!!! error TS17008: JSX element 'a' has no corresponding closing tag. ; + ~ +!!! error TS17008: JSX element 'a' has no corresponding closing tag. var x =
one
two
;; var x =
one
/* intervening comment */
two
;;
{"str";}; @@ -218,9 +226,13 @@ tests/cases/conformance/jsx/jsxInvalidEsprimaTestSuite.tsx(35,21): error TS17002 >; >; ; + ~ +!!! error TS17008: JSX element 'a' has no corresponding closing tag. ~ !!! error TS1005: '{' expected. ; + ~ +!!! error TS17008: JSX element 'a' has no corresponding closing tag. ~ !!! error TS1005: '{' expected. ~ @@ -230,4 +242,4 @@ tests/cases/conformance/jsx/jsxInvalidEsprimaTestSuite.tsx(35,21): error TS17002 ~~~ !!! error TS1003: Identifier expected. -!!! error TS17002: Expected corresponding JSX closing tag for 'a'. \ No newline at end of file +!!! error TS1005: '; + ~~~ +!!! error TS17008: JSX element 'div' has no corresponding closing tag. + ~~~~~~ +!!! error TS17002: Expected corresponding JSX closing tag for 'span'. + + +!!! error TS1005: '; + ~~~~~~~ +!!! error TS17002: Expected corresponding JSX closing tag for 'div'. + + +==== tests/cases/conformance/jsx/Error3.tsx (2 errors) ==== + let x3 =
; + ~~~ +!!! error TS17008: JSX element 'div' has no corresponding closing tag. + + + +!!! error TS1005: '
; + ~~~ +!!! error TS17008: JSX element 'div' has no corresponding closing tag. + ~~~~~~~ +!!! error TS17002: Expected corresponding JSX closing tag for 'div'. + + +!!! error TS1005: ' + ~~~ +!!! error TS17008: JSX element 'div' has no corresponding closing tag. + ~~~~ +!!! error TS17008: JSX element 'span' has no corresponding closing tag. + + + +!!! error TS1005: '
; + +//// [Error2.tsx] +let x2 =
; + + +//// [Error3.tsx] +let x3 =
; + + +//// [Error4.tsx] +let x4 =
; + +//// [Error5.tsx] +let x5 =
+ + + +//// [file.jsx] +//// [Error1.jsx] +// Issue error about missing span closing tag, not missing div closing tag +var x1 =
; +; +//// [Error2.jsx] +var x2 =
; +//// [Error3.jsx] +var x3 =
; + +; +//// [Error4.jsx] +var x4 =
; +; +//// [Error5.jsx] +var x5 =
+ +; diff --git a/tests/baselines/reference/tsxErrorRecovery1.errors.txt b/tests/baselines/reference/tsxErrorRecovery1.errors.txt index 0937b0d37a..c368e48ddd 100644 --- a/tests/baselines/reference/tsxErrorRecovery1.errors.txt +++ b/tests/baselines/reference/tsxErrorRecovery1.errors.txt @@ -1,15 +1,18 @@ +tests/cases/conformance/jsx/file.tsx(5,11): error TS17008: JSX element 'div' has no corresponding closing tag. tests/cases/conformance/jsx/file.tsx(5,19): error TS1109: Expression expected. tests/cases/conformance/jsx/file.tsx(8,11): error TS2304: Cannot find name 'a'. tests/cases/conformance/jsx/file.tsx(8,12): error TS1005: '}' expected. -tests/cases/conformance/jsx/file.tsx(9,1): error TS17002: Expected corresponding JSX closing tag for 'div'. +tests/cases/conformance/jsx/file.tsx(9,1): error TS1005: ' {
+ ~~~ +!!! error TS17008: JSX element 'div' has no corresponding closing tag. ~~ !!! error TS1109: Expression expected. } @@ -21,4 +24,4 @@ tests/cases/conformance/jsx/file.tsx(9,1): error TS17002: Expected corresponding !!! error TS1005: '}' expected. -!!! error TS17002: Expected corresponding JSX closing tag for 'div'. \ No newline at end of file +!!! error TS1005: '
; + +// @filename: Error2.tsx +let x2 =
; + + +// @filename: Error3.tsx +let x3 =
; + + +// @filename: Error4.tsx +let x4 =
; + +// @filename: Error5.tsx +let x5 =
+