-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor validate, added atrule validation
- Loading branch information
Showing
16 changed files
with
462 additions
and
265 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,5 @@ | ||
const { | ||
validatePathList, | ||
validatePath, | ||
validateFile, | ||
validateDictionary, | ||
validateString | ||
} = require('./validators.js'); | ||
|
||
module.exports = { | ||
validatePathList, | ||
validatePath, | ||
validateFile, | ||
validateDictionary, | ||
validateString, | ||
...require('./validators.js'), | ||
...require('./validate'), | ||
reporters: require('./reporter') | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,172 @@ | ||
const csstree = require('css-tree'); | ||
const syntax = csstree.lexer; | ||
|
||
module.exports = function validate(css, filename) { | ||
function isTargetError(error) { | ||
if (!error) { | ||
return null; | ||
} | ||
|
||
if (error.name !== 'SyntaxError' && | ||
error.name !== 'SyntaxMatchError' && | ||
error.name !== 'SyntaxReferenceError') { | ||
return null; | ||
} | ||
|
||
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 = []; | ||
let error; | ||
|
||
if (error = isTargetError(syntax.checkAtruleName(atrule))) { | ||
errors.push(Object.assign(error, { | ||
...node.loc && node.loc.start | ||
})); | ||
return errors; | ||
} | ||
|
||
errors.push(...validateAtrulePrelude( | ||
atrule, | ||
node.prelude, | ||
(node.prelude && node.prelude.loc && node.prelude.loc.start) || (node.loc && node.loc.start) | ||
)); | ||
|
||
if (node.block && node.block.children) { | ||
node.block.children.forEach(child => { | ||
if (child.type === 'Declaration') { | ||
errors.push(...validateAtruleDescriptor( | ||
atrule, | ||
child.property, | ||
child.value, | ||
child.loc && child.loc.start | ||
)); | ||
} | ||
}); | ||
} | ||
|
||
return errors; | ||
} | ||
|
||
function validateAtrulePrelude(atrule, prelude, preludeLoc) { | ||
const errors = []; | ||
let error; | ||
|
||
if (error = isTargetError(syntax.checkAtrulePrelude(atrule, prelude))) { | ||
errors.push(Object.assign(error, { | ||
...preludeLoc || (prelude && prelude.loc && prelude.loc.start) | ||
})); | ||
} else if (error = isTargetError(syntax.matchAtrulePrelude(atrule, prelude).error)) { | ||
errors.push(Object.assign(error, { | ||
...error.rawMessage === 'Mismatch' && | ||
{ details: error.message, message: 'Invalid value for `@' + atrule + '` prelude' } | ||
})); | ||
} | ||
|
||
return errors; | ||
} | ||
|
||
function validateDeclaration(property, value, valueLoc) { | ||
const errors = []; | ||
let error; | ||
|
||
if (error = isTargetError(syntax.checkPropertyName(property))) { | ||
errors.push(Object.assign(error, { | ||
property, | ||
...valueLoc || (value && value.loc && value.loc.start) | ||
})); | ||
} else if (error = isTargetError(syntax.matchProperty(property, value).error)) { | ||
errors.push(Object.assign(error, { | ||
property, | ||
...error.rawMessage === 'Mismatch' && | ||
{ details: error.message, message: 'Invalid value for `' + property + '` property' } | ||
})); | ||
} | ||
|
||
return errors; | ||
} | ||
|
||
function validateRule(node) { | ||
const errors = []; | ||
|
||
if (node.block && node.block.children) { | ||
node.block.children.forEach(child => { | ||
if (child.type === 'Declaration') { | ||
errors.push(...validateDeclaration( | ||
child.property, | ||
child.value, | ||
child.loc && child.loc.start | ||
)); | ||
} | ||
}); | ||
} | ||
|
||
return errors; | ||
} | ||
|
||
function validate(css, filename) { | ||
const errors = []; | ||
const ast = csstree.parse(css, { | ||
filename, | ||
positions: true, | ||
onParseError(error) { | ||
errors.push(error); | ||
const ast = typeof css !== 'string' | ||
? css | ||
: csstree.parse(css, { | ||
filename, | ||
positions: true, | ||
parseAtrulePrelude: false, | ||
parseRulePrelude: false, | ||
parseValue: false, | ||
parseCustomProperty: false, | ||
onParseError(error) { | ||
errors.push(error); | ||
} | ||
}); | ||
|
||
csstree.walk(ast, { | ||
visit: 'Atrule', | ||
enter(node) { | ||
errors.push(...validateAtrule(node)); | ||
} | ||
}); | ||
|
||
csstree.walk(ast, { | ||
visit: 'Declaration', | ||
visit: 'Rule', | ||
enter(node) { | ||
const { error } = syntax.matchDeclaration(node); | ||
|
||
if (error) { | ||
let message = error.rawMessage || error.message || error; | ||
|
||
// ignore errors except those which make sense | ||
if (error.name !== 'SyntaxMatchError' && | ||
error.name !== 'SyntaxReferenceError') { | ||
return; | ||
} | ||
|
||
if (message === 'Mismatch') { | ||
message = 'Invalid value for `' + node.property + '`'; | ||
} | ||
|
||
errors.push({ | ||
name: error.name, | ||
node, | ||
loc: error.loc || node.loc, | ||
line: error.line || node.loc && node.loc.start && node.loc.start.line, | ||
column: error.column || node.loc && node.loc.start && node.loc.start.column, | ||
property: node.property, | ||
message, | ||
error | ||
}); | ||
} | ||
errors.push(...validateRule(node)); | ||
} | ||
}); | ||
|
||
return errors; | ||
}; | ||
|
||
module.exports = { | ||
validateAtrule, | ||
validateAtrulePrelude, | ||
validateAtruleDescriptor, | ||
validateRule, | ||
validateDeclaration, | ||
validate | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<checkstyle version="4.3"> | ||
<file name="match.css"> | ||
<error line="1" column="16" severity="error" message="Invalid value for `color`" source="csstree-validator"/> | ||
<error line="1" column="33" severity="error" message="Invalid value for `border`" source="csstree-validator"/> | ||
<error line="1" column="16" severity="error" message="Invalid value for `color` property" source="csstree-validator"/> | ||
<error line="1" column="33" severity="error" message="Invalid value for `border` property" source="csstree-validator"/> | ||
<error line="1" column="46" severity="error" message="Unknown property `unknown`" source="csstree-validator"/> | ||
</file> | ||
<file name="parse.css"> | ||
<error line="1" column="11" severity="error" message="Colon is expected" source="csstree-validator"/> | ||
<error line="1" column="32" severity="error" message="Invalid value for `color`" source="csstree-validator"/> | ||
<error line="1" column="32" severity="error" message="Invalid value for `color` property" source="csstree-validator"/> | ||
</file> | ||
</checkstyle> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,17 @@ | ||
# match.css | ||
* Invalid value for `color` | ||
* Invalid value for `color` property | ||
syntax: <color> | ||
value: 123 | ||
--------^ | ||
* Invalid value for `border` | ||
* Invalid value for `border` property | ||
syntax: <line-width> || <line-style> || <color> | ||
value: 1px unknown red | ||
------------^ | ||
* Unknown property `unknown` | ||
|
||
# parse.css | ||
[ERROR] Colon is expected | ||
* Invalid value for `color` | ||
* Invalid value for `color` property | ||
syntax: <color> | ||
value: red green | ||
------------^ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
"match.css":1.16-1.19: error: Invalid value for `color`: `123`; allowed: <color> | ||
"match.css":1.33-1.40: error: Invalid value for `border`: `1px unknown red`; allowed: <line-width> || <line-style> || <color> | ||
"match.css":1.16-1.19: error: Invalid value for `color` property: `123`; allowed: <color> | ||
"match.css":1.33-1.40: error: Invalid value for `border` property: `1px unknown red`; allowed: <line-width> || <line-style> || <color> | ||
"match.css":1.46: error: Unknown property `unknown` | ||
"parse.css":1.11: error: Colon is expected | ||
"parse.css":1.32-1.37: error: Invalid value for `color`: `red green`; allowed: <color> | ||
"parse.css":1.32-1.37: error: Invalid value for `color` property: `red green`; allowed: <color> |
Oops, something went wrong.