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

Commit

Permalink
Merge branch 'master' into patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
jonboiser authored Mar 20, 2019
2 parents ea997ce + 8ae10a4 commit f417a00
Show file tree
Hide file tree
Showing 11 changed files with 1,861 additions and 1,186 deletions.
37 changes: 37 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,40 @@
## [4.4.3](https://github.com/AtomLinter/linter-stylelint/compare/v4.4.2...v4.4.3) (2019-01-22)


### Bug Fixes

* **deps:** update dependency stylelint to v9.10.1 ([f5465a4](https://github.com/AtomLinter/linter-stylelint/commit/f5465a4))

## [4.4.2](https://github.com/AtomLinter/linter-stylelint/compare/v4.4.1...v4.4.2) (2019-01-22)


### Bug Fixes

* **deps:** update dependency atom-package-deps to v5 ([9018f56](https://github.com/AtomLinter/linter-stylelint/commit/9018f56))

## [4.4.1](https://github.com/AtomLinter/linter-stylelint/compare/v4.4.0...v4.4.1) (2019-01-22)


### Bug Fixes

* **deps:** update dependency resolve to v1.10.0 ([e56e754](https://github.com/AtomLinter/linter-stylelint/commit/e56e754))

# [4.4.0](https://github.com/AtomLinter/linter-stylelint/compare/v4.3.2...v4.4.0) (2019-01-22)


### Bug Fixes

* add default option object ([a4d6c92](https://github.com/AtomLinter/linter-stylelint/commit/a4d6c92))
* check passed-in editor, not a new one ([d9489c7](https://github.com/AtomLinter/linter-stylelint/commit/d9489c7))
* don’t change editor text unless necessary ([32427ea](https://github.com/AtomLinter/linter-stylelint/commit/32427ea))
* only save files (buffers) once ([cfd5b9c](https://github.com/AtomLinter/linter-stylelint/commit/cfd5b9c))
* restore cursors ([5ebb26f](https://github.com/AtomLinter/linter-stylelint/commit/5ebb26f))


### Features

* added autofix on save ([3164aab](https://github.com/AtomLinter/linter-stylelint/commit/3164aab))

## [4.3.2](https://github.com/AtomLinter/linter-stylelint/compare/v4.3.1...v4.3.2) (2018-06-29)


Expand Down
30 changes: 30 additions & 0 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,31 @@ 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 newText = result.root.toString(result.opts.syntax);
// Only set new text if it's changed
if (newText !== editor.getText()) {
// Save the cursor positions so that we can restore them after the `setText`,
// which consolodates all cursors together into one at the end of the file.
const bufferPositions = editor.getCursorBufferPositions();
editor.setText(newText);
bufferPositions.forEach((position, index) => {
// Buffer positions are returned in order they were created, so we
// want to restore them in order as well.
if (index === 0) {
// We'll have one cursor in the editor after the `setText`, so the first
// one can just be a move
editor.setCursorBufferPosition(position, { autoscroll: false });
} else {
// After that, we need to create new cursors
editor.addCursorAtBufferPosition(position);
}
});
}
};

export const runStylelint = async (editor, stylelintOptions, filePath, settings) => {
startMeasure('linter-stylelint: Stylelint');
let data;
Expand Down Expand Up @@ -230,6 +255,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
42 changes: 40 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

// eslint-disable-next-line import/extensions, import/no-extraneous-dependencies
import { CompositeDisposable } from 'atom';
import hasValidScope from './validate';

// Dependencies
let helpers;
Expand Down Expand Up @@ -58,8 +59,29 @@ export default {
atom.config.observe('core.excludeVcsIgnoredPaths', (value) => {
this.coreIgnored = value;
}),
atom.config.observe('linter-stylelint.fixOnSave', (value) => {
this.fixOnSave = value;
}),
);

const textBuffers = new Map();
this.subscriptions.add(atom.workspace.observeTextEditors((editor) => {
const buffer = editor.getBuffer();
if (!textBuffers.has(buffer)) {
textBuffers.set(buffer, buffer.onWillSave(() => {
if (this.fixOnSave && hasValidScope(editor, this.baseScopes)) {
return this.fixJob(editor);
}
return Promise.resolve();
}));
buffer.onDidDestroy(() => {
// Maybe this is handled in the destruction of the buffer itself?
textBuffers.get(buffer).dispose();
textBuffers.delete(buffer);
});
}
}));

this.baseScopes = [
'source.css',
'source.scss',
Expand All @@ -78,13 +100,28 @@ export default {
this.subscriptions.dispose();
},

fixJob(editor) {
// Silently return if the editor is invalid
if (!editor || !atom.workspace.isTextEditor(editor)) {
return Promise.resolve(null);
}

// Do not try to make fixes on an empty file
const text = editor.getText();
if (text.length === 0) {
return Promise.resolve(null);
}

return this.provideLinter().lint(editor, { shouldFix: true });
},

provideLinter() {
return {
name: 'stylelint',
grammarScopes: this.baseScopes,
scope: 'file',
lintsOnChange: true,
lint: async (editor) => {
lint: async (editor, { shouldFix } = {}) => {
// Force the dependencies to load if they haven't already
loadDeps();

Expand All @@ -100,7 +137,8 @@ export default {

const options = {
code: text,
codeFilename: filePath
codeFilename: filePath,
fix: Boolean(shouldFix)
};

const scopes = editor.getLastCursor().getScopeDescriptor().getScopesArray();
Expand Down
8 changes: 8 additions & 0 deletions lib/validate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use babel';

const hasValidScope = (editor, validScopes) => editor.getCursors()
.some(cursor => cursor.getScopeDescriptor()
.getScopesArray()
.some(scope => validScopes.includes(scope)));

export default hasValidScope;
Loading

0 comments on commit f417a00

Please sign in to comment.