The EU’s top court has told the home secretary, Theresa May, she cannot deport a Moroccan mother with a British-born son simply because she has a criminal record.

The advocate general of the European court of justice has told May that it will be contrary to EU law if she automatically expels or refuses a residence permit to a non-EU national with a criminal record who is a parent of a child who is an EU citizen.

The preliminary opinion of the court’s advocate general, Maciej Szpunar, however, adds that while, in principle, deportation in such cases was contrary to EU law, he agreed with UK representations that there should be exceptional circumstances when a convicted criminal could still be deported depending on the seriousness of the offences involved.

The intervention by the EU’s most senior court is likely to be taken by Eurosceptic campaigners as evidence of unwarranted interference by Europe in the powers of the British home secretary to deport convicted foreign criminals even if it does allow her to press ahead with the Moroccan woman’s deportation.

The advocate general’s opinion follows a request from British judges on the immigration and asylum tribunal in London for a EU court of justice ruling on the effect a criminal record may have on the recognition of a right of residence under EU law.

The EU legal opinion applies to two deportation cases. The first case involves a Moroccan woman, known for legal reasons as CS, who became liable for deportation after serving a 12-month prison sentence. In August 2012 she was informed that she was liable to be deported.

She has a four-year-old son born in 2011 as a result of her marriage to a British citizen, but following her divorce now has sole care and custody of the child and has told the British courts there is no one else to care for her son.

The second case involes a Colombian man, Rendon Marin, who has two Spanish-born children and who faces expulsion from Spain after being given a nine-month prison sentence suspended for two years.

In both cases the European court says that the rights of the children involved as EU citizens must take priority. “In the cases under consideration the children could be obliged to go with their respective parents if the latter are expelled, given that they have been entrusted to the sole care of those parents,” says the advocate general’s opinion.

“The children would then have to leave the territory of the EU, which would however frustrate the actual enjoyment of the substance of the rights conferred on them by their status as citizens of the EU.”

The EU court did however agree that there should be a “public policy or public security” exemption as invoked in the case of the Moroccan mother to justify her deportation.

“CS’s serious criminal offences represent an obvious threat to the preservation of that member state’s social cohesion and the values of its society, which is a legitimate interest. The advocate general considers that expulsion, is in principle, contrary to EU law but that, in exceptional circumstances, such a measure may be adopted,” says an EU court of justice press release on the case.