Skip to content

Commit

Permalink
Stricter node type check (#14165)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Jan 13, 2023
1 parent c1b9765 commit 06ea1ac
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
9 changes: 4 additions & 5 deletions src/language-js/print/jsx.js
Expand Up @@ -25,7 +25,6 @@ const { getLast, getPreferredQuote } = require("../../common/util.js");
const {
isJsxNode,
rawText,
isLiteral,
isCallExpression,
isStringLiteral,
isBinaryish,
Expand Down Expand Up @@ -279,7 +278,7 @@ function printJsxChildren(
const parts = [];
path.each((childPath, i, children) => {
const child = childPath.getValue();
if (isLiteral(child)) {
if (child.type === "JSXText") {
const text = rawText(child);

// Contains a non-whitespace character
Expand Down Expand Up @@ -812,7 +811,7 @@ function isEmptyJsxElement(node) {
// if there is one text child and does not contain any meaningful text
// we can treat the element as empty.
const child = node.children[0];
return isLiteral(child) && !isMeaningfulJsxText(child);
return child.type === "JSXText" && !isMeaningfulJsxText(child);
}

// Meaningful if it contains non-whitespace characters,
Expand All @@ -823,7 +822,7 @@ function isEmptyJsxElement(node) {
*/
function isMeaningfulJsxText(node) {
return (
isLiteral(node) &&
node.type === "JSXText" &&
(containsNonJsxWhitespaceRegex.test(rawText(node)) ||
!/\n/.test(rawText(node)))
);
Expand All @@ -833,7 +832,7 @@ function isMeaningfulJsxText(node) {
function isJsxWhitespaceExpression(node) {
return (
node.type === "JSXExpressionContainer" &&
isLiteral(node.expression) &&
isStringLiteral(node.expression) &&
node.expression.value === " " &&
!hasComment(node.expression)
);
Expand Down
4 changes: 2 additions & 2 deletions src/language-js/print/typescript.js
Expand Up @@ -15,7 +15,7 @@ const {
},
} = require("../../document/index.js");
const {
isLiteral,
isStringLiteral,
getTypeScriptMappedTypeModifier,
shouldPrintComma,
isCallExpression,
Expand Down Expand Up @@ -449,7 +449,7 @@ function printTypescript(path, options, print) {
return ["require(", print("expression"), ")"];
case "TSModuleDeclaration": {
const parent = path.getParentNode();
const isExternalModule = isLiteral(node.id);
const isExternalModule = isStringLiteral(node.id);
const parentIsDeclaration = parent.type === "TSModuleDeclaration";
const bodyIsDeclaration =
node.body && node.body.type === "TSModuleDeclaration";
Expand Down

0 comments on commit 06ea1ac

Please sign in to comment.