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

Commit

Permalink
Merge pull request #101 from AtomLinter/resolve-relative-paths
Browse files Browse the repository at this point in the history
Resolve relative paths
  • Loading branch information
DanPurdy authored Jun 16, 2016
2 parents 4b05b05 + 7f84d7b commit 1797299
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.6.0
- Adds `resolvePathsRelativeToConfig` to the config options to allow paths to be resolved relative to your config file rather than project root. Thanks to [@DirtyHairy](https://github.com/DirtyHairy)
- The usual third party package updates

## 1.5.0
- Allows the inclusion of files with extra extensions such as 'file.scss.liquid'
- Updated to Atom linter ^5.0.1
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ There are three options you can configure either within the plugin or by editing

* `globalSassLint` This allows you to specify that you want to use your globally installed version of `sass-lint` (`npm install -g sass-lint`) instead of the version bundled with `linter-sass-lint`.

* `resolvePathsRelativeToConfig` This option allows you to choose to resolve file paths relative to your config file rather than relative to the root of your currently open project.

### Extra File Extensions

This plugin will attempt to lint a file with framework specific file extensions on top of the usual `.scss` and `.sass` extensions such as with shopify's `.scss.liquid` extension as long as you still include `.scss` or `.sass` somewhere in the file, you must also ensure that the Atom grammar scope for that file is set to either SCSS or Sass depending on which it corresponds to.
Expand Down
18 changes: 15 additions & 3 deletions lib/main.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ module.exports =
type: 'boolean'
description: 'and a .sass-lint.yml file is not specified in the .sass-lint.yml Path option'
default: false
resolvePathsRelativeToConfig:
title: 'Resolve paths in configuration relative to config file'
type: 'boolean'
description: 'Instead of the default where paths are resolved relative to the project root'
default: 'false'
configFile:
title: '.sass-lint.yml Config File'
description: 'A .sass-lint.yml file to use/fallback to if no config file is found in the current project root'
Expand Down Expand Up @@ -41,15 +46,22 @@ module.exports =
@subs.add atom.config.observe 'linter-sass-lint.globalNodePath',
(globalNodePath) =>
@globalPath = globalNodePath
@subs.add atom.config.observe 'linter-sass-lint.resolvePathsRelativeToConfig',
(resolvePathsRelativeToConfig) =>
@resolvePathsRelativeToConfig = resolvePathsRelativeToConfig

deactivate: ->
@subs.dispose()

# return a relative path for a file within our project
# we use this to match it to our include/exclude glob string within sass-lint's
# user specified config
getFilePath: (path) ->
relative = atom.project.relativizePath(path)
getFilePath: (absolutePath, configFilePath) ->
path = require('path')
if @resolvePathsRelativeToConfig
return path.relative(path.dirname(configFilePath), absolutePath)
else
return atom.project.relativizePath(absolutePath)[1]

# Determines whether to use the sass-lint package included with linter-sass-lint
# or the users globally installed sass-lint version
Expand Down Expand Up @@ -131,7 +143,7 @@ module.exports =

try
compiledConfig = linter.getConfig({}, config)
relativePath = this.getFilePath(filePath)[1]
relativePath = this.getFilePath(filePath, config)

if globule.isMatch(compiledConfig.files.include, relativePath) and not globule.isMatch(compiledConfig.files.ignore, relativePath)
result = linter.lintText({
Expand Down
8 changes: 8 additions & 0 deletions spec/fixtures/config/.relative-config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
options:
formatter: json
merge-default-rules: false
files:
include: '../files/ignored.scss'
rules:
no-color-literals: 1
no-ids: 2
82 changes: 82 additions & 0 deletions spec/linter-sass-lint-resolve-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
'use babel';
const lint = require('../lib/main.coffee').provideLinter().lint;

describe('The sass-lint provider for Linter - resolve paths relative to config file', () => {
const configFile = `${__dirname}/fixtures/config/.relative-config.yml`;

beforeEach(() => {
atom.workspace.destroyActivePaneItem();
waitsForPromise(() => {
atom.packages.activatePackage('linter-sass-lint');
return atom.packages.activatePackage('language-sass');
});
});
describe('checks ignored.scss and', () => {
let editor = null;
beforeEach(() => {
waitsForPromise(() => {
atom.config.set('linter-sass-lint.configFile', configFile);
atom.config.set('linter-sass-lint.resolvePathsRelativeToConfig', true);
return atom.workspace.open(`${__dirname}/fixtures/files/ignored.scss`).then(openEditor => {
editor = openEditor;
});
});
});

it('finds at least one message', () => {
const messages = lint(editor);
expect(messages.length).toBeGreaterThan(0);
});

it('verifies the first message', () => {
const messages = lint(editor);
const slDocUrl = 'https://github.com/sasstools/sass-lint/tree/master/docs/rules/no-ids.md';
const attributes = `href="${slDocUrl}" class="badge badge-flexible sass-lint"`;
const warningMarkup = `<a ${attributes}>no-ids</a>`;
const warnId = ' ID selectors not allowed';
expect(messages[0].type).toBeDefined();
expect(messages[0].type).toEqual('Error');
expect(messages[0].html).toBeDefined();
expect(messages[0].html).toEqual(`${warningMarkup}${warnId}`);
expect(messages[0].filePath).toBeDefined();
expect(messages[0].filePath).toMatch(/.+ignored\.scss$/);
expect(messages[0].range).toBeDefined();
expect(messages[0].range.length).toEqual(2);
expect(messages[0].range).toEqual([[0, 0], [0, 1]]);
});

it('verifies the second message', () => {
const messages = lint(editor);
const slDocUrl = 'https://github.com/sasstools/sass-lint/tree/master/docs/rules/no-color-literals.md';
const attributes = `href="${slDocUrl}" class="badge badge-flexible sass-lint"`;
const warningMarkup = `<a ${attributes}>no-color-literals</a>`;
const warnId = ' Color literals such as \'red\' should only be used in variable declarations';
expect(messages[1].type).toBeDefined();
expect(messages[1].type).toEqual('Warning');
expect(messages[1].html).toBeDefined();
expect(messages[1].html).toEqual(`${warningMarkup}${warnId}`);
expect(messages[1].filePath).toBeDefined();
expect(messages[1].filePath).toMatch(/.+ignored\.scss$/);
expect(messages[1].range).toBeDefined();
expect(messages[1].range.length).toEqual(2);
expect(messages[1].range).toEqual([[1, 9], [1, 10]]);
});
});

describe('checks failure.scss and', () => {
let editor = null;
beforeEach(() => {
waitsForPromise(() => {
atom.config.set('linter-sass-lint.configFile', configFile);
return atom.workspace.open(`${__dirname}/fixtures/files/failure.scss`).then(openEditor => {
editor = openEditor;
});
});
});

it('finds nothing wrong with the valid file', () => {
const messages = lint(editor);
expect(messages.length).toEqual(0);
});
});
});

0 comments on commit 1797299

Please sign in to comment.