Skip to content
Merged
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
68 changes: 45 additions & 23 deletions lib/harAnalyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ export class HarAnalyzer {

// Extract style="" attributes from HTML content
const elementsWithStyleAttr = dom.window.document.querySelectorAll('[style]');
elementsWithStyleAttr.forEach((element) => {
elementsWithStyleAttr.forEach((element, index) => {
const styleAttrContent = element.getAttribute('style');
const styleAttrObj = {
'url': htmlObj.url,
// Wrap the style attribute content in a dummy #id rule
'content': `#dummy-style-attribute-id { ${styleAttrContent} }`,
'content': `#dummy-style-attribute-id-${index} { ${styleAttrContent} }`,
'index': htmlObj.index
};
data['all-styles'].push(styleAttrObj);
Expand Down Expand Up @@ -132,30 +132,52 @@ export class HarAnalyzer {
return knowledgeData;
}

// Ensure the correct context for `this` using arrow function
const lintPromises = analyzedData['all-styles'].map(entry => {
return stylelint.lint({
code: entry.content,
config: this.config
}).then(result => {
// Attach warnings along with the entry.url
return result.results.flatMap(res =>
res.warnings.map(warning => ({
url: entry.url,
rule: warning.rule,
category: 'standard',
severity: warning.severity,
text: warning.text,
line: warning.line,
column: warning.column
}))
);
// Concatenate all CSS content with line offsets for accurate mapping
let concatenatedCSS = '';
const lineOffsets = [];
analyzedData['all-styles'].forEach(entry => {
const contentLines = entry.content.split('\n').length;
lineOffsets.push({
url: entry.url,
index: entry.index,
startLine: concatenatedCSS.split('\n').length,
lineCount: contentLines
});
concatenatedCSS += entry.content + '\n';
});

// Lint the concatenated CSS
const lintResult = await stylelint.lint({
code: concatenatedCSS,
config: this.config
});

// Wait for all linting promises to resolve and flatten the results
const lintResults = await Promise.all(lintPromises);
const flatResults = lintResults.flat();
// Map warnings back to their original files using line offsets
const flatResults = lintResult.results.flatMap(res =>
res.warnings.map(warning => {
const originalEntry = lineOffsets.find(offset =>
warning.line >= offset.startLine &&
warning.line < offset.startLine + offset.lineCount
);

if (!originalEntry) {
return {
...warning, // Preserve the original warning
url: 'unknown', // Add a placeholder URL
category: 'unknown'
};
}
return {
url: originalEntry.url,
rule: warning.rule,
category: 'standard',
severity: warning.severity,
text: warning.text,
line: warning.line - originalEntry.startLine + 1,
column: warning.column
};
})
);

// Convert issues to a set grouped by rule
const issuesByRule = {};
Expand Down