Sha256: 7e69b13568143498db88b1e83a0291ecf5b13edf35823fd9a95d1ab60f369c34

Contents?: true

Size: 1.49 KB

Versions: 16

Compression:

Stored size: 1.49 KB

Contents

const needsParens = ["args", "assign", "assoc_new", "massign", "opassign"];

// If you have a modifier statement (for instance an inline if statement or an
// inline while loop) there are times when you need to wrap the entire statement
// in parentheses. This occurs when you have something like:
//
//     foo[:foo] =
//       if bar?
//         baz
//       end
//
// Normally we would shorten this to an inline version, which would result in:
//
//     foo[:foo] = baz if bar?
//
// but this actually has different semantic meaning. The first example will
// result in a nil being inserted into the hash for the :foo key, whereas the
// second example will result in an empty hash because the if statement applies
// to the entire assignment.
//
// We can fix this in a couple of ways. We can use the then keyword, as in:
//
//     foo[:foo] = if bar? then baz end
//
// but I haven't actually seen this anywhere. We can also just leave it as is
// with the multi-line version, but for a short predicate and short value it
// looks pretty silly. The last option and the one I've selected here is to add
// parentheses on both sides of the expression, as in:
//
//     foo[:foo] = (baz if bar?)
//
// This approach maintains the nice conciseness of the inline version, while
// keeping the correct semantic meaning.
const inlineEnsureParens = (path, parts) => {
  if (needsParens.includes(path.getParentNode().type)) {
    return ["("].concat(parts, ")");
  }

  return parts;
};

module.exports = inlineEnsureParens;

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
prettier-1.5.0 src/utils/inlineEnsureParens.js
prettier-1.4.0 src/utils/inlineEnsureParens.js
prettier-1.3.0 src/utils/inlineEnsureParens.js
prettier-1.2.5 src/utils/inlineEnsureParens.js
prettier-1.2.4 src/utils/inlineEnsureParens.js
prettier-1.2.3 src/utils/inlineEnsureParens.js
prettier-1.2.2 src/utils/inlineEnsureParens.js
prettier-1.2.1 src/utils/inlineEnsureParens.js
prettier-1.2.0 src/utils/inlineEnsureParens.js
prettier-1.1.0 src/utils/inlineEnsureParens.js
prettier-1.0.1 src/utils/inlineEnsureParens.js
prettier-1.0.0 src/utils/inlineEnsureParens.js
prettier-1.0.0.pre.rc2 src/utils/inlineEnsureParens.js
prettier-1.0.0.pre.rc1 src/utils/inlineEnsureParens.js
prettier-0.22.0 src/utils/inlineEnsureParens.js
prettier-0.21.0 src/utils/inlineEnsureParens.js