Skip to content

Commit

Permalink
refactor: Add isObjectProperty util function (#10942)
Browse files Browse the repository at this point in the history
* Add isObjectProperty

* Rename to isProperty

* Address review

* Address review2

* Make isProperty strict

* Rename to isObjectProperty
  • Loading branch information
sosukesuzuki committed May 25, 2021
1 parent 7073520 commit f1e0f63
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 20 deletions.
11 changes: 3 additions & 8 deletions src/language-js/comments.js
Expand Up @@ -24,6 +24,7 @@ const {
getCallArguments,
isCallExpression,
isMemberExpression,
isObjectProperty,
} = require("./utils");
const { locStart, locEnd } = require("./loc");

Expand Down Expand Up @@ -360,9 +361,7 @@ function handleObjectPropertyAssignment({
enclosingNode,
}) {
if (
enclosingNode &&
(enclosingNode.type === "ObjectProperty" ||
enclosingNode.type === "Property") &&
isObjectProperty(enclosingNode) &&
enclosingNode.shorthand &&
enclosingNode.key === precedingNode &&
enclosingNode.value.type === "AssignmentPattern"
Expand Down Expand Up @@ -689,11 +688,7 @@ function handleUnionTypeComments({
}

function handlePropertyComments({ comment, enclosingNode }) {
if (
enclosingNode &&
(enclosingNode.type === "Property" ||
enclosingNode.type === "ObjectProperty")
) {
if (isObjectProperty(enclosingNode)) {
addLeadingComment(enclosingNode, comment);
return true;
}
Expand Down
6 changes: 3 additions & 3 deletions src/language-js/embed.js
@@ -1,6 +1,6 @@
"use strict";

const { hasComment, CommentCheckFlags } = require("./utils");
const { hasComment, CommentCheckFlags, isObjectProperty } = require("./utils");
const formatMarkdown = require("./embed/markdown");
const formatCss = require("./embed/css");
const formatGraphql = require("./embed/graphql");
Expand Down Expand Up @@ -135,7 +135,7 @@ function isAngularComponentStyles(path) {
(node) => node.type === "TemplateLiteral",
(node, name) => node.type === "ArrayExpression" && name === "elements",
(node, name) =>
(node.type === "Property" || node.type === "ObjectProperty") &&
isObjectProperty(node) &&
node.key.type === "Identifier" &&
node.key.name === "styles" &&
name === "value",
Expand All @@ -146,7 +146,7 @@ function isAngularComponentTemplate(path) {
return path.match(
(node) => node.type === "TemplateLiteral",
(node, name) =>
(node.type === "Property" || node.type === "ObjectProperty") &&
isObjectProperty(node) &&
node.key.type === "Identifier" &&
node.key.name === "template" &&
name === "value",
Expand Down
4 changes: 2 additions & 2 deletions src/language-js/needs-parens.js
Expand Up @@ -13,6 +13,7 @@ const {
getPrecedence,
isCallExpression,
isMemberExpression,
isObjectProperty,
} = require("./utils");

function needsParens(path, options) {
Expand Down Expand Up @@ -762,9 +763,8 @@ function needsParens(path, options) {
parent.type !== "JSXExpressionContainer" &&
parent.type !== "JSXFragment" &&
parent.type !== "LogicalExpression" &&
parent.type !== "ObjectProperty" &&
!isCallExpression(parent) &&
parent.type !== "Property" &&
!isObjectProperty(parent) &&
parent.type !== "ReturnStatement" &&
parent.type !== "ThrowStatement" &&
parent.type !== "TypeCastExpression" &&
Expand Down
6 changes: 3 additions & 3 deletions src/language-js/print/assignment.js
Expand Up @@ -17,6 +17,7 @@ const {
rawText,
hasComment,
isSignedNumericLiteral,
isObjectProperty,
} = require("../utils");
const { shouldInlineLogicalExpression } = require("./binaryish");
const { printCallExpression } = require("./call-expression");
Expand Down Expand Up @@ -229,8 +230,7 @@ function isComplexDestructuring(node) {
leftNode.properties.length > 2 &&
leftNode.properties.some(
(property) =>
(property.type === "ObjectProperty" ||
property.type === "Property") &&
isObjectProperty(property) &&
(!property.shorthand ||
(property.value && property.value.type === "AssignmentPattern"))
)
Expand Down Expand Up @@ -396,7 +396,7 @@ function isLoneShortArgument(node, { printWidth }) {
}

function isObjectPropertyWithShortKey(node, keyDoc, options) {
if (node.type !== "ObjectProperty" && node.type !== "Property") {
if (!isObjectProperty(node)) {
return false;
}
// TODO: for performance, it might make sense to use a more lightweight
Expand Down
4 changes: 2 additions & 2 deletions src/language-js/print/binaryish.js
Expand Up @@ -24,6 +24,7 @@ const {
CommentCheckFlags,
isCallExpression,
isMemberExpression,
isObjectProperty,
} = require("../utils");

/** @typedef {import("../../document").Doc} Doc */
Expand Down Expand Up @@ -106,8 +107,7 @@ function printBinaryishExpression(path, options, print) {
parent.type === "PropertyDefinition" ||
parent.type === "TSAbstractClassProperty" ||
parent.type === "ClassPrivateProperty" ||
parent.type === "ObjectProperty" ||
parent.type === "Property";
isObjectProperty(parent);

const samePrecedenceSubExpression =
isBinaryish(node.left) && shouldFlatten(node.operator, node.left.operator);
Expand Down
4 changes: 2 additions & 2 deletions src/language-js/print/call-arguments.js
Expand Up @@ -15,6 +15,7 @@ const {
isNextLineEmpty,
isCallExpression,
isStringLiteral,
isObjectProperty,
} = require("../utils");

const {
Expand Down Expand Up @@ -294,8 +295,7 @@ function isTypeModuleObjectExpression(node) {
return (
node.type === "ObjectExpression" &&
node.properties.length === 1 &&
(node.properties[0].type === "ObjectProperty" ||
node.properties[0].type === "Property") &&
isObjectProperty(node.properties[0]) &&
node.properties[0].key.type === "Identifier" &&
node.properties[0].key.name === "type" &&
isStringLiteral(node.properties[0].value) &&
Expand Down
9 changes: 9 additions & 0 deletions src/language-js/utils.js
Expand Up @@ -1320,6 +1320,14 @@ function isCallLikeExpression(node) {
);
}

function isObjectProperty(node) {
return (
node &&
(node.type === "ObjectProperty" ||
(node.type === "Property" && !node.method && node.kind === "init"))
);
}

module.exports = {
getFunctionParameters,
iterateFunctionParametersPath,
Expand Down Expand Up @@ -1358,6 +1366,7 @@ module.exports = {
isMemberish,
isNumericLiteral,
isSignedNumericLiteral,
isObjectProperty,
isObjectType,
isObjectTypePropertyAFunction,
isSimpleType,
Expand Down

0 comments on commit f1e0f63

Please sign in to comment.