prettier-atom failed: Cannot read property 'node' of undefined

Created on 30 Jan 2018  路  12Comments  路  Source: prettier/prettier-atom

Updating to the new prettier-atom 0.49.0 gives me an error on every save when the last 2 lines are blank.

screen shot 2018-01-30 at 17 38 37

How i tried to solve it:

  • restarted atom
  • reinstalled prettier-atom and its dependencies
  • restarted again and again.
OSX: version 10.11.6
Atom version: 1.23.3
prettier-atom version: 0.49.0
prettier version: 1.10.2
prettier-eslint version: 8.8.1
prettier-atom configuration: {
  "formatOnSaveOptions": {
    "enabled": true,
    "respectEslintignore": true,
    "showInStatusBar": false,
    "javascriptScopes": [
      "source.js",
      "source.jsx",
      "source.js.jsx",
      "source.babel",
      "source.js-semantic",
      "text.html.basic",
      "text.html.vue"
    ],
    "typescriptScopes": [
      "source.ts",
      "source.tsx",
      "source.ts.tsx"
    ],
    "cssScopes": [
      "source.css",
      "source.less",
      "source.css.less",
      "source.scss",
      "source.css.scss",
      "source.css.postcss"
    ],
    "jsonScopes": [
      "source.json"
    ],
    "graphQlScopes": [
      "source.graphql"
    ],
    "markdownScopes": [
      "source.md",
      "source.gfm",
      "text.md"
    ],
    "vueScopes": [
      "text.html.vue"
    ],
    "excludedGlobs": [],
    "whitelistedGlobs": [],
    "isDisabledIfNotInPackageJson": false,
    "isDisabledIfNoConfigFile": false
  },
  "prettierOptions": {
    "singleQuote": true,
    "bracketSpacing": true,
    "semi": true,
    "useTabs": false,
    "jsxBracketSameLine": false,
    "printWidth": 80,
    "tabWidth": "auto",
    "trailingComma": "none",
    "parser": "babylon",
    "arrowParens": "avoid"
  },
  "useEslint": false,
  "useStylelint": false,
  "useEditorConfig": true,
  "prettierEslintOptions": {
    "prettierLast": false
  }
}


