Skip to content

Commit

Permalink
Some renamings, tweak descriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
lahmatiy committed Nov 18, 2020
1 parent 79eba95 commit 05094c8
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 45 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (C) 2016-2018 by Roman Dvornov
Copyright (C) 2016-2020 by Roman Dvornov

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
93 changes: 82 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,90 @@

# CSS Tree Validator

CSS validator built on [CSSTree](https://github.com/csstree/csstree)

## How to use:

### NPM package

```bash
> npm install csstree-validator
```

Manualy validate CSS string or [CSSTree's AST](https://github.com/csstree/csstree/blob/master/docs/ast.md):

```js
const { validate } = require('./lib');

console.log(validate('.class { pading: 10px; border: 1px super red }', 'demo/example.css'));
// [
// SyntaxError [SyntaxReferenceError]: Unknown property `pading` {
// reference: 'pading',
// property: 'pading',
// offset: 9,
// line: 1,
// column: 10
// },
// SyntaxError [SyntaxMatchError]: Mismatch {
// message: 'Invalid value for `border` property',
// rawMessage: 'Mismatch',
// syntax: '<line-width> || <line-style> || <color>',
// css: '1px super red',
// mismatchOffset: 4,
// mismatchLength: 5,
// offset: 35,
// line: 1,
// column: 36,
// loc: { source: 'demo/example.css', start: [Object], end: [Object] },
// property: 'border',
// details: 'Mismatch\n' +
// ' syntax: <line-width> || <line-style> || <color>\n' +
// ' value: 1px super red\n' +
// ' ------------^'
// }
// ]
```

Another option is to use helpers to validate a file or directory and buildin reporters:

```js
const { validateFile } = require('csstree-validator');
const reporter = require('csstree-validator').reporters.checkstyle;

console.log(reporter(validateFile('/path/to/style.css')));
```

#### API

Validate methods:

* validateAtrule(node)
* validateAtrulePrelude(atrule, prelude, preludeLoc)
* validateAtruleDescriptor(atrule, descriptor, value, descriptorLoc)
* validateDeclaration(property, value, valueLoc)
* validateRule(node)
* validate(css, filename)

Helpers:

* validateDictionary(dictionary)
* validateString(css, filename)
* validateFile(filename)
* validateFileList(list)
* validatePath(searchPath, filter)
* validatePathList(pathList, filter)

Reporters

* json
* console
* checkstyle
* gnu

### CLI (terminal command)

```bash
> npm i -g csstree-validator
> npm install -g csstree-validator
> csstree-validator /path/to/style.css
```

Expand All @@ -23,18 +103,9 @@ Options:
-v, --version Output version
```

## API

```js
var validateFile = require('csstree-validator').validateFile;
var reporter = require('csstree-validator').reporters.checkstyle;

console.log(reporter(validateFile('/path/to/style.css')));
```

## Ready to use

Some plugins that are using `csstree-validator`:
Plugins that are using `csstree-validator`:

* [Sublime plugin](https://github.com/csstree/SublimeLinter-contrib-csstree)
* [VS Code plugin](https://github.com/csstree/vscode-plugin)
Expand Down
16 changes: 8 additions & 8 deletions lib/validators.js → lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,22 @@ function validateFileList(list) {
}, {});
}

function validatePath(searchPath, shouldBeValidated) {
if (typeof shouldBeValidated !== 'function') {
shouldBeValidated = defaultShouldBeValidated;
function validatePath(searchPath, filter) {
if (typeof filter !== 'function') {
filter = defaultShouldBeValidated;
}

return validateFileList(collectFiles(searchPath, shouldBeValidated));
return validateFileList(collectFiles(searchPath, filter));
}

function validatePathList(pathList, shouldBeValidated) {
if (typeof shouldBeValidated !== 'function') {
shouldBeValidated = defaultShouldBeValidated;
function validatePathList(pathList, filter) {
if (typeof filter !== 'function') {
filter = defaultShouldBeValidated;
}

const fileList = Object.keys(
pathList.reduce(function(result, searchPath) {
collectFiles(searchPath, shouldBeValidated).forEach(function(filename) {
collectFiles(searchPath, filter).forEach(function(filename) {
result[filename] = true;
});
return result;
Expand Down
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
...require('./validators.js'),
...require('./helpers.js'),
...require('./validate'),
reporters: require('./reporter')
};
48 changes: 24 additions & 24 deletions lib/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,6 @@ function isTargetError(error) {
return error;
}

function validateAtruleDescriptor(atrule, descriptor, value, descriptorLoc) {
const errors = [];
let error;

if (error = isTargetError(syntax.checkAtruleDescriptorName(atrule, descriptor))) {
errors.push(Object.assign(error, {
atrule,
descriptor,
...descriptorLoc || (value && value.loc && value.loc.start)
}));
} else {
if (error = isTargetError(syntax.matchAtruleDescriptor(atrule, descriptor, value).error)) {
errors.push(Object.assign(error, {
atrule,
descriptor,
...error.rawMessage === 'Mismatch' &&
{ details: error.message, message: 'Invalid value for `' + descriptor + '` descriptor' }
}));
}
}

return errors;
}

function validateAtrule(node) {
const atrule = node.name;
const errors = [];
Expand Down Expand Up @@ -91,6 +67,30 @@ function validateAtrulePrelude(atrule, prelude, preludeLoc) {
return errors;
}

function validateAtruleDescriptor(atrule, descriptor, value, descriptorLoc) {
const errors = [];
let error;

if (error = isTargetError(syntax.checkAtruleDescriptorName(atrule, descriptor))) {
errors.push(Object.assign(error, {
atrule,
descriptor,
...descriptorLoc || (value && value.loc && value.loc.start)
}));
} else {
if (error = isTargetError(syntax.matchAtruleDescriptor(atrule, descriptor, value).error)) {
errors.push(Object.assign(error, {
atrule,
descriptor,
...error.rawMessage === 'Mismatch' &&
{ details: error.message, message: 'Invalid value for `' + descriptor + '` descriptor' }
}));
}
}

return errors;
}

function validateDeclaration(property, value, valueLoc) {
const errors = [];
let error;
Expand Down

0 comments on commit 05094c8

Please sign in to comment.