<commit_before>var warnStyle = {"border-bottom": "2px dotted orange"},
    errorStyle = {"background-color": "red"};

export default class JavaScriptChecker {

  // targetMode: "ace/mode/javascript"

  uninstall(editor) {
    var morph = editor.text || editor;
    (morph.markers || []).forEach(ea => ea.id.startsWith("js-checker-") && morph.removeMarker(ea))
    morph.removeMarker("js-syntax-error");
  }

  parse(src, astType = null) {
    // astType = 'FunctionExpression' || astType == 'FunctionDeclaration' || null
    
    // FIXME!
    var astModule = System.get(System.decanonicalize("lively.ast"));
    if (!astModule) return null;

    var options = {withComments: true, allowReturnOutsideFunction: true};
    options.type = astType;
    return astModule.fuzzyParse(src, options);
  }

  onDocumentChange(change, editor) {
    this.updateAST(change, editor)
  }

  updateAST(change, editor) {
    var ast, morph = editor.text || editor;
    
    // 1. parse
    try {
      ast = this.parse(morph.textString);
    } catch(e) { ast = e; }
  
    // 2. update lively codemarker
    // var morph = that
    // var ast = new JavaScriptChecker().parse(morph.textString)

    var doc = morph.document;

    var query = System.get(System.decanonicalize("lively.ast")).query; //a

    // if (codeEditor.getShowWarnings()) {
      var prevMarkers = (morph.markers || []).filter(({id}) => id.startsWith("js-checker-")),
          globals = query.findGlobalVarRefs(ast, {jslintGlobalComment: true}),
          newMarkers = globals.map(({start, end, name, type}, i) => {
            start = doc.indexToPosition(start);
            end = doc.indexToPosition(end);
            return morph.addMarker({id: "js-checker-" + i, style: warnStyle, range: {start, end}})
          });
      prevMarkers.slice(newMarkers.length).forEach(ea => morph.removeMarker(ea))
    // }
  
    if (ast.parseError/* && codeEditor.getShowErrors()*/) {
      var {column, line} = ast.parseError.loc,
          row = line-1;
      morph.addMarker({
        id: "js-syntax-error",
        range: {start: {column: column-1, row}, end: {column: column+1, row}},
        style: errorStyle
      })
    } else {
      morph.removeMarker("js-syntax-error")
    }
  }
}<commit_msg>js checker fixes<commit_after>var warnStyle = {"border-bottom": "2px dotted orange"},
    errorStyle = {"background-color": "red"};

export default class JavaScriptChecker {

  // targetMode: "ace/mode/javascript"

  uninstall(editor) {
    var morph = editor.text || editor;
    (morph.markers || []).forEach(ea => ea.id.startsWith("js-checker-") && morph.removeMarker(ea))
    morph.removeMarker("js-syntax-error");
  }

  parse(src, astType = null) {
    // astType = 'FunctionExpression' || astType == 'FunctionDeclaration' || null
    
    // FIXME!
    var astModule = System.get(System.decanonicalize("lively.ast"));
    if (!astModule) return null;

    var options = {withComments: true, allowReturnOutsideFunction: true};
    options.type = astType;
    return astModule.fuzzyParse(src, options);
  }

  onDocumentChange(change, editor) {
    this.updateAST(change, editor)
  }

  updateAST(change, editor) {
    var ast, morph = editor.text || editor;
    
    // 1. parse
    try {
      ast = this.parse(morph.textString);
    } catch(e) { ast = e; }
  
    // 2. update lively codemarker
    // var morph = that
    // var ast = new JavaScriptChecker().parse(morph.textString)

    var doc = morph.document;

    var query = System.get(System.decanonicalize("lively.ast")).query; //a

    // if (codeEditor.getShowWarnings()) {
      var prevMarkers = (morph.markers || []).filter(({id}) => id.startsWith("js-checker-")),
          globals = query.findGlobalVarRefs(ast, {jslintGlobalComment: true}),
          newMarkers = globals.map(({start, end, name, type}, i) => {
            start = doc.indexToPosition(start);
            end = doc.indexToPosition(end);
            return morph.addMarker({id: "js-checker-" + i, style: warnStyle, range: {start, end}})
          });
      prevMarkers.slice(newMarkers.length).forEach(ea => morph.removeMarker(ea))
    // }

    if (ast.parseError/* && codeEditor.getShowErrors()*/) {
      var {column, line} = ast.parseError.loc, row = line-1;
      var {column, row} = doc.indexToPosition(ast.parseError.pos)
      morph.addMarker({
        id: "js-syntax-error",
        range: {start: {column: column-1, row}, end: {column: column+1, row}},
        style: errorStyle
      })
    } else {
      morph.removeMarker("js-syntax-error")
    }
  }
}
{'fraction_non_alphanumeric': 0.15967493960026355, 'fraction_numerical': 0.002635624862727872, 'mean_word_length': 2.8625954198473282, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1662122', 'n_tokens_mistral': 1488, 'n_tokens_neox': 1456, 'n_words': 298}