diff --git a/lib/languages/lib/javascript.dart b/lib/languages/lib/javascript.dart index 1692032..7d6e60b 100644 --- a/lib/languages/lib/javascript.dart +++ b/lib/languages/lib/javascript.dart @@ -1,20 +1,23 @@ import 'package:re_highlight/re_highlight.dart'; -ModeCallback callbackOnBegin = (EnhancedMatch match, ModeCallbackResponse response) { +ModeCallback callbackOnBegin = + (EnhancedMatch match, ModeCallbackResponse response) { final String? match0 = match[0]; if (match0 == null) { return; } final int afterMatchIndex = match0.length + match.index; - final String nextChar = match.input.substring(afterMatchIndex, afterMatchIndex + 1); + final String? nextChar = afterMatchIndex < match.input.length + ? match.input[afterMatchIndex] + : null; if ( - // HTML should not include another raw `<` inside a tag - // nested type? - // `>`, etc. - nextChar == "<" || - // the , gives away that this is not HTML - // `` - nextChar == ",") { + // HTML should not include another raw `<` inside a tag + // nested type? + // `>`, etc. + nextChar == "<" || + // the , gives away that this is not HTML + // `` + nextChar == ",") { response.ignoreMatch(); return; } @@ -57,4 +60,4 @@ bool _hasClosingTag(EnhancedMatch match, int after) { final String tag = "float # float'); + HighlightResult result = highlight.highlight( + code: code, language: 'java', ignoreIllegals: true); + expect(result.toHtml(), + 'float # float'); code = 'float # float'; - result = highlight.highlight(code: code, language: 'java', ignoreIllegals: false); + result = highlight.highlight( + code: code, language: 'java', ignoreIllegals: false); expect(result.toHtml(), 'float # float'); expect(result.illegal, true); // defaults to true code = 'float # float'; result = highlight.highlight(code: code, language: 'java'); - expect(result.toHtml(), 'float # float'); + expect(result.toHtml(), + 'float # float'); expect(result.illegal, false); }); + test('should not crash on incomplete JSX-like tags at end of input', () { + const String code = 'const x = result.toHtml(), returnsNormally); + + result = safeHighlight.highlight(code: code, language: 'typescript'); + expect(result.errorRaised, isNull); + expect(() => result.toHtml(), returnsNormally); + + final Highlight debugHighlight = Highlight(); + debugHighlight.debugMode(); + debugHighlight.registerLanguages(builtinAllLanguages); + + expect( + () => debugHighlight + .highlight(code: code, language: 'javascript') + .toHtml(), + returnsNormally); + expect( + () => debugHighlight + .highlight(code: code, language: 'typescript') + .toHtml(), + returnsNormally); + }); test('should use new API with options', () { const String code = 'public void moveTo(int x, int y, int z);'; - final HighlightResult result = highlight.highlight(code: code, language: 'java'); + final HighlightResult result = + highlight.highlight(code: code, language: 'java'); - expect(result.toHtml(), - 'public ' - 'void moveTo' - '(int x, ' - 'int y, ' - 'int z);' - ); + expect( + result.toHtml(), + 'public ' + 'void moveTo' + '(int x, ' + 'int y, ' + 'int z);'); }); test('should works without continuation', () { const String code = "public void moveTo(int x, int y, int z);"; - final HighlightResult result = highlight.highlight(code: code, language: 'java'); + final HighlightResult result = + highlight.highlight(code: code, language: 'java'); - expect(result.toHtml(), - 'public ' - 'void moveTo' - '(int x, ' - 'int y, ' - 'int z);' - ); + expect( + result.toHtml(), + 'public ' + 'void moveTo' + '(int x, ' + 'int y, ' + 'int z);'); }); }); -} \ No newline at end of file +}