<commit_before>package com.redhat.ceylon.compiler.js;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.io.Writer;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import net.minidev.json.JSONObject;

import com.redhat.ceylon.cmr.api.ArtifactContext;
import com.redhat.ceylon.cmr.api.RepositoryManager;
import com.redhat.ceylon.cmr.api.SourceArchiveCreator;
import com.redhat.ceylon.cmr.ceylon.CeylonUtils;
import com.redhat.ceylon.cmr.impl.JULLogger;
import com.redhat.ceylon.cmr.impl.ShaSigner;
import com.redhat.ceylon.compiler.Options;
import com.redhat.ceylon.compiler.typechecker.TypeChecker;
import com.redhat.ceylon.compiler.typechecker.context.PhasedUnit;
import com.redhat.ceylon.compiler.typechecker.tree.AnalysisMessage;
import com.redhat.ceylon.compiler.typechecker.analyzer.AnalysisWarning;
import com.redhat.ceylon.compiler.typechecker.analyzer.AnalysisError;
import com.redhat.ceylon.compiler.typechecker.analyzer.UsageWarning;
import com.redhat.ceylon.compiler.typechecker.model.Module;
import com.redhat.ceylon.compiler.typechecker.model.TypedDeclaration;
import com.redhat.ceylon.compiler.typechecker.model.Unit;
import com.redhat.ceylon.compiler.typechecker.parser.RecognitionError;
import com.redhat.ceylon.compiler.typechecker.tree.Message;
import com.redhat.ceylon.compiler.typechecker.tree.Node;
import com.redhat.ceylon.compiler.typechecker.tree.Tree;
import com.redhat.ceylon.compiler.typechecker.tree.Visitor;

public class JsCompiler {

    protected final TypeChecker tc;
    protected final Options opts;
    protected final RepositoryManager outRepo;

    private boolean stopOnErrors = true;
    private int errCount = 0;

    protected Set<Message> errors = new HashSet<Message>();
    protected Set<Message> unitErrors = new HashSet<Message>();
    protected List<String> files;
    private final Map<Module, JsOutput> output = new HashMap<Module, JsOutput>();
    //You have to manually set this when compiling the language module
    static boolean compilingLanguageModule;
    private TypeUtils types;

    private final Visitor unitVisitor = new Visitor() {
        private boolean hasErrors(Node that) {
            for (Message m: that.getErrors()) {
                if (m instanceof AnalysisError) {
                    return true;
                }
            }
            return false;
        }
        @Override
        public void visitAny(Node that) {
            super.visitAny(that);
            for (Message err: that.getErrors()) {
                unitErrors.add(err);
            }
        }
        @Override
        public void visit(Tree.ImportMemberOrType that) {
            if (hasErrors(that)) return;
            Unit u = that.getImportModel().getDeclaration().getUnit();
            if ((u.getFilename() != null && !u.getFilename().endsWith(".ceylon"))
                    || u.getPackage().getModule().isJava()) {
                that.addUnexpectedError("cannot import Java declarations in Javascript");
            }
            super.visit(that);
        }
        @Override
        public void visit(Tree.ImportModule that) {
            if (hasErrors(that)) return;
            if (((Module)that.getImportPath().getModel()).isJava()) {
                that.addUnexpectedError("cannot import Java modules in Javascript");
            }
            super.visit(that);
        }
        @Override
        public void visit(Tree.BaseMemberOrTypeExpression that) {
            if (hasErrors(that)) return;
            if (that.getDeclaration() != null && that.getDeclaration().getUnit() != null) {
                Unit u = that.getDeclaration().getUnit();
                if ((u.getFilename() != null && !u.getFilename().endsWith(".ceylon"))
                        || (u.getPackage() != null && u.getPackage().getModule() != null && u.getPackage().getModule().isJava())) {
                    that.addUnexpectedError("cannot call Java declarations in Javascript");
                }
            }
            super.visit(that);
        }
        @Override
        public void visit(Tree.QualifiedMemberOrTypeExpression that) {
            if (hasErrors(that)) return;
            if (that.getDeclaration() != null && that.getDeclaration().getUnit() != null) {
                Unit u = that.getDeclaration().getUnit();
                if ((u.getFilename() != null && !u.getFilename().endsWith(".ceylon"))
                        || (u.getPackage() != null && u.getPackage().getModule() != null && u.getPackage().getModule().isJava())) {
                    that.addUnexpectedError("cannot call Java declarations in Javascript");
                }
            }
            super.visit(that);
        }
    };

