Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: [a11y-input-has-name-attribute]Inputにreact-hook-formのregisterが指定されている場合はエラーにならないようにする #490

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
const JSON5 = require('json5');
const fs = require('fs');
const { generateTagFormatter } = require('../../libs/format_styled_components');

const OPTION = (() => {
const file = `${process.cwd()}/package.json`;

if (!fs.existsSync(file)) {
return {};
}

const json = JSON5.parse(fs.readFileSync(file));
const dependencies = [
...Object.keys(json.dependencies || {}),
...Object.keys(json.devDependencies || {}),
];

return {
react_hook_form: dependencies.includes('react-hook-form'),
};
})();

const EXPECTED_NAMES = {
'(i|I)nput$': 'Input$',
'(t|T)extarea$': 'Textarea$',
Expand All @@ -21,6 +41,7 @@ const RADIO_BUTTON_REGEX = /RadioButton(Panel)?$/
const findNameAttr = (a) => a?.name?.name === 'name'
const findSpreadAttr = (a) => a.type === 'JSXSpreadAttribute'
const findRadioInput = (a) => a.name?.name === 'type' && a.value.value === 'radio'
const findReactHookFormRegisterSpreadAttr = (a) => a.type === 'JSXSpreadAttribute' && a.argument?.callee?.name === 'register'

const MESSAGE_PART_FORMAT = `"${INPUT_NAME_REGEX.toString()}"にmatchするフォーマットで命名してください`
const MESSAGE_UNDEFINED_NAME_PART = `
Expand Down Expand Up @@ -56,20 +77,26 @@ module.exports = {
return {
...generateTagFormatter({ context, EXPECTED_NAMES }),
JSXOpeningElement: (node) => {
const nodeName = node.name.name || '';
const { name, attributes } = node;
const nodeName = name.name || '';

if (nodeName.match(TARGET_TAG_NAME_REGEX)) {
const nameAttr = node.attributes.find(findNameAttr)
const nameAttr = attributes.find(findNameAttr)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ロジック自体は大丈夫そうですが、attributesに対するチェックを最適化すると良いかも。
今の方法だと複数回attributesに対して操作が走る可能性があります。

let nameAttr = null
let hasSpreadAttr = false
let hasReactHookFormRegisterSpreadAttr = false
let hasRadioInput = false

attributes.forEach((attr) => {
  if (a.type === 'JSXSpreadAttribute') {
    hasSpreadAttr = true
    
    if (hasReactHookFormRegisterSpreadAttr == false && a.argument?.callee?.name === 'register') {
      hasReactHookFormRegisterSpreadAttr = true
    }
  } else {
    switch (a.name?.name) {
      case 'name': {
        nameAttr = a
        break
      }
      case 'type': {
        if (a.value.value === 'radio') {
          hasRadioInput = true
        }
        break
      }
    }
  }
})

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この方法ならattributesに対する検査は1loopだけで済むので高速化できます

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AtsushiM 添付のコードにあるhasSpreadAttrhasRadioInputにはbooleanを代入するだけでどこでも使われていないようなのですが、どういった意味があるのでしょうか?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yamakeeeeeeeeen
以下の箇所で都度someをしている箇所を置き換える想定です。
https://github.com/kufu/tamatebako/pull/490/files#diff-9bc6cdcc4ece3aff163a028a8962d667e77b90463af87f220e4464d8e4f9e3bbR90
https://github.com/kufu/tamatebako/pull/490/files#diff-9bc6cdcc4ece3aff163a028a8962d667e77b90463af87f220e4464d8e4f9e3bbR99

現在のロジックではattributes配列に対して、最大3回ループする可能性があり、処理が重いので1oopですべての計算を済ませてしまえば軽くなるよね!という感じの対応です

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

もともと nameAttr を生成するためにattributesを一度はループする必要があるので、そのついでに他の計算もしてしまおう、という発想です


if (!nameAttr) {
if (
node.attributes.length === 0 ||
checkType !== 'allow-spread-attributes' ||
!node.attributes.some(findSpreadAttr)
!(
OPTION.react_hook_form &&
attributes.some(findReactHookFormRegisterSpreadAttr)
) &&
(attributes.length === 0 ||
checkType !== 'allow-spread-attributes' ||
!attributes.some(findSpreadAttr))
) {
const isRadio =
nodeName.match(RADIO_BUTTON_REGEX) ||
(nodeName.match(INPUT_TAG_REGEX) && node.attributes.some(findRadioInput));
(nodeName.match(INPUT_TAG_REGEX) &&
attributes.some(findRadioInput));

context.report({
node,
Expand Down
Loading