Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
fix(formatOnSave): don't error when saving a new file
Browse files Browse the repository at this point in the history
Fixes a bug where saving a brand new file would cause an error. This was because we were trying to
access the path of the editor's buffer's file when the file is actually null. Now we check for the
presence of the file before trying to access its path.

Fixes #79
  • Loading branch information
robwise committed Mar 7, 2017
1 parent 5f936cc commit 19b4d04
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
18 changes: 9 additions & 9 deletions decls/index.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
// types for working with Atom stuff
declare type FilePath = string;
declare type Point = {row: number, column: number};
declare type Range = {start: Point, end: Point};
declare type Point = { row: number, column: number };
declare type Range = { start: Point, end: Point };
declare type Ranges = Array<Range>;
// eslint-disable-next-line no-undef
declare type Globs = Array<string>;
// eslint-disable-next-line no-undef
declare type TextEditor = {
getGrammar: () => {scopeName: string},
getBuffer: () => {getRange: () => Range},
getGrammar: () => { scopeName: string },
getBuffer: () => { getRange: () => Range },
getCursorScreenPosition: () => Point,
getLastCursor: () => {getScopeDescriptor: () => string},
getLastCursor: () => { getScopeDescriptor: () => string },
getSelectedText: () => string,
getSelectedBufferRanges: () => Ranges,
// getTextInRange: () => string,
getTextInBufferRange: () => string,
setCursorScreenPosition: (point: Point) => Point,
setTextInBufferRange: (bufferRange: Range, text: string) => Range,
buffer: {file: {path: ?FilePath}},
buffer: { file: ?{ path: ?FilePath } },
backwardsScanInBufferRange: (
regex: RegExp,
Range: Range,
Expand All @@ -28,15 +28,15 @@ declare type TextEditor = {
range: Range,
stop: Function,
replace: (replacement: string) => void,
}
) => void
},
) => void,
) => void,
};
declare var atom: {
config: {
get: (key: string) => any,
},
notifications: {
addError: (message: string, options?: {detail?: string, dismissable?: boolean}) => void,
addError: (message: string, options?: { detail?: string, dismissable?: boolean }) => void,
},
};
2 changes: 1 addition & 1 deletion dist/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ var getPrettierOption = function getPrettierOption(key) {
};

var getCurrentFilePath = function getCurrentFilePath(editor) {
return editor.buffer.file.path;
return editor.buffer.file ? editor.buffer.file.path : undefined;
};

var isInScope = function isInScope(editor) {
Expand Down
2 changes: 1 addition & 1 deletion src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const shouldDisplayErrors = () => !getConfigOption('silenceErrors');

const getPrettierOption = (key: string) => getConfigOption(`prettierOptions.${key}`);

const getCurrentFilePath = (editor: TextEditor) => editor.buffer.file.path;
const getCurrentFilePath = (editor: TextEditor) => editor.buffer.file ? editor.buffer.file.path : undefined;

const isInScope = (editor: TextEditor) =>
getConfigOption('formatOnSaveOptions.scopes').includes(getCurrentScope(editor));
Expand Down
9 changes: 9 additions & 0 deletions src/helpers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ describe('getCurrentFilePath', () => {

expect(actual).toBe(expected);
});

test('returns undefined if there is no file', () => {
const editor = textEditor({ buffer: { file: null } });

const actual = getCurrentFilePath(editor);
const expected = undefined;

expect(actual).toBe(expected);
});
});

describe('isInScope', () => {
Expand Down

0 comments on commit 19b4d04

Please sign in to comment.