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

Resolve issue #3667 - CSS parser error on invisible character <0x200b> #4605

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/css/src/analysis/analyzer.ts
Expand Up @@ -33,8 +33,10 @@ import { CssAnalysisInput, CssAnalysisOutput } from './analysis';
export async function analyzeCSS(input: CssAnalysisInput): Promise<CssAnalysisOutput> {
const { filePath, fileContent: code, rules } = input;
const config = createStylelintConfig(rules);
const sanitizedCode = code.replace(/[\u2000-\u200F]/g, ' ');

const options = {
code,
code: sanitizedCode,
codeFilename: filePath,
config,
};
Expand Down
62 changes: 62 additions & 0 deletions packages/css/tests/analysis/analyzer.test.ts
Expand Up @@ -93,6 +93,68 @@ describe('analyzeCSS', () => {
});
});

describe('should emit correctly located issues regardless of invisible characters', () => {
const testCases: Array<
[type: 'single' | 'multiple', expectation: [line: number, column: number]]
> = [
['single', [5, 2]],
['multiple', [7, 4]],
];
ericmorand-sonarsource marked this conversation as resolved.
Show resolved Hide resolved

for (const [type, expectation] of testCases) {
const candidates: Array<number | [from: number, to: number]> = [[8192, 8207]];

for (let candidate of candidates) {
ericmorand-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
const executeTest = async (characterCode: number) => {
const hexadecimalRepresentation = characterCode.toString(16);

it(`${type} character(s) 0x${hexadecimalRepresentation}`, async () => {
ericmorand-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
const character = String.fromCharCode(characterCode);
const analysisInput: CssAnalysisInput = {
fileContent:
type === 'single'
? `body {
color: orangered;
}
${character}
${character}.foo {`
: `body {
color: orangered;
}
${character}
${character}
${character}
${character}${character}${character}.foo {`,
rules,
filePath: path.resolve('foo.css'),
};

await expect(analyzeCSS(analysisInput))
.resolves.toEqual({
issues: [
{
ruleId: 'CssSyntaxError',
line: expectation[0],
column: expectation[1],
message: 'Unclosed block (CssSyntaxError)',
},
],
})
.catch(error => {
throw error;
});
});
};

const [from, to] = typeof candidate === 'number' ? [candidate, candidate] : candidate;

for (let i = from; i <= to; i++) {
executeTest(i);
}
}
}
});

async function input(
filePath?: string,
fileContent?: string,
Expand Down