Skip to content

Commit

Permalink
Favor function declarations over assignment
Browse files Browse the repository at this point in the history
Helps avoid this pattern sneaking back in.
  • Loading branch information
sebmarkbage committed Mar 15, 2023
1 parent f2367ee commit 3127265
Show file tree
Hide file tree
Showing 17 changed files with 228 additions and 222 deletions.
2 changes: 1 addition & 1 deletion packages/dom-event-testing-library/domEnvironment.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* Change environment support for PointerEvent.
*/

const emptyFunction = function () {};
function emptyFunction() {}

export function hasPointerEvent() {
return global != null && global.PointerEvent != null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/**
* Create a function which has 'unsafe' privileges (required by windows8 apps)
*/
const createMicrosoftUnsafeLocalFunction = function (func) {
function createMicrosoftUnsafeLocalFunction(func) {
if (typeof MSApp !== 'undefined' && MSApp.execUnsafeLocalFunction) {
return function (arg0, arg1, arg2, arg3) {
MSApp.execUnsafeLocalFunction(function () {
Expand All @@ -20,6 +20,6 @@ const createMicrosoftUnsafeLocalFunction = function (func) {
} else {
return func;
}
};
}

export default createMicrosoftUnsafeLocalFunction;
4 changes: 2 additions & 2 deletions packages/react-dom-bindings/src/client/setTextContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {TEXT_NODE} from './HTMLNodeType';
* @param {string} text
* @internal
*/
const setTextContent = function (node: Element, text: string): void {
function setTextContent(node: Element, text: string): void {
if (text) {
const firstChild = node.firstChild;

Expand All @@ -32,6 +32,6 @@ const setTextContent = function (node: Element, text: string): void {
}
}
node.textContent = text;
};
}

export default setTextContent;
257 changes: 129 additions & 128 deletions packages/react-dom-bindings/src/client/validateDOMNesting.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@
* @flow
*/

type ValidateDOMNesting = (?string, ?string, AncestorInfoDev) => void;
let validateDOMNesting: ValidateDOMNesting = (() => {}: any);

type UpdatedAncestorInfoDev = (?AncestorInfoDev, string) => AncestorInfoDev;
let updatedAncestorInfoDev: UpdatedAncestorInfoDev = (() => {}: any);

type Info = {tag: string};
export type AncestorInfoDev = {
current: ?Info,
Expand Down Expand Up @@ -178,70 +172,74 @@ const emptyAncestorInfoDev: AncestorInfoDev = {
containerTagInScope: null,
};

function updatedAncestorInfoDev(oldInfo: ?AncestorInfoDev, tag: string) {
const ancestorInfo = {...(oldInfo || emptyAncestorInfoDev)};
const info = {tag};
function updatedAncestorInfoDev(
oldInfo: ?AncestorInfoDev,
tag: string,
): AncestorInfoDev {
if (__DEV__) {
const ancestorInfo = {...(oldInfo || emptyAncestorInfoDev)};
const info = {tag};

if (inScopeTags.indexOf(tag) !== -1) {
ancestorInfo.aTagInScope = null;
ancestorInfo.buttonTagInScope = null;
ancestorInfo.nobrTagInScope = null;
}
if (buttonScopeTags.indexOf(tag) !== -1) {
ancestorInfo.pTagInButtonScope = null;
}

if (inScopeTags.indexOf(tag) !== -1) {
ancestorInfo.aTagInScope = null;
ancestorInfo.buttonTagInScope = null;
ancestorInfo.nobrTagInScope = null;
}
if (buttonScopeTags.indexOf(tag) !== -1) {
ancestorInfo.pTagInButtonScope = null;
}
// See rules for 'li', 'dd', 'dt' start tags in
// https://html.spec.whatwg.org/multipage/syntax.html#parsing-main-inbody
if (
specialTags.indexOf(tag) !== -1 &&
tag !== 'address' &&
tag !== 'div' &&
tag !== 'p'
) {
ancestorInfo.listItemTagAutoclosing = null;
ancestorInfo.dlItemTagAutoclosing = null;
}

// See rules for 'li', 'dd', 'dt' start tags in
// https://html.spec.whatwg.org/multipage/syntax.html#parsing-main-inbody
if (
specialTags.indexOf(tag) !== -1 &&
tag !== 'address' &&
tag !== 'div' &&
tag !== 'p'
) {
ancestorInfo.listItemTagAutoclosing = null;
ancestorInfo.dlItemTagAutoclosing = null;
}
ancestorInfo.current = info;

ancestorInfo.current = info;
if (tag === 'form') {
ancestorInfo.formTag = info;
}
if (tag === 'a') {
ancestorInfo.aTagInScope = info;
}
if (tag === 'button') {
ancestorInfo.buttonTagInScope = info;
}
if (tag === 'nobr') {
ancestorInfo.nobrTagInScope = info;
}
if (tag === 'p') {
ancestorInfo.pTagInButtonScope = info;
}
if (tag === 'li') {
ancestorInfo.listItemTagAutoclosing = info;
}
if (tag === 'dd' || tag === 'dt') {
ancestorInfo.dlItemTagAutoclosing = info;
}
if (tag === '#document' || tag === 'html') {
ancestorInfo.containerTagInScope = null;
} else if (!ancestorInfo.containerTagInScope) {
ancestorInfo.containerTagInScope = info;
}

if (tag === 'form') {
ancestorInfo.formTag = info;
}
if (tag === 'a') {
ancestorInfo.aTagInScope = info;
}
if (tag === 'button') {
ancestorInfo.buttonTagInScope = info;
}
if (tag === 'nobr') {
ancestorInfo.nobrTagInScope = info;
}
if (tag === 'p') {
ancestorInfo.pTagInButtonScope = info;
}
if (tag === 'li') {
ancestorInfo.listItemTagAutoclosing = info;
}
if (tag === 'dd' || tag === 'dt') {
ancestorInfo.dlItemTagAutoclosing = info;
}
if (tag === '#document' || tag === 'html') {
ancestorInfo.containerTagInScope = null;
} else if (!ancestorInfo.containerTagInScope) {
ancestorInfo.containerTagInScope = info;
return ancestorInfo;
} else {
return (null: any);
}

return ancestorInfo;
}

/**
* Returns whether
*/
const isTagValidWithParent = function (
tag: string,
parentTag: ?string,
): boolean {
function isTagValidWithParent(tag: string, parentTag: ?string): boolean {
// First, let's check if we're in an unusual parsing mode...
switch (parentTag) {
// https://html.spec.whatwg.org/multipage/syntax.html#parsing-main-inselect
Expand Down Expand Up @@ -361,12 +359,12 @@ const isTagValidWithParent = function (
}

return true;
};
}

/**
* Returns whether
*/
const findInvalidAncestorForTag = function (
function findInvalidAncestorForTag(
tag: string,
ancestorInfo: AncestorInfoDev,
): ?Info {
Expand Down Expand Up @@ -431,89 +429,92 @@ const findInvalidAncestorForTag = function (
}

return null;
};
}

const didWarn: {[string]: boolean} = {};

function validateDOMNesting(
childTag: ?string,
childText: ?string,
ancestorInfo: AncestorInfoDev,
) {
ancestorInfo = ancestorInfo || emptyAncestorInfoDev;
const parentInfo = ancestorInfo.current;
const parentTag = parentInfo && parentInfo.tag;

if (childText != null) {
if (childTag != null) {
): void {
if (__DEV__) {
ancestorInfo = ancestorInfo || emptyAncestorInfoDev;
const parentInfo = ancestorInfo.current;
const parentTag = parentInfo && parentInfo.tag;

if (childText != null) {
if (childTag != null) {
console.error(
'validateDOMNesting: when childText is passed, childTag should be null',
);
}
childTag = '#text';
} else if (childTag == null) {
console.error(
'validateDOMNesting: when childText is passed, childTag should be null',
'validateDOMNesting: when childText or childTag must be provided',
);
return;
}
childTag = '#text';
} else if (childTag == null) {
console.error(
'validateDOMNesting: when childText or childTag must be provided',
);
return;
}

const invalidParent = isTagValidWithParent(childTag, parentTag)
? null
: parentInfo;
const invalidAncestor = invalidParent
? null
: findInvalidAncestorForTag(childTag, ancestorInfo);
const invalidParentOrAncestor = invalidParent || invalidAncestor;
if (!invalidParentOrAncestor) {
return;
}

const ancestorTag = invalidParentOrAncestor.tag;
const invalidParent = isTagValidWithParent(childTag, parentTag)
? null
: parentInfo;
const invalidAncestor = invalidParent
? null
: findInvalidAncestorForTag(childTag, ancestorInfo);
const invalidParentOrAncestor = invalidParent || invalidAncestor;
if (!invalidParentOrAncestor) {
return;
}

const warnKey =
// eslint-disable-next-line react-internal/safe-string-coercion
String(!!invalidParent) + '|' + childTag + '|' + ancestorTag;
if (didWarn[warnKey]) {
return;
}
didWarn[warnKey] = true;
const ancestorTag = invalidParentOrAncestor.tag;

let tagDisplayName = childTag;
let whitespaceInfo = '';
if (childTag === '#text') {
if (childText != null && /\S/.test(childText)) {
tagDisplayName = 'Text nodes';
const warnKey =
// eslint-disable-next-line react-internal/safe-string-coercion
String(!!invalidParent) + '|' + childTag + '|' + ancestorTag;
if (didWarn[warnKey]) {
return;
}
didWarn[warnKey] = true;

let tagDisplayName = childTag;
let whitespaceInfo = '';
if (childTag === '#text') {
if (childText != null && /\S/.test(childText)) {
tagDisplayName = 'Text nodes';
} else {
tagDisplayName = 'Whitespace text nodes';
whitespaceInfo =
" Make sure you don't have any extra whitespace between tags on " +
'each line of your source code.';
}
} else {
tagDisplayName = 'Whitespace text nodes';
whitespaceInfo =
" Make sure you don't have any extra whitespace between tags on " +
'each line of your source code.';
tagDisplayName = '<' + childTag + '>';
}
} else {
tagDisplayName = '<' + childTag + '>';
}

if (invalidParent) {
let info = '';
if (ancestorTag === 'table' && childTag === 'tr') {
info +=
' Add a <tbody>, <thead> or <tfoot> to your code to match the DOM tree generated by ' +
'the browser.';
if (invalidParent) {
let info = '';
if (ancestorTag === 'table' && childTag === 'tr') {
info +=
' Add a <tbody>, <thead> or <tfoot> to your code to match the DOM tree generated by ' +
'the browser.';
}
console.error(
'validateDOMNesting(...): %s cannot appear as a child of <%s>.%s%s',
tagDisplayName,
ancestorTag,
whitespaceInfo,
info,
);
} else {
console.error(
'validateDOMNesting(...): %s cannot appear as a descendant of ' +
'<%s>.',
tagDisplayName,
ancestorTag,
);
}
console.error(
'validateDOMNesting(...): %s cannot appear as a child of <%s>.%s%s',
tagDisplayName,
ancestorTag,
whitespaceInfo,
info,
);
} else {
console.error(
'validateDOMNesting(...): %s cannot appear as a descendant of ' + '<%s>.',
tagDisplayName,
ancestorTag,
);
}
}

Expand Down
Loading

0 comments on commit 3127265

Please sign in to comment.