Skip to content

Commit

Permalink
issue-813 -- DRYs up helper functions. Adds conditional messaging to …
Browse files Browse the repository at this point in the history
…no-protocols.
  • Loading branch information
danwaz committed Aug 25, 2016
1 parent cf9472a commit d506b26
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 14 deletions.
10 changes: 3 additions & 7 deletions lib/rules/no-domains.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
'use strict';

var helpers = require('../helpers');
var url = require('url');

var stripQuotes = function (str) {
return str.substring(1, str.length - 1);
};
var helpers = require('../helpers'),
url = require('url');

module.exports = {
'name': 'no-domains',
Expand All @@ -16,7 +12,7 @@ module.exports = {
ast.traverseByType('uri', function (uri) {
uri.traverse(function (item) {
if (item.is('string')) {
var stripped = stripQuotes(item.content),
var stripped = helpers.stripQuotes(item.content),
parsedUrl = url.parse(stripped, false, true);

if (parsedUrl.host) {
Expand Down
13 changes: 6 additions & 7 deletions lib/rules/no-url-protocols.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ var helpers = require('../helpers');
var isUrlRegex = /^(https?:)?\/\//,
noProtocolRegex = /^(https?:)\/\//;

var stripQuotes = function (str) {
return str.substring(1, str.length - 1);
};

module.exports = {
'name': 'no-url-protocols',
'defaults': {
Expand All @@ -20,17 +16,20 @@ module.exports = {
ast.traverseByType('uri', function (uri) {
uri.traverse(function (item) {
if (item.is('string')) {
var stripped = stripQuotes(item.content),
var stripped = helpers.stripQuotes(item.content),
regexSelector = parser.options['enforce-domains'] ?
isUrlRegex : noProtocolRegex;
isUrlRegex : noProtocolRegex,
message = parser.options['enforce-domains'] ?
"Protocols and domains in URLs are disallowed" :
"Protocols in URLS are disallowed";

if (stripped.match(regexSelector)) {
result = helpers.addUnique(result, {
'ruleId': parser.rule.name,
'severity': parser.severity,
'line': item.end.line,
'column': item.end.column,
'message': 'Protocols and domains in URLs are disallowed'
'message': message
});
}
}
Expand Down

0 comments on commit d506b26

Please sign in to comment.