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

Commit

Permalink
feat: added autofix on save
Browse files Browse the repository at this point in the history
  • Loading branch information
Wim Van Aerschot authored and IanVS committed Jan 22, 2019
1 parent c5a7a50 commit 3164aab
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
12 changes: 12 additions & 0 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
};

Expand Down
44 changes: 42 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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();

Expand All @@ -99,7 +138,8 @@ export default {

const options = {
code: text,
codeFilename: filePath
codeFilename: filePath,
fix: !!autofix
};

const scopes = editor.getLastCursor().getScopeDescriptor().getScopesArray();
Expand Down
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 3164aab

Please sign in to comment.