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

Commit 409960a

Browse files
committed
Add helper tests constants and support extra syntaxes
1 parent b379bea commit 409960a

File tree

4 files changed

+86
-6
lines changed

4 files changed

+86
-6
lines changed

lib/constants.coffee

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports =
2+
SASSLINT_DOC_URL: 'https://github.com/sasstools/sass-lint/tree/master/docs/rules',
3+
VALID_SYNTAXES: ['scss', 'sass']

lib/helpers.coffee

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,35 @@
1+
path = require 'path'
2+
{SASSLINT_DOC_URL, VALID_SYNTAXES} = require './constants.coffee'
3+
14
module.exports =
25

3-
# Constructs the rule URI from the rule ID provided
6+
###*
7+
* Function to construct the rule URI from the rule ID provided
8+
* @param {string} ruleId - The rule name / id
9+
* @return {string} The rule URL
10+
###
411
getRuleURI: (ruleId) ->
5-
sassLintDocs = 'https://github.com/sasstools/sass-lint/tree/master/docs/rules'
6-
return sassLintDocs + '/' + ruleId + '.md'
12+
return SASSLINT_DOC_URL + '/' + ruleId + '.md'
13+
14+
###*
15+
* Function to check a file base / extension for valid extensions to use with sass-lint
16+
* @param {string} syntax - The syntax to check
17+
* @return {boolean} Whether or not the syntax is valid for sass-lint
18+
###
19+
isValidSyntax: (syntax) ->
20+
return VALID_SYNTAXES.indexOf(syntax) isnt -1
21+
22+
###*
23+
* Function to check a file base / extension for valid extensions to use with sass-lint
24+
* @param {string} filePath - The filepath to check
25+
* @return {string} The syntax we wish to pass to sass-lint
26+
###
27+
getFileSyntax: (filePath) ->
28+
existingSyntax = path.extname(filePath).slice(1)
29+
if @isValidSyntax(existingSyntax) is false
30+
base = path.parse(filePath).base.split('.')
31+
syntax = (item for item in base when @isValidSyntax(item))
32+
if syntax.length
33+
return syntax[0]
34+
35+
return existingSyntax

lib/main.coffee

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ module.exports =
7575
provideLinter: ->
7676
{find} = require 'atom-linter'
7777
globule = require 'globule'
78-
{getRuleURI} = require './helpers'
78+
helpers = require './helpers'
7979

8080
provider =
8181
name: 'sass-lint'
@@ -136,7 +136,7 @@ module.exports =
136136
if globule.isMatch(compiledConfig.files.include, relativePath) and not globule.isMatch(compiledConfig.files.ignore, relativePath)
137137
result = linter.lintText({
138138
text: editor.getText(),
139-
format: path.extname(filePath).slice(1),
139+
format: helpers.getFileSyntax(filePath),
140140
filename: filePath
141141
}, {}, config)
142142
catch error
@@ -169,7 +169,7 @@ module.exports =
169169
line = if msg.line then msg.line - 1 else 0
170170
col = if msg.column then msg.column - 1 else 0
171171
text = if msg.message then ' ' + msg.message else 'Unknown Error'
172-
ruleHref = getRuleURI(msg.ruleId)
172+
ruleHref = helpers.getRuleURI(msg.ruleId)
173173
html = '<a href="'+ ruleHref + '" class="badge badge-flexible sass-lint">' + msg.ruleId + '</a>' + text
174174

175175
result = {

spec/helpers-spec.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
'use babel';
2+
3+
import { SASSLINT_DOC_URL } from '../lib/constants.coffee';
4+
5+
const helpers = require('../lib/helpers.coffee');
6+
7+
describe('helpers', () => {
8+
describe('getRuleURI', () => {
9+
it('should return the correct rule URL', () => {
10+
const ruleId = 'no-ids';
11+
const result = helpers.getRuleURI(ruleId);
12+
13+
expect(result).toEqual(`${SASSLINT_DOC_URL}/${ruleId}.md`);
14+
});
15+
});
16+
17+
describe('isValidSyntax', () => {
18+
it('should return true if a supported syntax is passed', () => {
19+
expect(helpers.isValidSyntax('scss')).toEqual(true);
20+
});
21+
22+
it('should return false if a supported syntax is not passed', () => {
23+
expect(helpers.isValidSyntax('html')).toEqual(false);
24+
});
25+
});
26+
27+
describe('getFileSyntax', () => {
28+
it('it should return scss if a scss filename is provided', () => {
29+
expect(helpers.getFileSyntax('test/file.scss')).toEqual('scss');
30+
});
31+
32+
it('it should return sass if a sass filename is provided', () => {
33+
expect(helpers.getFileSyntax('test/file.sass')).toEqual('sass');
34+
});
35+
36+
it('it should return scss if a scss.liquid filename is provided', () => {
37+
expect(helpers.getFileSyntax('test/file.scss.liquid')).toEqual('scss');
38+
});
39+
40+
it('it should return sass if a sass.liquid filename is provided', () => {
41+
expect(helpers.getFileSyntax('test/file.sass.liquid')).toEqual('sass');
42+
});
43+
44+
it('it should return html if a html filename is provided', () => {
45+
expect(helpers.getFileSyntax('test/file.html')).toEqual('html');
46+
});
47+
});
48+
});

0 commit comments

Comments
 (0)