SACRAMENTO — California lawmakers on Thursday gave final approval to a bill that would place strict limits on the disclosure of a person’s immigration status in open court, continuing the sanctuary state’s rebellion against the Trump administration’s illegal immigration crackdown.

Senate Bill 785 was introduced in response to news reports of ICE agents tracking down undocumented immigrants in courthouses across the country. It takes aim at a tactic that advocates say is keeping many immigrants from testifying in court, reporting crimes or simply showing up to pay a ticket.

“This is about protecting public safety,” said Assemblywoman Gonzalez Fletcher, D-San Diego, one of the bill’s authors, in a statement Thursday. “Our criminal-justice system can’t function if witnesses or victims are afraid to testify out of fear of being deported. You should be able to testify against a murderer or rapist without fearing that you or your loved ones will be thrown out of the country as a result.”

A spokesman with U.S. Immigration and Customs Enforcement said the agency doesn’t comment on pending legislation.

The bill, introduced last year by Sen. Scott Wiener, D-San Francisco, passed the Senate Thursday with a bipartisan vote of 31-6. Six of the 13 Senate Republicans voted for the proposal and one did not vote.

If signed by Gov. Jerry Brown, it will take effect immediately.

“Courts need to be safe zones, but with rhetoric coming out of Washington, D.C., that demonizes immigrants and threatens mass deportations, entering a courtroom is more daunting than ever. Public safety is suffering as a result,” wrote Wiener in an editorial for the Sacramento Bee Monday with San Francisco District Attorney George Gascón, who sponsored the measure.

Wiener’s office said some attorneys are revealing the immigration status of victims or witnesses who come forward to participate in court cases, even when it’s not relevant, creating a “chilling effect” that can prevent others from coming forward.

In one case, a San Francisco mother who testified in court against her daughter’s alleged abuser was questioned about her immigration status and whether she’d been motivated to testify against the defendant in order to secure a special type of visa granted to people who cooperate with law enforcement, according to Wiener and Gascón. A judge ruled the woman’s immigration status was irrelevant to the case and couldn’t be considered by the jury, but the jury was unable to reach a verdict. When prosecutors tried to retry the case, the victim’s mother declined to testify a second time, in part because of the fact that her immigration status had been discussed, they said.

In a 2017 letter to U.S. Attorney General and former Department of Homeland Security John Kelly, California Chief Justice Tani G. Cantil-Sakauye expressed concern over reports that ICE agents are “stalking undocumented immigrants in our courthouses to make arrests” and asked immigration officials to keep enforcement tactics out of state courts.

“Our courts are the main point of contact for millions of the most vulnerable Californians in times of anxiety, stress and crises in their lives,” she said.

“Enforcement policies that include stalking courthouses and arresting undocumented immigrants, the vast majority of whom pose no risk to public safety, are neither safe nor fair.”

Though they’d also be protected under the legislation, criminal defendants likely wouldn’t be affected, since a person’s immigration status isn’t typically a factor in criminal cases. “It’s usually excluded from testimony,” said San Jose State political scientist Garrick Percival, of a person’s legal status.

Immigration cases are handled separately, in federal court.

Immigration enforcement has been a cornerstone of President Donald Trump’s administration. Since he launched his campaign for president, Trump has invoked the killing of Kate Steinle by an undocumented immigrant with multiple criminal convictions and prior deportations as an example of a broken immigration system. A San Francisco jury found Jose Inez Garcia Zarate not guilty in a murder trial after his defense attorney showed the shooting was accidental. However, Trump and Attorney General Jeff Sessions have used the Steinle case to justify ongoing ICE crackdowns that have many illegal immigrants living in fear.

The only group on record opposing the bill was the California News Publishers Association, which argued it would hurt the public’s interest by making some proceedings secret, hindering reporting.

“By automatically moving all discussions of the immigration status of parties and witnesses to a judges’ chambers,” the association wrote in its letter of opposition, “SB 785 establishes a per se ban on the public’s right to attend this aspect of criminal and civil proceedings which the Supreme Court found to be unconstitutional.”

A directive from U.S. Immigration and Customs Enforcement released in January allows immigration agents to enter courthouses to arrest targeted convicted criminals but bars them from detaining anyone else who may be undocumented. Cantil-Sakauye said the policy is a step in the right direction, while some advocates said it still allows ICE to step into courthouses, possibly dissuading undocumented immigrants from showing up at all.

Percival said the debate brings up the issue of separation of powers and, “whether ICE agents coming into courts is an infringement on the judicial system’s ability to do its work.”

“This is a continuation of a real fight about the balance of power between the federal and state government,” he said. “Not only that, but also between the executive and judicial branches. There’s a lot still left to unfold.”