Git Product home page Git Product logo

Comments (1)

grncdr avatar grncdr commented on July 23, 2024 1

We are seeing the same thing in our project, and I believe I've tracked it down to a mismatch in expected return types in this try/catch block:

try {
if (!formatRaw) {
formattedExpression = embedTextToDoc(expression, { ...options, parser: 'ruby' });
formattedExpression.parts.pop(); // removes newline at the end
}
} catch (error) {
formatRaw = true;
}

What happens is embedTextToDoc(...) returns a string instead of a Doc, so the formattedExpression.parts.pop() always fails. However the `formattedExpression variable has now been overwritten.

In the case where embedTextToDoc added line breaks to a long line, this leads to a mismatch between formattedExpression and expression; formattedExpression is a multi-line expression, but expression is not, so indentedLines ends up as a single element array in this block:

if (formatRaw && formattedExpression.trim().match(/\r?\n/)) {
let indentedLines = expression.split(/\r?\n/);
// The first line isn't empty. `<% smt`
if (indentedLines[0] && indentedLines[0].trim()) {
indentedLines = indentedLines.slice(1);
}
const spacesNumber = indentedLines
.filter((s) => s)
.map((s) => s.match(/^\s+/))
.map((s) => (s === null ? 0 : s[0].length));
const minSpacesNumber = Math.min(...spacesNumber);
const spaces = ' '.repeat(minSpacesNumber);
formattedExpression = formattedExpression

That leads to an empty spacesNumber array, a call to Math.min() //=> Infinity and the error seen above.


Here's an expression that can be used to trigger that behaviour:

<%= call_a_method :any_method, "it really does not matter which one", as_long_as(the_line: "is very very long"), it_will: "fail" %>

As for fixing it, replacing line 22 with formattedExpression = formattedExpression.trim() works for me.

from prettier-plugin-erb.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.