Skip to content
This repository was archived by the owner on Sep 23, 2023. It is now read-only.

Make standard.lintText() synchronous again #57

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 27 additions & 32 deletions lib/standard-formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,23 @@ var path = require('path')
var minimatch = require('minimatch')
var findRoot = require('find-root')
var allowUnsafeNewFunction = require('loophole').allowUnsafeNewFunction
var standard = allowUnsafeNewFunction(function () {
return require('standard')
})
var format = {
'standard-format': 'standard-format',
'semi-standard': 'semistandard-format',
'happiness': 'happiness-format'
}

function syncLintText (text) {
try {
opts = this.parseOpts({ fix: true }).eslintConfig
var engine = new this.eslint.CLIEngine(opts)
return engine.executeOnText(text).results[0].output
} catch (err) {
console.log('Error transforming using standard:', e)
return text
}
}

module.exports = {
style: null,
fileTypes: ['.js', '.jsx'],
Expand Down Expand Up @@ -65,39 +73,26 @@ module.exports = {
var selectedText = selection ? editor.getSelectedText() : null
var text = selectedText || editor.getText()
var cursorPosition = editor.getCursorScreenPosition()
this.transformText(text, function (e, transformed) {
if (e) {
console.log('Error transforming using standard:', e)
transformed = text
}
if (selectedText) {
editor.setTextInBufferRange(editor.getSelectedBufferRange(), transformed)
} else {
editor.setText(transformed)
}
editor.setCursorScreenPosition(cursorPosition)
})
var transformed = this.transformText(text)
if (selectedText) {
editor.setTextInBufferRange(editor.getSelectedBufferRange(), transformed)
} else {
editor.setText(transformed)
}
editor.setCursorScreenPosition(cursorPosition)
},

transformText: function (text, cb) {
if (this.style === 'standard') {
allowUnsafeNewFunction(function () {
require('standard').lintText(text, {fix: true}, function (e, res) {
if (e) return cb(e)
var fixed = res && Array.isArray(res.results) && res.results[0] && res.results[0].output
cb(null, typeof fixed !== 'string' ? text : fixed)
transformText: function (text) {
try {
if (this.style === 'standard') {
return allowUnsafeNewFunction(function () {
return syncLintText.call(require('standard'), text)
})
})
} else if (format.hasOwnProperty(this.style)) {
try {
cb(null, require(format[this.style]).transform(text))
} catch (e) {
// Failed to transform, likely due to syntax error in `text`
cb(null, text)
} else if (format.hasOwnProperty(this.style)) {
return require(format[this.style]).transform(text)
}
} else {
cb(null, text)
}
} catch (e) {}
return text
},

setStyle: function () {
Expand Down