Skip to content

Commit

Permalink
Merge 3d0b835 into c534817
Browse files Browse the repository at this point in the history
  • Loading branch information
ismay committed Jan 31, 2018
2 parents c534817 + 3d0b835 commit 2a3cb57
Show file tree
Hide file tree
Showing 15 changed files with 492 additions and 238 deletions.
1 change: 0 additions & 1 deletion .eslintignore

This file was deleted.

10 changes: 2 additions & 8 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
{
"extends": ["airbnb-base/legacy", "prettier"],
"env": {
"node": true
},
"extends": [
"airbnb-base/legacy"
],
"rules": {
"comma-dangle": [2, "never"],
"func-names": [2, "as-needed"],
"no-warning-comments": [2, { "terms": ["todo", "fixme"], "location": "anywhere" }],
"semi": [2, "never"]
"func-names": 0
}
}

3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ node_modules
npm-debug.log
yarn-error.log
package-lock.json

# Coveralls coverage
/coverage
7 changes: 7 additions & 0 deletions .lintstagedrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"**/*.js": [
"prettier --write",
"git add",
"eslint"
]
}
11 changes: 11 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Dotfiles
.editorconfig
.eslintrc
.gitattributes
.gitignore
.lintstagedrc
.prettierrc
.travis.yml

# Tests
/test
7 changes: 7 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"printWidth": 100,
"useTabs": false,
"singleQuote": true,
"tabWidth": 2,
"semi": true
}
8 changes: 6 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ cache:
notifications:
email: false
node_js:
- "4"
- "6"
- "8"
jobs:
include:
- stage: coverage
node_js: "8"
script:
- npm run test:coverage
- stage: lint
node_js: "8"
script:
- npm run lint
- npm run lint:prettier
- npm run lint:js
54 changes: 0 additions & 54 deletions index.js

This file was deleted.

125 changes: 125 additions & 0 deletions lib/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`stylelint-no-unsupported-browser-features ignore option should ignore a single property 1`] = `
Array [
Object {
"deprecations": Array [],
"invalidOptionWarnings": Array [],
"parseErrors": Array [],
"source": "<input css 5>",
"warnings": Array [],
},
]
`;

exports[`stylelint-no-unsupported-browser-features ignore option should ignore multiple properties 1`] = `
Array [
Object {
"deprecations": Array [],
"invalidOptionWarnings": Array [],
"parseErrors": Array [],
"source": "<input css 6>",
"warnings": Array [],
},
]
`;

exports[`stylelint-no-unsupported-browser-features multiple browsers should allow display:table for IE 8 and IE 9 1`] = `
Array [
Object {
"deprecations": Array [],
"invalidOptionWarnings": Array [],
"parseErrors": Array [],
"source": "<input css 3>",
"warnings": Array [],
},
]
`;

exports[`stylelint-no-unsupported-browser-features multiple browsers should disallow display:table for IE 6 and IE 7 1`] = `
Array [
Object {
"deprecations": Array [],
"errored": true,
"invalidOptionWarnings": Array [],
"parseErrors": Array [],
"source": "<input css 4>",
"warnings": Array [
Object {
"column": 7,
"line": 1,
"rule": "plugin/no-unsupported-browser-features",
"severity": "error",
"text": "Unexpected browser feature \\"css-table\\" is not supported by IE 6,7 (plugin/no-unsupported-browser-features)",
},
],
},
]
`;

exports[`stylelint-no-unsupported-browser-features single browser should allow display:table for IE 8 1`] = `
Array [
Object {
"deprecations": Array [],
"invalidOptionWarnings": Array [],
"parseErrors": Array [],
"source": "<input css 1>",
"warnings": Array [],
},
]
`;

