mirror of
https://github.com/hanzoai/postcss.git
synced 2026-09-24 17:01:56 +00:00
305 lines
22 KiB
HTML
305 lines
22 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<title>css-syntax-error.es6 - Documentation</title>
|
|
|
|
<script src="scripts/prettify/prettify.js"></script>
|
|
<script src="scripts/prettify/lang-css.js"></script>
|
|
<!--[if lt IE 9]>
|
|
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
|
|
<![endif]-->
|
|
<link type="text/css" rel="stylesheet" href="styles/prettify.css">
|
|
<link type="text/css" rel="stylesheet" href="styles/jsdoc.css">
|
|
</head>
|
|
<body>
|
|
|
|
<input type="checkbox" id="nav-trigger" class="nav-trigger" />
|
|
<label for="nav-trigger" class="navicon-button x">
|
|
<div class="navicon"></div>
|
|
</label>
|
|
|
|
<label for="nav-trigger" class="overlay"></label>
|
|
|
|
<nav>
|
|
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="AtRule.html">AtRule</a><ul class='methods'><li data-type='method'><a href="AtRule.html#append">append</a></li><li data-type='method'><a href="AtRule.html#each">each</a></li><li data-type='method'><a href="AtRule.html#every">every</a></li><li data-type='method'><a href="AtRule.html#index">index</a></li><li data-type='method'><a href="AtRule.html#insertAfter">insertAfter</a></li><li data-type='method'><a href="AtRule.html#insertBefore">insertBefore</a></li><li data-type='method'><a href="AtRule.html#prepend">prepend</a></li><li data-type='method'><a href="AtRule.html#removeAll">removeAll</a></li><li data-type='method'><a href="AtRule.html#removeChild">removeChild</a></li><li data-type='method'><a href="AtRule.html#replaceValues">replaceValues</a></li><li data-type='method'><a href="AtRule.html#some">some</a></li><li data-type='method'><a href="AtRule.html#walk">walk</a></li><li data-type='method'><a href="AtRule.html#walkAtRules">walkAtRules</a></li><li data-type='method'><a href="AtRule.html#walkComments">walkComments</a></li><li data-type='method'><a href="AtRule.html#walkDecls">walkDecls</a></li><li data-type='method'><a href="AtRule.html#walkRules">walkRules</a></li></ul></li><li><a href="Comment.html">Comment</a><ul class='methods'><li data-type='method'><a href="Comment.html#after">after</a></li><li data-type='method'><a href="Comment.html#before">before</a></li><li data-type='method'><a href="Comment.html#cleanRaws">cleanRaws</a></li><li data-type='method'><a href="Comment.html#clone">clone</a></li><li data-type='method'><a href="Comment.html#cloneAfter">cloneAfter</a></li><li data-type='method'><a href="Comment.html#cloneBefore">cloneBefore</a></li><li data-type='method'><a href="Comment.html#error">error</a></li><li data-type='method'><a href="Comment.html#next">next</a></li><li data-type='method'><a href="Comment.html#prev">prev</a></li><li data-type='method'><a href="Comment.html#raw">raw</a></li><li data-type='method'><a href="Comment.html#remove">remove</a></li><li data-type='method'><a href="Comment.html#replaceWith">replaceWith</a></li><li data-type='method'><a href="Comment.html#root">root</a></li><li data-type='method'><a href="Comment.html#toString">toString</a></li><li data-type='method'><a href="Comment.html#warn">warn</a></li></ul></li><li><a href="Container.html">Container</a><ul class='methods'><li data-type='method'><a href="Container.html#after">after</a></li><li data-type='method'><a href="Container.html#append">append</a></li><li data-type='method'><a href="Container.html#before">before</a></li><li data-type='method'><a href="Container.html#cleanRaws">cleanRaws</a></li><li data-type='method'><a href="Container.html#clone">clone</a></li><li data-type='method'><a href="Container.html#cloneAfter">cloneAfter</a></li><li data-type='method'><a href="Container.html#cloneBefore">cloneBefore</a></li><li data-type='method'><a href="Container.html#each">each</a></li><li data-type='method'><a href="Container.html#error">error</a></li><li data-type='method'><a href="Container.html#every">every</a></li><li data-type='method'><a href="Container.html#index">index</a></li><li data-type='method'><a href="Container.html#insertAfter">insertAfter</a></li><li data-type='method'><a href="Container.html#insertBefore">insertBefore</a></li><li data-type='method'><a href="Container.html#next">next</a></li><li data-type='method'><a href="Container.html#prepend">prepend</a></li><li data-type='method'><a href="Container.html#prev">prev</a></li><li data-type='method'><a href="Container.html#raw">raw</a></li><li data-type='method'><a href="Container.html#remove">remove</a></li><li data-type='method'><a href="Container.html#removeAll">removeAll</a></li><li data-type='method'><a href="Container.html#removeChild">removeChild</a></li><li data-type='method'><a href="Container.html#replaceValues">replaceValues</a></li><li data-type='method'><a href="Container.html#replaceWith">replaceWith</a></li><li data-type='method'><a href="Container.html#root">root</a></li><li data-type='method'><a href="Container.html#some">some</a></li><li data-type='method'><a href="Container.html#toString">toString</a></li><li data-type='method'><a href="Container.html#walk">walk</a></li><li data-type='method'><a href="Container.html#walkAtRules">walkAtRules</a></li><li data-type='method'><a href="Container.html#walkComments">walkComments</a></li><li data-type='method'><a href="Container.html#walkDecls">walkDecls</a></li><li data-type='method'><a href="Container.html#walkRules">walkRules</a></li><li data-type='method'><a href="Container.html#warn">warn</a></li></ul></li><li><a href="CssSyntaxError.html">CssSyntaxError</a><ul class='methods'><li data-type='method'><a href="CssSyntaxError.html#showSourceCode">showSourceCode</a></li><li data-type='method'><a href="CssSyntaxError.html#toString">toString</a></li></ul></li><li><a href="Declaration.html">Declaration</a><ul class='methods'><li data-type='method'><a href="Declaration.html#after">after</a></li><li data-type='method'><a href="Declaration.html#before">before</a></li><li data-type='method'><a href="Declaration.html#cleanRaws">cleanRaws</a></li><li data-type='method'><a href="Declaration.html#clone">clone</a></li><li data-type='method'><a href="Declaration.html#cloneAfter">cloneAfter</a></li><li data-type='method'><a href="Declaration.html#cloneBefore">cloneBefore</a></li><li data-type='method'><a href="Declaration.html#error">error</a></li><li data-type='method'><a href="Declaration.html#next">next</a></li><li data-type='method'><a href="Declaration.html#prev">prev</a></li><li data-type='method'><a href="Declaration.html#raw">raw</a></li><li data-type='method'><a href="Declaration.html#remove">remove</a></li><li data-type='method'><a href="Declaration.html#replaceWith">replaceWith</a></li><li data-type='method'><a href="Declaration.html#root">root</a></li><li data-type='method'><a href="Declaration.html#toString">toString</a></li><li data-type='method'><a href="Declaration.html#warn">warn</a></li></ul></li><li><a href="Input.html">Input</a><ul class='methods'><li data-type='method'><a href="Input.html#origin">origin</a></li></ul></li><li><a href="LazyResult.html">LazyResult</a><ul class='methods'><li data-type='method'><a href="LazyResult.html#catch">catch</a></li><li data-type='method'><a href="LazyResult.html#finally">finally</a></li><li data-type='method'><a href="LazyResult.html#then">then</a></li><li data-type='method'><a href="LazyResult.html#toString">toString</a></li><li data-type='method'><a href="LazyResult.html#warnings">warnings</a></li></ul></li><li><a href="Node.html">Node</a><ul class='methods'><li data-type='method'><a href="Node.html#after">after</a></li><li data-type='method'><a href="Node.html#before">before</a></li><li data-type='method'><a href="Node.html#cleanRaws">cleanRaws</a></li><li data-type='method'><a href="Node.html#clone">clone</a></li><li data-type='method'><a href="Node.html#cloneAfter">cloneAfter</a></li><li data-type='method'><a href="Node.html#cloneBefore">cloneBefore</a></li><li data-type='method'><a href="Node.html#error">error</a></li><li data-type='method'><a href="Node.html#next">next</a></li><li data-type='method'><a href="Node.html#prev">prev</a></li><li data-type='method'><a href="Node.html#raw">raw</a></li><li data-type='method'><a href="Node.html#remove">remove</a></li><li data-type='method'><a href="Node.html#replaceWith">replaceWith</a></li><li data-type='method'><a href="Node.html#root">root</a></li><li data-type='method'><a href="Node.html#toString">toString</a></li><li data-type='method'><a href="Node.html#warn">warn</a></li></ul></li><li><a href="PreviousMap.html">PreviousMap</a><ul class='methods'><li data-type='method'><a href="PreviousMap.html#consumer">consumer</a></li><li data-type='method'><a href="PreviousMap.html#withContent">withContent</a></li></ul></li><li><a href="Processor.html">Processor</a><ul class='methods'><li data-type='method'><a href="Processor.html#process">process</a></li><li data-type='method'><a href="Processor.html#use">use</a></li></ul></li><li><a href="Result.html">Result</a><ul class='methods'><li data-type='method'><a href="Result.html#toString">toString</a></li><li data-type='method'><a href="Result.html#warn">warn</a></li><li data-type='method'><a href="Result.html#warnings">warnings</a></li></ul></li><li><a href="Root.html">Root</a><ul class='methods'><li data-type='method'><a href="Root.html#after">after</a></li><li data-type='method'><a href="Root.html#append">append</a></li><li data-type='method'><a href="Root.html#before">before</a></li><li data-type='method'><a href="Root.html#cleanRaws">cleanRaws</a></li><li data-type='method'><a href="Root.html#clone">clone</a></li><li data-type='method'><a href="Root.html#cloneAfter">cloneAfter</a></li><li data-type='method'><a href="Root.html#cloneBefore">cloneBefore</a></li><li data-type='method'><a href="Root.html#each">each</a></li><li data-type='method'><a href="Root.html#error">error</a></li><li data-type='method'><a href="Root.html#every">every</a></li><li data-type='method'><a href="Root.html#index">index</a></li><li data-type='method'><a href="Root.html#insertAfter">insertAfter</a></li><li data-type='method'><a href="Root.html#insertBefore">insertBefore</a></li><li data-type='method'><a href="Root.html#next">next</a></li><li data-type='method'><a href="Root.html#prepend">prepend</a></li><li data-type='method'><a href="Root.html#prev">prev</a></li><li data-type='method'><a href="Root.html#raw">raw</a></li><li data-type='method'><a href="Root.html#remove">remove</a></li><li data-type='method'><a href="Root.html#removeAll">removeAll</a></li><li data-type='method'><a href="Root.html#removeChild">removeChild</a></li><li data-type='method'><a href="Root.html#replaceValues">replaceValues</a></li><li data-type='method'><a href="Root.html#replaceWith">replaceWith</a></li><li data-type='method'><a href="Root.html#root">root</a></li><li data-type='method'><a href="Root.html#some">some</a></li><li data-type='method'><a href="Root.html#toResult">toResult</a></li><li data-type='method'><a href="Root.html#toString">toString</a></li><li data-type='method'><a href="Root.html#walk">walk</a></li><li data-type='method'><a href="Root.html#walkAtRules">walkAtRules</a></li><li data-type='method'><a href="Root.html#walkComments">walkComments</a></li><li data-type='method'><a href="Root.html#walkDecls">walkDecls</a></li><li data-type='method'><a href="Root.html#walkRules">walkRules</a></li><li data-type='method'><a href="Root.html#warn">warn</a></li></ul></li><li><a href="Rule.html">Rule</a><ul class='methods'><li data-type='method'><a href="Rule.html#after">after</a></li><li data-type='method'><a href="Rule.html#append">append</a></li><li data-type='method'><a href="Rule.html#before">before</a></li><li data-type='method'><a href="Rule.html#cleanRaws">cleanRaws</a></li><li data-type='method'><a href="Rule.html#clone">clone</a></li><li data-type='method'><a href="Rule.html#cloneAfter">cloneAfter</a></li><li data-type='method'><a href="Rule.html#cloneBefore">cloneBefore</a></li><li data-type='method'><a href="Rule.html#each">each</a></li><li data-type='method'><a href="Rule.html#error">error</a></li><li data-type='method'><a href="Rule.html#every">every</a></li><li data-type='method'><a href="Rule.html#index">index</a></li><li data-type='method'><a href="Rule.html#insertAfter">insertAfter</a></li><li data-type='method'><a href="Rule.html#insertBefore">insertBefore</a></li><li data-type='method'><a href="Rule.html#next">next</a></li><li data-type='method'><a href="Rule.html#prepend">prepend</a></li><li data-type='method'><a href="Rule.html#prev">prev</a></li><li data-type='method'><a href="Rule.html#raw">raw</a></li><li data-type='method'><a href="Rule.html#remove">remove</a></li><li data-type='method'><a href="Rule.html#removeAll">removeAll</a></li><li data-type='method'><a href="Rule.html#removeChild">removeChild</a></li><li data-type='method'><a href="Rule.html#replaceValues">replaceValues</a></li><li data-type='method'><a href="Rule.html#replaceWith">replaceWith</a></li><li data-type='method'><a href="Rule.html#root">root</a></li><li data-type='method'><a href="Rule.html#some">some</a></li><li data-type='method'><a href="Rule.html#toString">toString</a></li><li data-type='method'><a href="Rule.html#walk">walk</a></li><li data-type='method'><a href="Rule.html#walkAtRules">walkAtRules</a></li><li data-type='method'><a href="Rule.html#walkComments">walkComments</a></li><li data-type='method'><a href="Rule.html#walkDecls">walkDecls</a></li><li data-type='method'><a href="Rule.html#walkRules">walkRules</a></li><li data-type='method'><a href="Rule.html#warn">warn</a></li></ul></li><li><a href="Warning.html">Warning</a><ul class='methods'><li data-type='method'><a href="Warning.html#toString">toString</a></li></ul></li></ul><h3>Namespaces</h3><ul><li><a href="list.html">list</a><ul class='methods'><li data-type='method'><a href="list.html#.comma">comma</a></li><li data-type='method'><a href="list.html#.space">space</a></li></ul></li><li><a href="postcss.html">postcss</a><ul class='methods'><li data-type='method'><a href="postcss.html#.atRule">atRule</a></li><li data-type='method'><a href="postcss.html#.comment">comment</a></li><li data-type='method'><a href="postcss.html#.decl">decl</a></li><li data-type='method'><a href="postcss.html#.parse">parse</a></li><li data-type='method'><a href="postcss.html#.plugin">plugin</a></li><li data-type='method'><a href="postcss.html#.root">root</a></li><li data-type='method'><a href="postcss.html#.rule">rule</a></li><li data-type='method'><a href="postcss.html#.stringify">stringify</a></li></ul></li><li><a href="vendor.html">vendor</a><ul class='methods'><li data-type='method'><a href="vendor.html#.prefix">prefix</a></li><li data-type='method'><a href="vendor.html#.unprefixed">unprefixed</a></li></ul></li></ul><h3><a href="global.html">Global</a></h3>
|
|
</nav>
|
|
|
|
<div id="main">
|
|
|
|
<h1 class="page-title">css-syntax-error.es6</h1>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<section>
|
|
<article>
|
|
<pre class="prettyprint source linenums"><code>import supportsColor from 'supports-color'
|
|
import chalk from 'chalk'
|
|
|
|
import terminalHighlight from './terminal-highlight'
|
|
|
|
/**
|
|
* The CSS parser throws this error for broken CSS.
|
|
*
|
|
* Custom parsers can throw this error for broken custom syntax using
|
|
* the {@link Node#error} method.
|
|
*
|
|
* PostCSS will use the input source map to detect the original error location.
|
|
* If you wrote a Sass file, compiled it to CSS and then parsed it with PostCSS,
|
|
* PostCSS will show the original position in the Sass file.
|
|
*
|
|
* If you need the position in the PostCSS input
|
|
* (e.g., to debug the previous compiler), use `error.input.file`.
|
|
*
|
|
* @example
|
|
* // Catching and checking syntax error
|
|
* try {
|
|
* postcss.parse('a{')
|
|
* } catch (error) {
|
|
* if (error.name === 'CssSyntaxError') {
|
|
* error //=> CssSyntaxError
|
|
* }
|
|
* }
|
|
*
|
|
* @example
|
|
* // Raising error from plugin
|
|
* throw node.error('Unknown variable', { plugin: 'postcss-vars' })
|
|
*/
|
|
class CssSyntaxError {
|
|
/**
|
|
* @param {string} message Error message.
|
|
* @param {number} [line] Source line of the error.
|
|
* @param {number} [column] Source column of the error.
|
|
* @param {string} [source] Source code of the broken file.
|
|
* @param {string} [file] Absolute path to the broken file.
|
|
* @param {string} [plugin] PostCSS plugin name, if error came from plugin.
|
|
*/
|
|
constructor (message, line, column, source, file, plugin) {
|
|
/**
|
|
* Always equal to `'CssSyntaxError'`. You should always check error type
|
|
* by `error.name === 'CssSyntaxError'`
|
|
* instead of `error instanceof CssSyntaxError`,
|
|
* because npm could have several PostCSS versions.
|
|
*
|
|
* @type {string}
|
|
*
|
|
* @example
|
|
* if (error.name === 'CssSyntaxError') {
|
|
* error //=> CssSyntaxError
|
|
* }
|
|
*/
|
|
this.name = 'CssSyntaxError'
|
|
/**
|
|
* Error message.
|
|
*
|
|
* @type {string}
|
|
*
|
|
* @example
|
|
* error.message //=> 'Unclosed block'
|
|
*/
|
|
this.reason = message
|
|
|
|
if (file) {
|
|
/**
|
|
* Absolute path to the broken file.
|
|
*
|
|
* @type {string}
|
|
*
|
|
* @example
|
|
* error.file //=> 'a.sass'
|
|
* error.input.file //=> 'a.css'
|
|
*/
|
|
this.file = file
|
|
}
|
|
if (source) {
|
|
/**
|
|
* Source code of the broken file.
|
|
*
|
|
* @type {string}
|
|
*
|
|
* @example
|
|
* error.source //=> 'a { b {} }'
|
|
* error.input.column //=> 'a b { }'
|
|
*/
|
|
this.source = source
|
|
}
|
|
if (plugin) {
|
|
/**
|
|
* Plugin name, if error came from plugin.
|
|
*
|
|
* @type {string}
|
|
*
|
|
* @example
|
|
* error.plugin //=> 'postcss-vars'
|
|
*/
|
|
this.plugin = plugin
|
|
}
|
|
if (typeof line !== 'undefined' && typeof column !== 'undefined') {
|
|
/**
|
|
* Source line of the error.
|
|
*
|
|
* @type {number}
|
|
*
|
|
* @example
|
|
* error.line //=> 2
|
|
* error.input.line //=> 4
|
|
*/
|
|
this.line = line
|
|
/**
|
|
* Source column of the error.
|
|
*
|
|
* @type {number}
|
|
*
|
|
* @example
|
|
* error.column //=> 1
|
|
* error.input.column //=> 4
|
|
*/
|
|
this.column = column
|
|
}
|
|
|
|
this.setMessage()
|
|
|
|
if (Error.captureStackTrace) {
|
|
Error.captureStackTrace(this, CssSyntaxError)
|
|
}
|
|
}
|
|
|
|
setMessage () {
|
|
/**
|
|
* Full error text in the GNU error format
|
|
* with plugin, file, line and column.
|
|
*
|
|
* @type {string}
|
|
*
|
|
* @example
|
|
* error.message //=> 'a.css:1:1: Unclosed block'
|
|
*/
|
|
this.message = this.plugin ? this.plugin + ': ' : ''
|
|
this.message += this.file ? this.file : '<css input>'
|
|
if (typeof this.line !== 'undefined') {
|
|
this.message += ':' + this.line + ':' + this.column
|
|
}
|
|
this.message += ': ' + this.reason
|
|
}
|
|
|
|
/**
|
|
* Returns a few lines of CSS source that caused the error.
|
|
*
|
|
* If the CSS has an input source map without `sourceContent`,
|
|
* this method will return an empty string.
|
|
*
|
|
* @param {boolean} [color] Whether arrow will be colored red by terminal
|
|
* color codes. By default, PostCSS will detect
|
|
* color support by `process.stdout.isTTY`
|
|
* and `process.env.NODE_DISABLE_COLORS`.
|
|
*
|
|
* @example
|
|
* error.showSourceCode() //=> " 4 | }
|
|
* // 5 | a {
|
|
* // > 6 | bad
|
|
* // | ^
|
|
* // 7 | }
|
|
* // 8 | b {"
|
|
*
|
|
* @return {string} Few lines of CSS source that caused the error.
|
|
*/
|
|
showSourceCode (color) {
|
|
if (!this.source) return ''
|
|
|
|
let css = this.source
|
|
if (terminalHighlight) {
|
|
if (typeof color === 'undefined') color = supportsColor.stdout
|
|
if (color) css = terminalHighlight(css)
|
|
}
|
|
|
|
const lines = css.split(/\r?\n/)
|
|
const start = Math.max(this.line - 3, 0)
|
|
const end = Math.min(this.line + 2, lines.length)
|
|
|
|
const maxWidth = String(end).length
|
|
|
|
function mark (text) {
|
|
if (color && chalk.red) {
|
|
return chalk.red.bold(text)
|
|
} else {
|
|
return text
|
|
}
|
|
}
|
|
function aside (text) {
|
|
if (color && chalk.gray) {
|
|
return chalk.gray(text)
|
|
} else {
|
|
return text
|
|
}
|
|
}
|
|
|
|
return lines.slice(start, end).map((line, index) => {
|
|
const number = start + 1 + index
|
|
const gutter = ' ' + (' ' + number).slice(-maxWidth) + ' | '
|
|
if (number === this.line) {
|
|
const spacing = aside(gutter.replace(/\d/g, ' ')) +
|
|
line.slice(0, this.column - 1).replace(/[^\t]/g, ' ')
|
|
return mark('>') + aside(gutter) + line + '\n ' + spacing + mark('^')
|
|
} else {
|
|
return ' ' + aside(gutter) + line
|
|
}
|
|
}).join('\n')
|
|
}
|
|
|
|
/**
|
|
* Returns error position, message and source code of the broken part.
|
|
*
|
|
* @example
|
|
* error.toString() //=> "CssSyntaxError: app.css:1:1: Unclosed block
|
|
* // > 1 | a {
|
|
* // | ^"
|
|
*
|
|
* @return {string} Error position, message and source code.
|
|
*/
|
|
toString () {
|
|
let code = this.showSourceCode()
|
|
if (code) {
|
|
code = '\n\n' + code + '\n'
|
|
}
|
|
return this.name + ': ' + this.message + code
|
|
}
|
|
|
|
/**
|
|
* @memberof CssSyntaxError#
|
|
* @member {Input} input Input object with PostCSS internal information
|
|
* about input file. If input has source map
|
|
* from previous tool, PostCSS will use origin
|
|
* (for example, Sass) source. You can use this
|
|
* object to get PostCSS input source.
|
|
*
|
|
* @example
|
|
* error.input.file //=> 'a.css'
|
|
* error.file //=> 'a.sass'
|
|
*/
|
|
}
|
|
|
|
export default CssSyntaxError
|
|
</code></pre>
|
|
</article>
|
|
</section>
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
<br class="clear">
|
|
|
|
<footer>
|
|
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
|
|
</footer>
|
|
|
|
<script>prettyPrint();</script>
|
|
<script src="scripts/linenumber.js"></script>
|
|
</body>
|
|
</html>
|