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

Commit 1797299

Browse files
authored
Merge pull request #101 from AtomLinter/resolve-relative-paths
Resolve relative paths
2 parents 4b05b05 + 7f84d7b commit 1797299

File tree

5 files changed

+111
-3
lines changed

5 files changed

+111
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 1.6.0
2+
- 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)
3+
- The usual third party package updates
4+
15
## 1.5.0
26
- Allows the inclusion of files with extra extensions such as 'file.scss.liquid'
37
- Updated to Atom linter ^5.0.1

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ There are three options you can configure either within the plugin or by editing
4646

4747
* `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`.
4848

49+
* `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.
50+
4951
### Extra File Extensions
5052

5153
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.

lib/main.coffee

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ module.exports =
99
type: 'boolean'
1010
description: 'and a .sass-lint.yml file is not specified in the .sass-lint.yml Path option'
1111
default: false
12+
resolvePathsRelativeToConfig:
13+
title: 'Resolve paths in configuration relative to config file'
14+
type: 'boolean'
15+
description: 'Instead of the default where paths are resolved relative to the project root'
16+
default: 'false'
1217
configFile:
1318
title: '.sass-lint.yml Config File'
1419
description: 'A .sass-lint.yml file to use/fallback to if no config file is found in the current project root'
@@ -41,15 +46,22 @@ module.exports =
4146
@subs.add atom.config.observe 'linter-sass-lint.globalNodePath',
4247
(globalNodePath) =>
4348
@globalPath = globalNodePath
49+
@subs.add atom.config.observe 'linter-sass-lint.resolvePathsRelativeToConfig',
50+
(resolvePathsRelativeToConfig) =>
51+
@resolvePathsRelativeToConfig = resolvePathsRelativeToConfig
4452

4553
deactivate: ->
4654
@subs.dispose()
4755

4856
# return a relative path for a file within our project
4957
# we use this to match it to our include/exclude glob string within sass-lint's
5058
# user specified config
51-
getFilePath: (path) ->
52-
relative = atom.project.relativizePath(path)
59+
getFilePath: (absolutePath, configFilePath) ->
60+
path = require('path')
61+
if @resolvePathsRelativeToConfig
62+
return path.relative(path.dirname(configFilePath), absolutePath)
63+
else
64+
return atom.project.relativizePath(absolutePath)[1]
5365

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

132144
try
133145
compiledConfig = linter.getConfig({}, config)
134-
relativePath = this.getFilePath(filePath)[1]
146+
relativePath = this.getFilePath(filePath, config)
135147

136148
if globule.isMatch(compiledConfig.files.include, relativePath) and not globule.isMatch(compiledConfig.files.ignore, relativePath)
137149
result = linter.lintText({
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
options:
2+
formatter: json
3+
merge-default-rules: false
4+
files:
5+
include: '../files/ignored.scss'
6+
rules:
7+
no-color-literals: 1
8+
no-ids: 2
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
'use babel';
2+
const lint = require('../lib/main.coffee').provideLinter().lint;
3+
4+
describe('The sass-lint provider for Linter - resolve paths relative to config file', () => {
5+
const configFile = `${__dirname}/fixtures/config/.relative-config.yml`;
6+
7+
beforeEach(() => {
8+
atom.workspace.destroyActivePaneItem();
9+
waitsForPromise(() => {
10+
atom.packages.activatePackage('linter-sass-lint');
11+
return atom.packages.activatePackage('language-sass');
12+
});
13+
});
14+
describe('checks ignored.scss and', () => {
15+
let editor = null;
16+
beforeEach(() => {
17+
waitsForPromise(() => {
18+
atom.config.set('linter-sass-lint.configFile', configFile);
19+
atom.config.set('linter-sass-lint.resolvePathsRelativeToConfig', true);
20+
return atom.workspace.open(`${__dirname}/fixtures/files/ignored.scss`).then(openEditor => {
21+
editor = openEditor;
22+
});
23+
});
24+
});
25+
26+
it('finds at least one message', () => {
27+
const messages = lint(editor);
28+
expect(messages.length).toBeGreaterThan(0);
29+
});
30+
31+
it('verifies the first message', () => {
32+
const messages = lint(editor);
33+
const slDocUrl = 'https://github.com/sasstools/sass-lint/tree/master/docs/rules/no-ids.md';
34+
const attributes = `href="${slDocUrl}" class="badge badge-flexible sass-lint"`;
35+
const warningMarkup = `<a ${attributes}>no-ids</a>`;
36+
const warnId = ' ID selectors not allowed';
37+
expect(messages[0].type).toBeDefined();
38+
expect(messages[0].type).toEqual('Error');
39+
expect(messages[0].html).toBeDefined();
40+
expect(messages[0].html).toEqual(`${warningMarkup}${warnId}`);
41+
expect(messages[0].filePath).toBeDefined();
42+
expect(messages[0].filePath).toMatch(/.+ignored\.scss$/);
43+
expect(messages[0].range).toBeDefined();
44+
expect(messages[0].range.length).toEqual(2);
45+
expect(messages[0].range).toEqual([[0, 0], [0, 1]]);
46+
});
47+
48+
it('verifies the second message', () => {
49+
const messages = lint(editor);
50+
const slDocUrl = 'https://github.com/sasstools/sass-lint/tree/master/docs/rules/no-color-literals.md';
51+
const attributes = `href="${slDocUrl}" class="badge badge-flexible sass-lint"`;
52+
const warningMarkup = `<a ${attributes}>no-color-literals</a>`;
53+
const warnId = ' Color literals such as \'red\' should only be used in variable declarations';
54+
expect(messages[1].type).toBeDefined();
55+
expect(messages[1].type).toEqual('Warning');
56+
expect(messages[1].html).toBeDefined();
57+
expect(messages[1].html).toEqual(`${warningMarkup}${warnId}`);
58+
expect(messages[1].filePath).toBeDefined();
59+
expect(messages[1].filePath).toMatch(/.+ignored\.scss$/);
60+
expect(messages[1].range).toBeDefined();
61+
expect(messages[1].range.length).toEqual(2);
62+
expect(messages[1].range).toEqual([[1, 9], [1, 10]]);
63+
});
64+
});
65+
66+
describe('checks failure.scss and', () => {
67+
let editor = null;
68+
beforeEach(() => {
69+
waitsForPromise(() => {
70+
atom.config.set('linter-sass-lint.configFile', configFile);
71+
return atom.workspace.open(`${__dirname}/fixtures/files/failure.scss`).then(openEditor => {
72+
editor = openEditor;
73+
});
74+
});
75+
});
76+
77+
it('finds nothing wrong with the valid file', () => {
78+
const messages = lint(editor);
79+
expect(messages.length).toEqual(0);
80+
});
81+
});
82+
});

0 commit comments

Comments
 (0)