Skip to content

Fix noMethodSetState url's #2078

@JBallin

Description

@JBallin

@Arcanemagus's docsUrl commit didn't account for the rule naming conventions of the different method names in /lib/util/makeNoMethodSetStateRule.

Currently componentWillUpdate links to /docs/rules/componentWillUpdate (doesn't exist, 404) when it should be linking to /docs/rules/no-will-update-set-state:

function makeNoMethodSetStateRule(methodName, shouldCheckUnsafeCb) {
  return {
    meta: {
      docs: {
        description: `Prevent usage of setState in ${methodName}`,
        category: 'Best Practices',
        recommended: false,
        url: docsUrl(methodName)
      },

I'm proposing to create a function that gets the proper url within /lib/util/makeNoMethodSetStateRule.

I have three suggestions to propose, and wanted feedback prior to opening a PR. Do you recommend placing this function at the top, under the 'Rule Definition' header? And then I would call the function directly within the docs object: url: docsUrl(getDocsTitle(methodName))

function getDocsTitle1(methodName) {
  let title;
  switch (methodName) {
    case 'componentDidMount':
      title = 'did-mount';
      break;
    case 'componentDidUpdate':
      title = 'did-update';
      break;
    case 'componentWillUpdate':
      title = 'will-update';
      break;
    default:
      throw Error(`No docsUrl for '${methodName}'`);
  }
  return `no-${title}-set-state`;
}

function getDocsTitle2(methodName) {
  return `${methodName.match(/[A-Z][a-z]+/g)
    .reduce((res, e) => `${res}-${e.toLowerCase()}`, 'no')}-set-state`;
}

function getDocsTitle3(methodName) {
  return methodName.match(/[A-Z][a-z]+/g)
    .reduce((x, y) => `no-${x.toLowerCase()}-${y.toLowerCase()}-set-state`);
}

function getDocsTitle4(methodName) {
  const map = {
    componentDidMount: 'did-mount',
    componentDidUpdate: 'did-update',
    componentWillUpdate: 'will-update',
  };
  const title = map[methodName];
  if (!title) throw Error(`No docsUrl for '${methodName}'`);
  return `no-${title}-set-state`;
}

console.log(getDocsTitle1('componentWillUpdate')); // no-will-update-set-state
console.log(getDocsTitle2('componentWillUpdate')); // no-will-update-set-state
console.log(getDocsTitle3('componentWillUpdate')); // no-will-update-set-state
console.log(getDocsTitle4('componentWillUpdate')); // no-will-update-set-state

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions