From 3164aab426c9bd3cc9bf9178e87fb75ac509e4f4 Mon Sep 17 00:00:00 2001 From: Wim Van Aerschot Date: Thu, 28 Dec 2017 16:30:21 +0100 Subject: [PATCH] feat: added autofix on save --- lib/helpers.js | 12 ++++++++++++ lib/index.js | 44 ++++++++++++++++++++++++++++++++++++++++++-- package.json | 6 ++++++ 3 files changed, 60 insertions(+), 2 deletions(-) diff --git a/lib/helpers.js b/lib/helpers.js index 0451404c..c58a8fb8 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -182,6 +182,13 @@ export async function getStylelintInstance(filePath) { return require(stylelintPath); } +export const applyFixedStyles = async (editor, results) => { + // eslint-disable-next-line no-underscore-dangle + const result = results._postcssResult; + const styles = result.root.toString(result.opts.syntax); + editor.setText(styles); +}; + export const runStylelint = async (editor, stylelintOptions, filePath, settings) => { startMeasure('linter-stylelint: Stylelint'); let data; @@ -230,6 +237,11 @@ export const runStylelint = async (editor, stylelintOptions, filePath, settings) endMeasure('linter-stylelint: Lint'); return null; } + + if (stylelintOptions.fix) { + applyFixedStyles(editor, results); + } + return parseResults(editor, results, filePath, settings.showIgnored); }; diff --git a/lib/index.js b/lib/index.js index 4ff75724..0d473153 100644 --- a/lib/index.js +++ b/lib/index.js @@ -58,7 +58,17 @@ export default { atom.config.observe('core.excludeVcsIgnoredPaths', (value) => { this.coreIgnored = value; }), + atom.config.observe('linter-stylelint.attemptFix', (value) => { + this.attemptFix = value; + }), ); + this.subscriptions.add(atom.workspace.observeTextEditors((editor) => { + editor.onDidSave(async () => { + if (this.attemptFix) { + this.fixJob(editor); + } + }); + })); this.baseScopes = [ 'source.css', @@ -77,13 +87,42 @@ export default { this.subscriptions.dispose(); }, + fixJob(editor) { + let correctScope = false; + + if (editor && editor.buffer && editor.buffer.file && editor.buffer.file.path) { + const { path } = editor.buffer.file; + + this.baseScopes.forEach((scope) => { + const extension = scope.replace('source', ''); + + if (path.lastIndexOf(extension) === path.length - extension.length) { + correctScope = true; + } + }); + } + + if (!correctScope) { + return; + } + + const oldFileContent = editor.getText(); + this.provideLinter().lint(editor, true).then(() => { + const newFileContent = editor.getText(); + + if (newFileContent !== oldFileContent) { + editor.save(); + } + }); + }, + provideLinter() { return { name: 'stylelint', grammarScopes: this.baseScopes, scope: 'file', lintsOnChange: true, - lint: async (editor) => { + lint: async (editor, autofix) => { // Force the dependencies to load if they haven't already loadDeps(); @@ -99,7 +138,8 @@ export default { const options = { code: text, - codeFilename: filePath + codeFilename: filePath, + fix: !!autofix }; const scopes = editor.getLastCursor().getScopeDescriptor().getScopesArray(); diff --git a/package.json b/package.json index c0ece42a..a802a3ac 100644 --- a/package.json +++ b/package.json @@ -48,6 +48,12 @@ "title": "Show message when a file is ignored", "type": "boolean", "default": false + }, + "attemptFix": { + "description": "Automatically attempt to fix errors using stylelint --fix flag (experimental)", + "title": "Attempt to fix styles on save", + "type": "boolean", + "default": false } }, "homepage": "https://github.com/AtomLinter/linter-stylelint#readme",