WASHINGTON — The Supreme Court’s conservatives decided Thursday that federal courts do not have a role to play in deciding whether partisan gerrymandering goes too far, giving a dominant political party in a state leeway to draw electoral maps that preserve or even expand its power.

The 5-to-4 decision was written by Chief Justice John Roberts and joined by the court’s other conservatives.

“We conclude that partisan gerrymandering claims present political questions beyond the reach of the federal courts,” Roberts wrote. “Federal judges have no license to reallocate political power between the two major political parties, with no plausible grant of authority in the Constitution, and no legal standards to limit and direct their decisions.”

The action puts a stop to recent decisions by federal courts across the country that have found extreme partisan gerrymandering went so far as to violate the constitutional rights of voters.

While both parties take advantage of drawing electoral districts when they have control in a state, the most recent beneficiaries have been Republicans. The GOP is in control of both the governorship and legislature in 22 states, compared to 14 for Democrats.

The recent decisions striking down Republican gerrymanders have come in battleground states such as Wisconsin, Ohio and Michigan.

The court was considering a Republican-drawn map in North Carolina and a Democratic gerrymander in Maryland. The maps, which have provided a 10-3 GOP advantage in congressional districts in the former, and a 7-1 Democratic advantage in the latter, will stand until they are redrawn after the 2020 Census.

“Excessive partisanship in districting leads to results that reasonably seem unjust,” wrote Roberts. “But the fact that such gerrymandering is incompatible with democratic principles does not mean that the solution lies with the federal judiciary.”

He was joined by Justices Clarence Thomas, Samuel Alito, Neil Gorsuch and Brett Kavanaugh.

Justice Elena Kagan dissented for the court’s liberals. “For the first time ever, this court refuses to remedy a constitutional violation because it thinks the task beyond judicial capabilities,” she wrote.

Kagan underscored her disagreement by reading — at times, emotionally — a lengthy excerpt of her dissent from the bench.

“The gerrymanders here — and others like them — violated the constitutional rights of many hundreds of thousands of American citizens,” she said.

“The practices challenged in these cases imperil our system of government. Part of the court’s role in that system is to defend its foundations. None is more important than free and fair elections.”

While the Supreme Court regularly scrutinizes electoral districts for racial gerrymandering, the justices have never found a state’s redistricting map so infected with politics that it violates the Constitution. Such a decision would have marked a dramatic change for how the nation’s political maps are drawn.

The court passed up the chance last term to settle the issue of whether courts have a role in policing partisan gerrymandering, sending back on technical rulings challenges to a Republican-drawn plan in Wisconsin and the challenged Maryland map.

There’s been less reticence outside the Supreme Court. With recent decisions in Ohio and Michigan, federal courts in five states have struck down maps as partisan gerrymanders. And last fall, voters in Michigan, Ohio, Colorado, Missouri and Utah either took redistricting away from politicians or limited their power.

In June 2018, the justices said the Maryland case was not ready for them and sent it back.

In November, a unanimous three-judge panel found that Democrats had unconstitutionally targeted Republican voters in the 6th Congressional District. The legislature had redrawn the district to take in Democratic strongholds in the Washington suburbs.

After the 2011 redistricting, a Democrat won the seat previously held by a Republican. There was an open election in the district in November, when Democrat David Trone defeated Republican Amie Hoeber by a wide margin.

“The massive and unnecessary reshuffling of the Sixth District, involving one-half of its population and dictated by party affiliation and voting history, had no other cause than the intended actions of the controlling Democratic officials to burden Republican voters by converting the district” into a Democratic one, wrote Judge Paul Niemeyer of the U.S. Court of Appeals for the 4th Circuit.

Rather than redraw the map, as the federal judges had ordered, Democratic Attorney General Brian Frosh decided to appeal to the Supreme Court. That put him at odds with the state’s Republican Gov. Larry Hogan.

Hogan called the court’s ruling “terribly disappointing to all who believe in fair elections.”

The Supreme Court had also sent back the North Carolina case last term, telling a panel of three federal judges to decide whether challengers in that state had the legal standing to bring the challenge.

The judges said they did, and also found that the legislature’s efforts violated constitutional protections of equal protection and free speech.