Prettier-atom: Formatting of less files gives error

Created on 29 Nov 2018  路  6Comments  路  Source: prettier/prettier-atom

When saving .less files and having enabled the Stylelint Integration in the setting.

My .stylelintrc file looks like this:

{
  "extends": "stylelint-config-standard"
}

And I am using [email protected]

A toast in atom is displayed with the following error: prettier-atom failed: Cannot set property 'useTabs' of null.

Errors from stylelint are shown in the gutter.

troubleshooting

Most helpful comment

TL;DR: You need an explicit Prettier config defined in your project.

I was having the issue as mentioned above, where the following error is displayed:

Cannot set property 'parser' of null

I took a quick look through the debugger, and it boiled down to this section in the prettier-stylelint codebase (.atom/packages/prettier-atom/node_modules/prettier-stylelint/src/index.js):

resolveConfig.resolve = (stylelintConfig, prettierOptions = {}) => {
    const { rules } = stylelintConfig;

    if (rules['max-line-length']) {
        const printWidth = rules['max-line-length'][0];

        prettierOptions.printWidth = printWidth;
    }

    if (rules['string-quotes']) {
        const quotes = rules['string-quotes'][0];

        if (quotes === 'single') {
            prettierOptions.singleQuote = true;
        }
    }

    if (rules.indentation) {
        const indentation = rules.indentation[0];

        if (indentation === 'tab') {
            prettierOptions.useTabs = true;
            prettierOptions.tabWidth = 2;
        } else {
            prettierOptions.useTabs = false;
            prettierOptions.tabWidth = indentation;
        }
    }
    prettierOptions.parser = 'postcss';
    debug('prettier %O', prettierOptions);
    debug('linter %O', stylelintConfig);

    return [prettierOptions, stylelintConfig];
};

In this case, prettierOptions was being passed in explicitly as null, and causing the error message to display. This happens if you have no explicit Prettier config defined in your project. I created a .prettierrc file with an empty config object {}, restarted Atom, and the issue was fixed.

All 6 comments

Formatting of .css file with enabled Stylelint Integration setting does not happens, but error message Cannot set property 'parser' of null displays in the gutter. Property name displayed in the error message changes time to time in an uncertain way.

Debugging:

Atom version: 1.33.0
prettier-atom version: 0.56.2
prettier: bundled
prettier version: 1.15.2
prettier-eslint version: 8.8.2
prettier-atom configuration: {
  "formatOnSaveOptions": {
    "enabled": true,
    "showInStatusBar": true,
    "respectEslintignore": true,
    "excludedGlobs": [],
    "whitelistedGlobs": [],
    "isDisabledIfNotInPackageJson": false,
    "isDisabledIfNoConfigFile": false
  },
  "useEslint": true,
  "useStylelint": true,
  "prettierEslintOptions": {
    "prettierLast": false
  }
}

.stylelintrc:

{
  "extends": "stylelint-config-recommended"
}

or:

{
  "extends": "stylelint-config-standard"
}

package.json dependencies section:

  "dependencies": {
    "eslint": "^5.10.0",
    "eslint-config-standard": "^12.0.0",
    "eslint-plugin-import": "^2.14.0",
    "eslint-plugin-node": "^8.0.0",
    "eslint-plugin-promise": "^4.0.1",
    "eslint-plugin-standard": "^4.0.0",
    "stylelint": "^9.9.0",
    "stylelint-config-recommended": "^2.1.0",
    "stylelint-config-standard": "^18.2.0"
  }

Eslint Integration works fine.
Stylelint is installed both global and local.

I'm experiencing this as well. Has there been any headway or solutions found yet?

Any progress on this?

Any progress on which issue? The OP issue seems like a configuration issue with stylelint, and the next post seems completely unrelated.

Not sure what triggered it for me (I was setting up a new project and messing with the package.json so it's probably caused by some kind of version compatibility issue), but the error stopped when I unchecked the "stylelint integration" checkbox.
But then of course it doesn't use stylelint rules but rather the default rules which is slightly different than my stylelint rules. Which is fine by me but probably not ideal for everyone if they have a project mandated stylelint setup.

TL;DR: You need an explicit Prettier config defined in your project.

I was having the issue as mentioned above, where the following error is displayed:

Cannot set property 'parser' of null

I took a quick look through the debugger, and it boiled down to this section in the prettier-stylelint codebase (.atom/packages/prettier-atom/node_modules/prettier-stylelint/src/index.js):

resolveConfig.resolve = (stylelintConfig, prettierOptions = {}) => {
    const { rules } = stylelintConfig;

    if (rules['max-line-length']) {
        const printWidth = rules['max-line-length'][0];

        prettierOptions.printWidth = printWidth;
    }

    if (rules['string-quotes']) {
        const quotes = rules['string-quotes'][0];

        if (quotes === 'single') {
            prettierOptions.singleQuote = true;
        }
    }

    if (rules.indentation) {
        const indentation = rules.indentation[0];

        if (indentation === 'tab') {
            prettierOptions.useTabs = true;
            prettierOptions.tabWidth = 2;
        } else {
            prettierOptions.useTabs = false;
            prettierOptions.tabWidth = indentation;
        }
    }
    prettierOptions.parser = 'postcss';
    debug('prettier %O', prettierOptions);
    debug('linter %O', stylelintConfig);

    return [prettierOptions, stylelintConfig];
};

In this case, prettierOptions was being passed in explicitly as null, and causing the error message to display. This happens if you have no explicit Prettier config defined in your project. I created a .prettierrc file with an empty config object {}, restarted Atom, and the issue was fixed.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jonyyz picture jonyyz  路  6Comments

saadq picture saadq  路  3Comments

kentcdodds picture kentcdodds  路  5Comments

mgrip picture mgrip  路  5Comments

mxstbr picture mxstbr  路  5Comments