    public JsCompiler(TypeChecker tc, Options options) {
        this.tc = tc;
        opts = options;
        outRepo = CeylonUtils.repoManager()
                .outRepo(options.getOutDir())
                .user(options.getUser())
                .password(options.getPass())
                .buildOutputManager();
        String outDir = options.getOutDir();<commit_msg>improve reporting of backend-specific problems<commit_after>package com.redhat.ceylon.compiler.js;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.io.Writer;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import net.minidev.json.JSONObject;

import com.redhat.ceylon.cmr.api.ArtifactContext;
import com.redhat.ceylon.cmr.api.RepositoryManager;
import com.redhat.ceylon.cmr.api.SourceArchiveCreator;
import com.redhat.ceylon.cmr.ceylon.CeylonUtils;
import com.redhat.ceylon.cmr.impl.JULLogger;
import com.redhat.ceylon.cmr.impl.ShaSigner;
import com.redhat.ceylon.compiler.Options;
import com.redhat.ceylon.compiler.typechecker.TypeChecker;
import com.redhat.ceylon.compiler.typechecker.context.PhasedUnit;
import com.redhat.ceylon.compiler.typechecker.tree.AnalysisMessage;
import com.redhat.ceylon.compiler.typechecker.analyzer.AnalysisWarning;
import com.redhat.ceylon.compiler.typechecker.analyzer.AnalysisError;
import com.redhat.ceylon.compiler.typechecker.analyzer.UsageWarning;
import com.redhat.ceylon.compiler.typechecker.model.Module;
import com.redhat.ceylon.compiler.typechecker.model.TypedDeclaration;
import com.redhat.ceylon.compiler.typechecker.model.Unit;
import com.redhat.ceylon.compiler.typechecker.parser.RecognitionError;
import com.redhat.ceylon.compiler.typechecker.tree.Message;
import com.redhat.ceylon.compiler.typechecker.tree.Node;
import com.redhat.ceylon.compiler.typechecker.tree.Tree;
import com.redhat.ceylon.compiler.typechecker.tree.Visitor;

public class JsCompiler {

    protected final TypeChecker tc;
    protected final Options opts;
    protected final RepositoryManager outRepo;

    private boolean stopOnErrors = true;
    private int errCount = 0;

    protected Set<Message> errors = new HashSet<Message>();
    protected Set<Message> unitErrors = new HashSet<Message>();
    protected List<String> files;
    private final Map<Module, JsOutput> output = new HashMap<Module, JsOutput>();
    //You have to manually set this when compiling the language module
    static boolean compilingLanguageModule;
    private TypeUtils types;

    private final Visitor unitVisitor = new Visitor() {
        private boolean hasErrors(Node that) {
            for (Message m: that.getErrors()) {
                if (m instanceof AnalysisError) {
                    return true;
                }
            }
            return false;
        }
        @Override
        public void visitAny(Node that) {
            super.visitAny(that);
            for (Message err: that.getErrors()) {
                unitErrors.add(err);
            }
        }
        @Override
        public void visit(Tree.ImportMemberOrType that) {
            if (hasErrors(that)) return;
            Unit u = that.getImportModel().getDeclaration().getUnit();
            if ((u.getFilename() != null && !u.getFilename().endsWith(".ceylon"))
                    || u.getPackage().getModule().isJava()) {
                that.addError("cannot import Java declarations in Javascript");
            }
            super.visit(that);
        }
        @Override
        public void visit(Tree.ImportModule that) {
            if (hasErrors(that)) return;
            if (((Module)that.getImportPath().getModel()).isJava()) {
                that.getImportPath().addError("cannot import Java modules in Javascript");
            }
            super.visit(that);
        }
        @Override
        public void visit(Tree.BaseMemberOrTypeExpression that) {
            if (hasErrors(that)) return;
            if (that.getDeclaration() != null && that.getDeclaration().getUnit() != null) {
                Unit u = that.getDeclaration().getUnit();
                if ((u.getFilename() != null && !u.getFilename().endsWith(".ceylon"))
                        || (u.getPackage() != null && u.getPackage().getModule() != null && u.getPackage().getModule().isJava())) {
                    that.addError("cannot call Java declarations in Javascript");
                }
            }
            super.visit(that);
        }
        @Override
        public void visit(Tree.QualifiedMemberOrTypeExpression that) {
            if (hasErrors(that)) return;
            if (that.getDeclaration() != null && that.getDeclaration().getUnit() != null) {
                Unit u = that.getDeclaration().getUnit();
                if ((u.getFilename() != null && !u.getFilename().endsWith(".ceylon"))
                        || (u.getPackage() != null && u.getPackage().getModule() != null && u.getPackage().getModule().isJava())) {
                    that.addError("cannot call Java declarations in Javascript");
                }
            }
            super.visit(that);
        }
    };

    public JsCompiler(TypeChecker tc, Options options) {
        this.tc = tc;
        opts = options;
        outRepo = CeylonUtils.repoManager()
                .outRepo(options.getOutDir())
                .user(options.getUser())
                .password(options.getPass())
                .buildOutputManager();
        String outDir = options.getOutDir();
{'fraction_non_alphanumeric': 0.11785747905488092, 'fraction_numerical': 0.00018827073331450627, 'mean_word_length': 2.6222297988407774, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '675860', 'n_tokens_mistral': 2999, 'n_tokens_neox': 2922, 'n_words': 583}