stylelint-prettier
Advanced tools
Comparing version 1.0.5 to 1.0.6
# Changelog | ||
## 1.0.6 (2019-01-05) | ||
- Report unparsable code as linting issues instead of crashing (#14) | ||
## 1.0.5 (2018-11-16) | ||
@@ -4,0 +8,0 @@ |
{ | ||
"name": "stylelint-prettier", | ||
"version": "1.0.5", | ||
"version": "1.0.6", | ||
"description": "Runs prettier as an stylelint rule", | ||
@@ -44,3 +44,3 @@ "keywords": [ | ||
"prettier": "^1.13.7", | ||
"stylelint": "^9.2.1", | ||
"stylelint": "^9.5.0", | ||
"stylelint-config-prettier": "^4.0.0" | ||
@@ -47,0 +47,0 @@ }, |
@@ -77,4 +77,33 @@ const stylelint = require('stylelint'); | ||
); | ||
const prettierSource = prettier.format(source, prettierOptions); | ||
try { | ||
prettierSource = prettier.format(source, prettierOptions); | ||
} catch (err) { | ||
if (!(err instanceof SyntaxError)) { | ||
throw err; | ||
} | ||
let message = 'Parsing error: ' + err.message; | ||
// Prettier's message contains a codeframe style preview of the | ||
// invalid code and the line/column at which the error occured. | ||
// ESLint shows those pieces of information elsewhere already so | ||
// remove them from the message | ||
if (err.codeFrame) { | ||
message = message.replace(`\n${err.codeFrame}`, ''); | ||
} | ||
if (err.loc) { | ||
message = message.replace(/ \(\d+:\d+\)$/, ''); | ||
} | ||
stylelint.utils.report({ | ||
ruleName, | ||
result, | ||
message, | ||
node: root, | ||
index: getIndexFromLoc(source, err.loc.start), | ||
}); | ||
return; | ||
} | ||
// Everything is the same. Nothing to do here; | ||
@@ -178,3 +207,22 @@ if (source === prettierSource) { | ||
function getIndexFromLoc(source, {line, column}) { | ||
function nthIndex(str, searchValue, n) { | ||
let i = -1; | ||
while (n-- && i++ < str.length) { | ||
i = str.indexOf(searchValue, i); | ||
if (i < 0) { | ||
break; | ||
} | ||
} | ||
return i; | ||
} | ||
if (line === 1) { | ||
return column - 1; | ||
} | ||
return nthIndex(source, '\n', line - 1) + column; | ||
} | ||
module.exports.ruleName = ruleName; | ||
module.exports.messages = messages; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14725
199