7

I am trying to use Prettier with eslint and TypeScript. When I am running npm run prettier -- --list-different all of my css files are getting the error SyntaxError: Unexpected token, expected ";". I am thinking it is an issue with the way I have my parsing set up, but cannot seem to get it to work. This is my current setup.

.eslintrc

{
  "parser": "@typescript-eslint/parser",
  "parserOptions": {
    "ecmaVersion": 2018,
    "sourceType":  "module",
    "ecmaFeatures":  {
      "jsx":  true
    }
  },
  "extends": [
      "plugin:@typescript-eslint/recommended",
      "plugin:react/recommended",
      "prettier/@typescript-eslint",
      "plugin:prettier/recommended"
  ],
  "rules": {
    "no-console": "warn",
    "@typescript-eslint/explicit-function-return-type": ["warn", {"allowExpressions": true}]
  },
  "settings": {
      "react" : {
        "createClass": "createReactClass",
        "pragma": "React",
        "version":  "detect"
      }
  },
  "env": {
    "browser": true
  }
}

.prettierrc

{
  "arrowParens": "avoid",
  "bracketSpacing": true,
  "htmlWhitespaceSensitivity": "css",
  "insertPragma": false,
  "jsxBracketSameLine": false,
  "jsxSingleQuote": false,
    "parser": "babel",
    "printWidth": 120,
    "proseWrap": "always",
    "requirePragma": false,
    "semi": true,
    "singleQuote": true,
    "tabWidth": 2,
    "trailingComma": "none",
    "useTabs": false        
}

css file

body {
  margin: 0;
  padding: 0;
  font-family: sans-serif;
}

package.json (just in case)

{
  "name": "my-app",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "classnames": "^2.2.6",
    "lodash": "^4.17.11",
    "react": "^16.8.6",
    "react-dom": "^16.8.6",
    "react-redux": "^6.0.1",
    "react-scripts": "^2.1.8",
    "react-scripts-ts": "3.1.0",
    "redux": "^4.0.1",
    "redux-thunk": "^2.3.0",
    "typescript": "^3.4.2"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject",
    "lint": "eslint --ext js,jsx,ts,tsx --max-warnings 0 -f codeframe --cache --color src",
    "format": "npm run prettier -- --write",
    "prettier": "prettier \"**/*.+(js|jsx|json|yml|yaml|css|less|scss|ts|tsx|md|graphql|mdx)\"",
    "validate": "npm run lint && npm run prettier -- --list-different && tsc",
    "precommit": "lint-staged && tsc",
    "typecheck": "tsc"
  },
  "devDependencies": {
    "@types/classnames": "^2.2.7",
    "@types/enzyme": "^3.9.1",
    "@types/enzyme-adapter-react-16": "^1.0.5",
    "@types/jest": "^24.0.11",
    "@types/lodash": "^4.14.123",
    "@types/node": "^11.13.0",
    "@types/react": "^16.8.13",
    "@types/react-dom": "^16.8.3",
    "@types/react-redux": "^7.0.6",
    "@typescript-eslint/eslint-plugin": "^1.6.0",
    "@typescript-eslint/parser": "^1.6.0",
    "enzyme": "^3.9.0",
    "enzyme-adapter-react-16": "^1.12.1",
    "eslint-config-prettier": "^4.1.0",
    "eslint-plugin-prettier": "^3.0.1",
    "eslint-plugin-react": "^7.12.4",
    "prettier": "^1.16.4",
    "react-addons-test-utils": "^15.6.2",
    "redux-devtools-extension": "^2.13.8"
  },
  "browserslist": [
    ">0.2%",
    "not dead",
    "not ie <= 11",
    "not op_mini all"
  ]
}

I have tried adding overrides to the .prettierrc and .eslintrc files like this but it did not work.

"overrides": [{
    "files": "*.css",
    "options": {
        "parser": "babel"
     }
}]
1

1 Answer 1

18

Turns out I needed to add the overrides to the .prettierrc file with the parser set to css. I was using the wrong parser, trying things like postcss-scss instead. It would look like this.

"overrides": [
  {
    "files": "*.css",
    "options": {
      "parser": "css"
    }
  }
]

Documentation is here: https://prettier.io/docs/en/options.html#parser

Sign up to request clarification or add additional context in comments.

1 Comment

Awesome, exactly what I needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.