-
Notifications
You must be signed in to change notification settings - Fork 39.4k
Fix #297799: color picker in private fields #307902
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -101,7 +101,7 @@ function _findMatches(model: IDocumentColorComputerTarget | string, regex: RegEx | |
| function computeColors(model: IDocumentColorComputerTarget): IColorInformation[] { | ||
| const result: IColorInformation[] = []; | ||
| // Early validation for RGB and HSL (including CSS Level 4 syntax with / separator) | ||
| const initialValidationRegex = /\b(rgb|rgba|hsl|hsla)(\([0-9\s,.\%\/]*\))|^(#)([A-Fa-f0-9]{3})\b|^(#)([A-Fa-f0-9]{4})\b|^(#)([A-Fa-f0-9]{6})\b|^(#)([A-Fa-f0-9]{8})\b|(?<=['"\s])(#)([A-Fa-f0-9]{3})\b|(?<=['"\s])(#)([A-Fa-f0-9]{4})\b|(?<=['"\s])(#)([A-Fa-f0-9]{6})\b|(?<=['"\s])(#)([A-Fa-f0-9]{8})\b/gm; | ||
| const initialValidationRegex = /\b(rgb|rgba|hsl|hsla)(\([0-9\s,.\%\/]*\))|^(#)([A-Fa-f0-9]{3})\b|^(#)([A-Fa-f0-9]{4})\b|^(#)([A-Fa-f0-9]{6})\b|^(#)([A-Fa-f0-9]{8})\b|(?<=['"][ ]*)(#)([A-Fa-f0-9]{3})\b|(?<=['"][ ]*)(#)([A-Fa-f0-9]{4})\b|(?<=['"][ ]*)(#)([A-Fa-f0-9]{6})\b|(?<=['"][ ]*)(#)([A-Fa-f0-9]{8})\b/gm; | ||
| const initialValidationMatches = _findMatches(model, initialValidationRegex); | ||
|
Comment on lines
103
to
105
|
||
|
|
||
| // Potential colors have been found, validate the parameters | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -79,7 +79,6 @@ suite('Default Document Colors Computer', () => { | |||||||||||||||
| const testCases = [ | ||||||||||||||||
| { content: `const color = ' #ff0000';`, name: 'hex with space before' }, | ||||||||||||||||
| { content: '#ff0000', name: 'hex at start of line' }, | ||||||||||||||||
| { content: ' #ff0000', name: 'hex with whitespace before' } | ||||||||||||||||
| ]; | ||||||||||||||||
|
|
||||||||||||||||
| testCases.forEach(testCase => { | ||||||||||||||||
|
|
@@ -188,4 +187,18 @@ suite('Default Document Colors Computer', () => { | |||||||||||||||
| assert.strictEqual(colors.length, 1, `Should detect rgb/rgba color with ${testCase.name}: ${testCase.content}`); | ||||||||||||||||
| }); | ||||||||||||||||
| }); | ||||||||||||||||
|
|
||||||||||||||||
| test('JavaScript private fields with # should not be recognized as colors', () => { | ||||||||||||||||
| // Test case from issue #297799 | ||||||||||||||||
| const testCases = [ | ||||||||||||||||
| { content: 'class Foo {\n\t#facade\n}', name: 'private field #facade should not be a color' }, | ||||||||||||||||
| { content: 'class Component {\n\treadonly #facade = inject(Facade);\n}', name: 'private field #facade with injection should not be a color' }, | ||||||||||||||||
|
||||||||||||||||
| { content: 'class Component {\n\treadonly #facade = inject(Facade);\n}', name: 'private field #facade with injection should not be a color' }, | |
| { content: 'class Component {\n\treadonly #facade = inject(Facade);\n}', name: 'private field #facade with injection should not be a color' }, | |
| { content: 'class Foo {\n\t#fed\n}', name: 'private field #fed should not be a color' }, | |
| { content: 'class Foo {\n\t#fedc\n}', name: 'private field #fedc should not be a color' }, | |
| { content: 'class Foo {\n\t#fedbca0\n}', name: 'private field #fedbca0 should not be a color' }, | |
| { content: 'class Foo {\n\t#fedbcaff\n}', name: 'private field #fedbcaff should not be a color' }, | |
| { content: 'class Foo {\n#fed\n}', name: 'private field #fed without indentation should not be a color' }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This regex still matches bare hex colors at the start of a line via the
^(#)([A-Fa-f0-9]{...})alternatives. That means a private field written without indentation (e.g.class Foo {\n#facade;\n}) would still be detected as a color, so the false positive isn’t fully eliminated. Consider removing the start-of-line hex alternatives (to align with the PR description of only matching inside strings), or otherwise tightening the non-string matching so it can’t match valid private field syntax.