/** @babel */
import path from 'path';
import {CompositeDisposable} from 'atom';
import {allowUnsafeNewFunction} from 'loophole';
import setText from 'atom-set-text';
import pkgDir from 'pkg-dir';
import {sync as loadJson} from 'load-json-file';

let lintText;
allowUnsafeNewFunction(() => {
	lintText = require('xo').lintText;
});

function lint(textEditor) {
	const filePath = textEditor.getPath();
	let report;

	const dir = pkgDir.sync(path.dirname(filePath));

	// no package.json
	if (!dir) {
		return [];
	}

	// ugly hack to workaround ESLint's lack of a `cwd` option
	// TODO: remove this when https://github.com/sindresorhus/atom-linter-xo/issues/19 is resolved
	const defaultCwd = process.cwd();
	process.chdir(dir);

	const pkg = loadJson(path.join(dir, 'package.json'));

	// only lint when `xo` is a dependency
	if (!(pkg.dependencies && pkg.dependencies.xo) &&
		!(pkg.devDependencies && pkg.devDependencies.xo)) {
		return [];
	}

	allowUnsafeNewFunction(() => {
		report = lintText(textEditor.getText(), {
			cwd: dir,
			filename: filePath
		});
	});

	process.chdir(defaultCwd);

	const ret = [];

	report.results.forEach(result => {
		result.messages.forEach(x => {
			ret.push({
				filePath,
				type: x.severity === 2 ? 'Error' : 'Warning',
				text: `${x.message} (${x.ruleId})`,
				range: [
					[x.line - 1, x.column - 1],
					[x.line - 1, x.column - 1]
				]
			});
		});
	});

	return ret;
}

export const provideLinter = () => ({
	name: 'xo',
	grammarScopes: [
		'source.js',
		'source.jsx',
		'source.js.jsx'
	],
	scope: 'file',
	lintOnFly: true,
	lint
});

export function activate() {
	require('atom-package-deps').install('linter-xo');

	this.subscriptions = new CompositeDisposable();
	this.subscriptions.add(atom.commands.add('atom-text-editor', {
		'XO:Fix': () => {
			const editor = atom.workspace.getActiveTextEditor();

			if (!editor) {
				return;
			}

			let report;

			allowUnsafeNewFunction(() => {
				report = lintText(editor.getText(), {
					fix: true,
					cwd: path.dirname(editor.getPath())
				});
			});

			const output = report.results[0].output;

			if (output) {
				setText(output);
			}
		}
	}));
}

export function deactivate() {
	this.subscriptions.dispose();
}

@@ -394,30 +394,16 @@
 Path();%0A
-%09let report;%0A%0A
 %09const d
@@ -926,16 +926,29 @@
 %5B%5D;%0A%09%7D%0A%0A
+%09let report;%0A
 %09allowUn
@@ -1096,110 +1096,54 @@
 ;%0A%0A%09
-const ret = %5B%5D;%0A%0A%09report.results.forEach(result =%3E %7B%0A%09%09result.messages.forEach(x =%3E %7B%0A%09%09%09ret.push
+return report.results%5B0%5D.messages.map(x =%3E 
 (%7B%0A
-%09%09
 %09%09fi
@@ -1150,18 +1150,16 @@
 lePath,%0A
-%09%09
 %09%09type: 
@@ -1200,18 +1200,16 @@
 ing',%0A%09%09
-%09
-%09
 text: %60$
@@ -1236,18 +1236,16 @@
 eId%7D)%60,%0A
-%09%09
 %09%09range:
@@ -1247,18 +1247,16 @@
 ange: %5B%0A
-%09%09
 %09%09%09%5Bx.li
@@ -1281,18 +1281,16 @@
  1%5D,%0A%09%09%09
-%09%09
 %5Bx.line 
@@ -1312,44 +1312,16 @@
  1%5D%0A
-%09%09
 %09%09%5D%0A%09
-%09%09%7D);%0A%09%09%7D);%0A%09%7D);%0A%0A%09return ret
+%7D))
 ;%0A%7D%0A
@@ -1328,21 +1328,24 @@
 %0Aexport 
-const
+function
  provide
@@ -1354,28 +1354,32 @@
 nter
- = () =%3E (
+() %7B%0A%09return 
 %7B%0A
+%09
 %09name: '
 xo',
@@ -1378,13 +1378,14 @@
 e: '
-xo
+XO
 ',%0A
+%09
 %09gra
@@ -1400,16 +1400,17 @@
 es: %5B%0A%09%09
+%09
 'source.
@@ -1414,16 +1414,17 @@
 ce.js',%0A
+%09
 %09%09'sourc
@@ -1433,16 +1433,17 @@
 jsx',%0A%09%09
+%09
 'source.
@@ -1454,12 +1454,14 @@
 sx'%0A
+%09
 %09%5D,%0A
+%09
 %09sco
@@ -1472,16 +1472,17 @@
 'file',%0A
+%09
 %09lintOnF
@@ -1495,17 +1495,20 @@
 ue,%0A
+%09
 %09lint%0A
-%7D);
+%09%7D;%0A%7D
 %0A%0Aex

simplify the code