exports[`stylelint-no-unsupported-browser-features single browser should disallow display:table for IE 6 1`] = `
Array [
Object {
"deprecations": Array [],
"errored": true,
"invalidOptionWarnings": Array [],
"parseErrors": Array [],
"source": "<input css 2>",
"warnings": Array [
Object {
"column": 7,
"line": 1,
"rule": "plugin/no-unsupported-browser-features",
"severity": "error",
"text": "Unexpected browser feature \\"css-table\\" is not supported by IE 6 (plugin/no-unsupported-browser-features)",
},
],
},
]
`;

exports[`stylelint-no-unsupported-browser-features validate options should validate the browsers option 1`] = `
Array [
Object {
"deprecations": Array [],
"errored": true,
"invalidOptionWarnings": Array [
Object {
"text": "Invalid value \\"1\\" for option \\"browsers\\" of rule \\"plugin/no-unsupported-browser-features\\"",
},
],
"parseErrors": Array [],
"source": "<input css 7>",
"warnings": Array [],
},
]
`;

exports[`stylelint-no-unsupported-browser-features validate options should validate the ignore option 1`] = `
Array [
Object {
"deprecations": Array [],
"errored": true,
"invalidOptionWarnings": Array [
Object {
"text": "Invalid value \\"1\\" for option \\"ignore\\" of rule \\"plugin/no-unsupported-browser-features\\"",
},
],
"parseErrors": Array [],
"source": "<input css 8>",
"warnings": Array [],
},
]
`;
20 changes: 0 additions & 20 deletions lib/cleanWarningText.js

This file was deleted.

98 changes: 98 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
var _ = require('lodash');
var stylelint = require('stylelint');
var doiuse = require('doiuse');
var Result = require('postcss/lib/result');

/**
* Plugin settings
*/

var ruleName = 'plugin/no-unsupported-browser-features';
var messages = stylelint.utils.ruleMessages(ruleName, {
rejected: function rejectRule(details) {
return 'Unexpected browser feature ' + details;
}
});

/**
* The allowed options
*/

var optionsSchema = {
browsers: [_.isString],
ignore: [_.isString]
};

/**
* Utilities
*/

function cleanWarningText(warningText) {
// Get index of feature Id
var featureIdIndex = warningText.lastIndexOf('(');

// Get feature Id, then replace brackets with quotes
var featureId = warningText.slice(featureIdIndex, warningText.length).replace(/\(|\)/g, '"');

// Get start of support text i.e. "x not supported by...", or "y only partially supported by..."
var browserSupportStartIndex =
warningText.indexOf('not') !== -1 ? warningText.indexOf('not') : warningText.indexOf('only');

// Get browser support text, then strip brackets.
var browserSupport = warningText
.slice(browserSupportStartIndex, featureIdIndex - 1)
.replace(/\(|\)|:/g, '');

var cleanedWarningText = featureId + ' is ' + browserSupport;

return cleanedWarningText;
}

function parseOptions(options) {
var doiuseOptions = {};

if (options && options.browsers) {
doiuseOptions.browsers = options.browsers;
}

if (options && options.ignore) {
doiuseOptions.ignore = options.ignore;
}

return doiuseOptions;
}

/**
* The main plugin rule
*/

function ruleFunction(on, options) {
return function postCssPlugin(root, result) {
var validOptions = stylelint.utils.validateOptions(result, ruleName, {
actual: options,
possible: optionsSchema,
optional: true
});

var doiuseOptions = parseOptions(options);
var doiuseResult = new Result();

if (!validOptions) return;

doiuse(doiuseOptions).postcss(root, doiuseResult);
doiuseResult.warnings().forEach(function reportWarning(doiuseWarning) {
stylelint.utils.report({
ruleName: ruleName,
result: result,
message: messages.rejected(cleanWarningText(doiuseWarning.text)),
node: doiuseWarning.node,
line: doiuseWarning.line,
column: doiuseWarning.column
});
});
};
}

module.exports = stylelint.createPlugin(ruleName, ruleFunction);
module.exports.ruleName = ruleName;
module.exports.messages = messages;
Loading

0 comments on commit 2a3cb57

Please sign in to comment.