Hydrogen: Refactor code-manager as pure functions

Created on 27 Mar 2017  路  8Comments  路  Source: nteract/hydrogen

Right now code-manager.js uses store.editor implicitly. This makes bugs like #669 a lot likelier since a user of the function may not check if store.editor !== null beforehand.

If we move to pure functions this will become more transparent and we can even add flow to this file to be reminded every time we don't check for the case store.editor == null before using a function.

For example getTextInRange right now looks like this (the output depends on the value of store.editor):

export function getTextInRange(start, end) {
  const code = store.editor.getTextInBufferRange([start, end]);
  return normalizeString(code);
}

As a pure functions and with flow typings it would look like this (the output only depends on the input parameters):

/* @flow */

export function getTextInRange(editor: atom$TextEditor, start: atom$Point, end: atom$Point) {
  const code = editor.getTextInBufferRange([start, end]);
  return normalizeString(code);
}
good first issue refactor

Most helpful comment

PS: If you need a nice flow linter I recommend the flow-ide plugin.

Cool, I was going to ask about this. Tried Nuclide, but it was a little much for me at this point.

All 8 comments

brb... reading up on flow

At first glance it looks similar to julia types, one of the reasons julia is probably my favorite language.

Nice, and they even have a cool new website, just in time 馃槅 https://flowtype.org/

I'm going to take a crack at this after work today.

@BenRussert Very cool! 馃帀

There will be likely a few flow errors due to missing declarations so feel free to submit a work in progress PR.

PS: If you need a nice flow linter I recommend the flow-ide plugin.

PS: If you need a nice flow linter I recommend the flow-ide plugin.

Cool, I was going to ask about this. Tried Nuclide, but it was a little much for me at this point.

@lgeiger this is going pretty well so far. You were right that the stopping point was type declarations.

Is the plan just to add to the atom.js.flow file as we go? There were several missing methods/properties under atom$TextEditor and atom$TextBuffer that I could add to the file.

Great!

Is the plan just to add to the atom.js.flow file as we go? There were several missing methods/properties under atom$TextEditor and atom$TextBuffer that I could add to the file.

馃憤 That's what I've been doing so far

This is working and passing tests (though I had to alter the code manger spec. I'll pr tomorrow for some feedback.

https://github.com/BenRussert/hydrogen/tree/refactor-cm-flow

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lgeiger picture lgeiger  路  3Comments

onyxfish picture onyxfish  路  4Comments

Mike-MU10 picture Mike-MU10  路  4Comments

DannyDannyDanny picture DannyDannyDanny  路  3Comments

mda6 picture mda6  路  3Comments