programing

eslint-config-airbnb가있는 확장자가 '.js'인 파일에서는 JSX를 사용할 수 없습니다.

nasanasas 2020. 10. 23. 07:58
반응형

eslint-config-airbnb가있는 확장자가 '.js'인 파일에서는 JSX를 사용할 수 없습니다.


React를 위해 ESLINT를 미리 구성해야하는 eslint-config-airbnb를 설치 했습니다 .

기본 내보내기에는 ECMAScript 6+ 및 React를 포함한 모든 ESLint 규칙이 포함됩니다. eslint, eslint-plugin-import, eslint-plugin-react 및 eslint-plugin-jsx-a11y가 필요합니다.

.eslintrc구성 확장 :

{ "extends": "eslint-config-airbnb",
  "env": {
    "browser": true,
    "node": true,
    "mocha": true
  },
  "rules": {
    "new-cap": [2, { "capIsNewExceptions": ["List", "Map", "Set"] }],
    "react/no-multi-comp": 0,
    "import/default": 0,
    "import/no-duplicates": 0,
    "import/named": 0,
    "import/namespace": 0,
    "import/no-unresolved": 0,
    "import/no-named-as-default": 2,
    "comma-dangle": 0,  // not sure why airbnb turned this on. gross!
    "indent": [2, 2, {"SwitchCase": 1}],
    "no-console": 0,
    "no-alert": 0,
    "linebreak-style": 0
  },
  "plugins": [
    "react", "import"
  ],
  "settings": {
    "import/parser": "babel-eslint",
    "import/resolve": {
      "moduleDirectory": ["node_modules", "src"]
    }
  },
  "globals": {
    "__DEVELOPMENT__": true,
    "__CLIENT__": true,
    "__SERVER__": true,
    "__DISABLE_SSR__": true,
    "__DEVTOOLS__": true,
    "socket": true,
    "webpackIsomorphicTools": true
  }
}

Unfortunatelly React JSX 코드가 포함 된 .js 파일을 linting 할 때 다음 오류가 발생합니다.

 error  JSX not allowed in files with extension '.js'              react/jsx-filename-extension

eslint-config-airbnb가 이미 언급했듯이 JSX를 지원하도록 반응하지 않았습니까?

이 오류를 제거하려면 어떻게해야합니까?


.jsx언급 한대로 파일 확장자를 변경 하거나 jsx-filename-extension 규칙을 비활성화하십시오 . 구성에 다음을 추가하여 .jsJSX 확장 을 허용 할 수 있습니다.

"rules": {
  "react/jsx-filename-extension": [1, { "extensions": [".js", ".jsx"] }],
}

그것은 나를위한 일입니다. 당신을 도울 수 있기를 바랍니다.

  1. 에서 jsx-filename-extension 비활성화 .eslintrc:

    "rules": { "react / jsx-filename-extension": [0]}

  2. 에서 webpack.config.js:

    해결 : {확장자 : [ '.js', '.jsx']},


파일 확장자를 변경하지 않으려면 jsx를 반환하는 함수를 내 보낸 다음 js 파일에서 해당 함수를 가져와 호출 할 수 있습니다.

// greeter.jsx

import React from 'react';

export default name => (
  <div>
    {`Hello, ${name}!`}
  </div>
);

그리고

// index.js

import ReactDOM from 'react-dom';
import greeter from './components/greeter';

const main = document.getElementsByTagName('main')[0];

ReactDOM.render(greeter('World'), main);

To expound on Martin's answer, it seems that it is not possible, currently, to use JSX in React Native. A PR was created but reverted due to concerns about fragmentation and unknown consequences of having things like index.ios.jsx. I'm not sure how AirBnb works around this or if they do, but I have used basically the same rules block as Martin.


Call me a dummy if it does not work for you

    "rules": {
        "react/jsx-filename-extension": [0],
        "import/extensions": "off"
    }

참고URL : https://stackoverflow.com/questions/43031126/jsx-not-allowed-in-files-with-extension-js-with-eslint-config-airbnb

반응형