From 05094c8b0e3bfe2cdd826c259c6fc9cee9157a65 Mon Sep 17 00:00:00 2001 From: Roman Dvornov Date: Wed, 18 Nov 2020 23:07:34 +0100 Subject: [PATCH] Some renamings, tweak descriptions --- LICENSE | 2 +- README.md | 93 +++++++++++++++++++++++++++---- lib/{validators.js => helpers.js} | 16 +++--- lib/index.js | 2 +- lib/validate.js | 48 ++++++++-------- 5 files changed, 116 insertions(+), 45 deletions(-) rename lib/{validators.js => helpers.js} (81%) diff --git a/LICENSE b/LICENSE index 29e0d26..a62639e 100644 --- a/LICENSE +++ b/LICENSE @@ -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 diff --git a/README.md b/README.md index 5324897..dae355b 100644 --- a/README.md +++ b/README.md @@ -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: ' || || ', +// 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: || || \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 ``` @@ -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) diff --git a/lib/validators.js b/lib/helpers.js similarity index 81% rename from lib/validators.js rename to lib/helpers.js index 8c2a3ed..9cd7ef7 100644 --- a/lib/validators.js +++ b/lib/helpers.js @@ -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; diff --git a/lib/index.js b/lib/index.js index 701959a..22271d0 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,5 +1,5 @@ module.exports = { - ...require('./validators.js'), + ...require('./helpers.js'), ...require('./validate'), reporters: require('./reporter') }; diff --git a/lib/validate.js b/lib/validate.js index 488bb66..ece7fc1 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -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 = []; @@ -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;