TypeError: Cannot read property 'node' of undefined
at formatWithCursor (/Users/user-name/.atom/packages/prettier-atom/node_modules/prettier/index.js:33662:44)
at Object.formatWithCursor (/Users/user-name/.atom/packages/prettier-atom/node_modules/prettier/index.js:33941:12)
at executePrettierWithCursor (/Users/user-name/.atom/packages/prettier-atom/dist/executePrettier/executePrettierOnBufferRange.js:41:38)
at _callee$ (/Users/user-name/.atom/packages/prettier-atom/dist/executePrettier/executePrettierOnBufferRange.js:87:46)
at tryCatch (/Users/user-name/.atom/packages/prettier-atom/node_modules/regenerator-runtime/runtime.js:62:40)
at Generator.invoke [as _invoke] (/Users/user-name/.atom/packages/prettier-atom/node_modules/regenerator-runtime/runtime.js:296:22)
at Generator.prototype.(anonymous function) [as next] (/Users/user-name/.atom/packages/prettier-atom/node_modules/regenerator-runtime/runtime.js:114:21)
at step (/Users/user-name/.atom/packages/prettier-atom/node_modules/babel-runtime/helpers/asyncToGenerator.js:17:30)
at /Users/user-name/.atom/packages/prettier-atom/node_modules/babel-runtime/helpers/asyncToGenerator.js:35:14
at F (/Users/user-name/.atom/packages/prettier-atom/node_modules/core-js/library/modules/_export.js:35:28)
at /Users/user-name/.atom/packages/prettier-atom/node_modules/babel-runtime/helpers/asyncToGenerator.js:14:12
at executePrettierOrIntegration (/Users/user-name/.atom/packages/prettier-atom/dist/executePrettier/executePrettierOnBufferRange.js:98:17)
at _callee2$ (/Users/user-name/.atom/packages/prettier-atom/dist/executePrettier/executePrettierOnBufferRange.js:129:20)
at tryCatch (/Users/user-name/.atom/packages/prettier-atom/node_modules/regenerator-runtime/runtime.js:62:40)
at Generator.invoke [as _invoke] (/Users/user-name/.atom/packages/prettier-atom/node_modules/regenerator-runtime/runtime.js:296:22)
at Generator.prototype.(anonymous function) [as next] (/Users/user-name/.atom/packages/prettier-atom/node_modules/regenerator-runtime/runtime.js:114:21)
at step (/Users/user-name/.atom/packages/prettier-atom/node_modules/babel-runtime/helpers/asyncToGenerator.js:17:30)
at /Users/user-name/.atom/packages/prettier-atom/node_modules/babel-runtime/helpers/asyncToGenerator.js:35:14
at F (/Users/user-name/.atom/packages/prettier-atom/node_modules/core-js/library/modules/_export.js:35:28)
at /Users/user-name/.atom/packages/prettier-atom/node_modules/babel-runtime/helpers/asyncToGenerator.js:14:12
at executePrettierOnBufferRange (/Users/user-name/.atom/packages/prettier-atom/dist/executePrettier/executePrettierOnBufferRange.js:181:18)
at callAppropriatePrettierExecutor (/Users/user-name/.atom/packages/prettier-atom/dist/formatOnSave/index.js:22:91)
at r (/Users/user-name/.atom/packages/prettier-atom/node_modules/lodash/lodash.min.js:5:165)
at /Users/user-name/.atom/packages/prettier-atom/node_modules/lodash/lodash.min.js:97:162
at r (/Users/user-name/.atom/packages/prettier-atom/node_modules/lodash/lodash.min.js:5:165)
at /Users/user-name/.atom/packages/prettier-atom/node_modules/lodash/lodash.min.js:63:325
at /Users/user-name/.atom/packages/prettier-atom/node_modules/lodash/lodash.min.js:49:144
at /Users/user-name/.atom/packages/prettier-atom/dist/formatOnSave/index.js:29:12
at attemptWithErrorNotification (/Users/user-name/.atom/packages/prettier-atom/dist/atomInterface/index.js:142:10)
at safeFormatOnSaveIfAppropriate (/Users/user-name/.atom/packages/prettier-atom/dist/formatOnSave/index.js:28:10)
at lazyFormatOnSave (/Users/user-name/.atom/packages/prettier-atom/dist/main.js:39:15)
at /Users/user-name/.atom/packages/prettier-atom/dist/main.js:120:14
at Function.module.exports.Emitter.simpleDispatch (/Applications/Atom.app/Contents/Resources/app/node_modules/event-kit/lib/emitter.js:25:20)
at Emitter. (/Applications/Atom.app/Contents/Resources/app/node_modules/event-kit/lib/emitter.js:154:50)
at Emitter.module.exports.Emitter.emitAsync (/Applications/Atom.app/Contents/Resources/app/node_modules/event-kit/lib/emitter.js:157:18)
at /Applications/Atom.app/Contents/Resources/app/node_modules/text-buffer/lib/text-buffer.js:1283:38

bug external

Most helpful comment

I鈥檒l be writing a patch this evening that will alleviate this issue for the mean time and fallback to formatting without cursor positioning until I can finish fixing the internal Prettier logic associated with maintaining cursor position. Apologies for the issues. They were unforeseen as an expectation of Prettier鈥檚 responsibility.

All 12 comments

Try updating the prettier dependency in your project, if you have one (and if yes, which version do you currently have?)

I have no prettier dependency in my project, only use it on save in atom.

If i create a empty .css file and try to save it with double empty lines it gives me the error.

screen shot 2018-01-30 at 21 52 15

I saw this creeping up after the update to maintain cursor position. I noticed it's really reliable when formatting something other than .js. I'll take a look at this since I introduced it on accident.

@SavePointSam FWIW I'm having this all the time while trying to format .md so +1 to the non-js observation.

Looks like there is a bug in Prettier causing this issue. During the process of formatting, the parsing algorithms return different AST structures and they are not being normalized to then process the correct cursor position. I will file a bug with Prettier to get this resolved. I may end up fixing it myself while I'm at it. haha.

After some more digging, it looks like there are a couple issues at hand. First, Prettier.formatWithCursor doesn't work for all of the supported parsers. Also, pretter-atom doesn't provide the correct parser name in some cases. Gonna get to work untangling this nest of wires.

Also seeing this issue on a project that was previously working. None of the configuration has changed but I started seeing the problem after updating prettier-atom

I鈥檒l be writing a patch this evening that will alleviate this issue for the mean time and fallback to formatting without cursor positioning until I can finish fixing the internal Prettier logic associated with maintaining cursor position. Apologies for the issues. They were unforeseen as an expectation of Prettier鈥檚 responsibility.

@SavePointSam you're too generous :)

Just giving back to the community that saves me hours of work and makes me a better engineer on a daily basis. 鉂わ笍

Closing as this behavior is no longer expected with 0.49.1. If you are still experiencing this error after updating, please open a new ticket.

@SavePointSam, thanks so much for the quick responses!

Was this page helpful?
0 / 5 - 0 ratings