Search code examples
javascriptreactjseslintpackage.jsonprettier

React ignore .prettierignore file [EDIT: nothing to do with prettier]


I desperately try to keep some escape characters (\) in a js file in my React js project, because I need them. But someone is deleting them automatically, I guess it is prettier. Here is my config :

└── src
|   └── folder
|       └── fileToIgnore.js
├──.prettierrc
├──.prettierignore
├── package.json

In my package.json I have this :

{
  "lint-staged": {
    "*.{js,json,css,md}": [
      "prettier --write",
      "git add"
    ],
  },
  "dependencies": {
    "react": "^16.6.0",
    "react-dom": "^16.6.0",
    ...
  },
  "devDependencies": {
    "husky": "^0.14.3",
    "lint-staged": "^7.1.2",
    "prettier": "1.13.2",
    "source-map-explorer": "^1.6.0"
  },
  "scripts": {
    "precommit": "lint-staged",
    "start": ". ./scripts/build-vars.sh && react-scripts start",
   ...
  }
}

In my fileToIgnore.js I have that :

/* eslint-disable no-useless-escape */

export const setClickTag = `'use strict';function getParameterByName(a){var b=RegExp('[?&]'+a+'=([^&]*)').exec(window.location.search);return b&&decodeURIComponent(b[1].replace(/\+/g,' '))}var clickTag=getParameterByName('clickTag');`;

In my .prettierrc I have that

singleQuote: true
trailingComma: all
printWidth: 120

SOLUTION 1 I tried to create a .prettierignore file with that :

/src/folder/fileToIgnore.js    
**/src/folder/fileToIgnore.js
src/folder/fileToIgnore.js
src/folder/*.js

but it didn't work out

SOLUTION 2 I added // prettier-ignore before each variable I wanted to ignore, but it didn't work out

SOLUTION 3 I tried to modify the packager.json like this :

  "lint-staged": {
    "*.{js,json,css,md}": [
      "prettier --write",
      "git add"
    ],
    "ignore":["**/src/folder/fileToIgnore.js"]
  },

but it wasn't the solution either.

I really am stuck there, I am thinking maybe React is overwriting some parameters ? Or not ? I am lost ! Thanks for your help !!


Solution

  • This has nothing to do with prettier, now you have provided an example it's easy to see you are not escaping your strings.

    You can't do just \ in JS, to do a backslash in JS inside a string you need to use \\.

    Now with that in mind, inside your string you have -> replace(/\+/g,' '), this needs to be replace(/\\+/g,' ')due to string escaping.