Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add end position to warnings and errors #1250

Merged
merged 5 commits into from
Mar 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/css/Selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export default class Selector {
while (i-- > 1) {
const selector = block.selectors[i];
if (selector.type === 'PseudoClassSelector' && selector.name === 'global') {
validator.error(`:global(...) must be the first element in a compound selector`, selector.start);
validator.error(`:global(...) must be the first element in a compound selector`, selector);
}
}
});
Expand All @@ -120,7 +120,7 @@ export default class Selector {

for (let i = start; i < end; i += 1) {
if (this.blocks[i].global) {
validator.error(`:global(...) can be at the start or end of a selector sequence, but not in the middle`, this.blocks[i].selectors[0].start);
validator.error(`:global(...) can be at the start or end of a selector sequence, but not in the middle`, this.blocks[i].selectors[0]);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,4 @@ export function create(source: string, _options: CompileOptions = {}) {
}
}

export { parse, validate, version as VERSION };
export { parse, validate, Stylesheet, version as VERSION };
1 change: 1 addition & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface Parsed {

export interface Warning {
loc?: { line: number; column: number; pos?: number };
end?: { line: number; column: number; };
pos?: number;
message: string;
filename?: string;
Expand Down
16 changes: 10 additions & 6 deletions src/utils/CompileError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,28 @@ import getCodeFrame from '../utils/getCodeFrame';
export default class CompileError extends Error {
frame: string;
loc: { line: number; column: number };
end: { line: number; column: number };
pos: number;
filename: string;

constructor(
message: string,
template: string,
index: number,
filename: string
startPos: number,
filename: string,
endPos: number = startPos
) {
super(message);

const { line, column } = locate(template, index);
const start = locate(template, startPos);
const end = locate(template, endPos);

this.loc = { line: line + 1, column };
this.pos = index;
this.loc = { line: start.line + 1, column: start.column };
this.end = { line: end.line + 1, column: end.column };
this.pos = startPos;
this.filename = filename;

this.frame = getCodeFrame(template, line, column);
this.frame = getCodeFrame(template, start.line, start.column);
}

public toString = () => {
Expand Down
32 changes: 16 additions & 16 deletions src/validate/html/a11y.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default function a11y(
if (name.startsWith('aria-')) {
if (invisibleElements.has(node.name)) {
// aria-unsupported-elements
validator.warn(`A11y: <${node.name}> should not have aria-* attributes`, attribute.start);
validator.warn(`A11y: <${node.name}> should not have aria-* attributes`, attribute);
}

const type = name.slice(5);
Expand All @@ -42,15 +42,15 @@ export default function a11y(
let message = `A11y: Unknown aria attribute 'aria-${type}'`;
if (match) message += ` (did you mean '${match}'?)`;

validator.warn(message, attribute.start);
validator.warn(message, attribute);
}
}

// aria-role
if (name === 'role') {
if (invisibleElements.has(node.name)) {
// aria-unsupported-elements
validator.warn(`A11y: <${node.name}> should not have role attribute`, attribute.start);
validator.warn(`A11y: <${node.name}> should not have role attribute`, attribute);
}

const value = getStaticAttributeValue(node, 'role');
Expand All @@ -59,30 +59,30 @@ export default function a11y(
let message = `A11y: Unknown role '${value}'`;
if (match) message += ` (did you mean '${match}'?)`;

validator.warn(message, attribute.start);
validator.warn(message, attribute);
}
}

// no-access-key
if (name === 'accesskey') {
validator.warn(`A11y: Avoid using accesskey`, attribute.start);
validator.warn(`A11y: Avoid using accesskey`, attribute);
}

// no-autofocus
if (name === 'autofocus') {
validator.warn(`A11y: Avoid using autofocus`, attribute.start);
validator.warn(`A11y: Avoid using autofocus`, attribute);
}

// scope
if (name === 'scope' && node.name !== 'th') {
validator.warn(`A11y: The scope attribute should only be used with <th> elements`, attribute.start);
validator.warn(`A11y: The scope attribute should only be used with <th> elements`, attribute);
}

// tabindex-no-positive
if (name === 'tabindex') {
const value = getStaticAttributeValue(node, 'tabindex');
if (!isNaN(value) && +value > 0) {
validator.warn(`A11y: avoid tabindex values above zero`, attribute.start);
validator.warn(`A11y: avoid tabindex values above zero`, attribute);
}
}

Expand All @@ -96,21 +96,21 @@ export default function a11y(
attributes.slice(0, -1).join(', ') + ` or ${attributes[attributes.length - 1]}` :
attributes[0];

validator.warn(`A11y: <${name}> element should have ${article} ${sequence} attribute`, node.start);
validator.warn(`A11y: <${name}> element should have ${article} ${sequence} attribute`, node);
}
}

function shouldHaveContent() {
if (node.children.length === 0) {
validator.warn(`A11y: <${node.name}> element should have child content`, node.start);
validator.warn(`A11y: <${node.name}> element should have child content`, node);
}
}

function shouldHaveValidHref (attribute) {
const href = attributeMap.get(attribute);
const value = getStaticAttributeValue(node, attribute);
if (value === '' || value === '#') {
validator.warn(`A11y: '${value}' is not a valid ${attribute} attribute`, href.start);
validator.warn(`A11y: '${value}' is not a valid ${attribute} attribute`, href);
}
}

Expand All @@ -122,7 +122,7 @@ export default function a11y(
// anchor-in-svg-is-valid
shouldHaveValidHref('xlink:href')
} else {
validator.warn(`A11y: <a> element should have an href attribute`, node.start);
validator.warn(`A11y: <a> element should have an href attribute`, node);
}

// anchor-has-content
Expand All @@ -141,7 +141,7 @@ export default function a11y(
shouldHaveContent();

if (attributeMap.has('aria-hidden')) {
validator.warn(`A11y: <${node.name}> element should not be hidden`, attributeMap.get('aria-hidden').start);
validator.warn(`A11y: <${node.name}> element should not be hidden`, attributeMap.get('aria-hidden'));
}
}

Expand All @@ -157,14 +157,14 @@ export default function a11y(

// no-distracting-elements
if (node.name === 'marquee' || node.name === 'blink') {
validator.warn(`A11y: Avoid <${node.name}> elements`, node.start);
validator.warn(`A11y: Avoid <${node.name}> elements`, node);
}

if (node.name === 'figcaption') {
const parent = elementStack[elementStack.length - 1];
if (parent) {
if (parent.name !== 'figure') {
validator.warn(`A11y: <figcaption> must be an immediate child of <figure>`, node.start);
validator.warn(`A11y: <figcaption> must be an immediate child of <figure>`, node);
} else {
const children = parent.children.filter(node => {
if (node.type === 'Comment') return false;
Expand All @@ -175,7 +175,7 @@ export default function a11y(
const index = children.indexOf(node);

if (index !== 0 && index !== children.length - 1) {
validator.warn(`A11y: <figcaption> must be first or last child of <figure>`, node.start);
validator.warn(`A11y: <figcaption> must be first or last child of <figure>`, node);
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/validate/html/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export default function validateHtml(validator: Validator, html: Node) {

else if (node.type === 'EachBlock') {
if (validator.helpers.has(node.context)) {
let c = node.expression.end;
let c: number = node.expression.end;

// find start of context
while (/\s/.test(validator.source[c])) c += 1;
Expand All @@ -61,13 +61,13 @@ export default function validateHtml(validator: Validator, html: Node) {

validator.warn(
`Context clashes with a helper. Rename one or the other to eliminate any ambiguity`,
c
{ start: c, end: c + node.context.length }
);
}
}

if (validator.options.dev && isEmptyBlock(node)) {
validator.warn('Empty block', node.start);
validator.warn('Empty block', node);
}

if (node.children) {
Expand Down Expand Up @@ -103,7 +103,7 @@ export default function validateHtml(validator: Validator, html: Node) {
let message = `'refs.${ref}' does not exist`;
if (match) message += ` (did you mean 'refs.${match}'?)`;

validator.error(message, callee.start);
validator.error(message, callee);
}
});
}
Loading