Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
move tests to error describe block
  • Loading branch information
RobinMalfait committed Jan 31, 2025
commit 857c7e368334d4a7b2ed079680d3e1eb25b5e80d
84 changes: 56 additions & 28 deletions packages/tailwindcss/src/css-parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,28 @@ describe.each(['Unix', 'Windows'])('Line endings: %s', (lineEndings) => {
])
})

it('should parse a custom property with an empty value', () => {
expect(parse('--foo:;')).toEqual([
{
kind: 'declaration',
property: '--foo',
value: '',
important: false,
},
])
})

it('should parse a custom property with a space value', () => {
expect(parse('--foo: ;')).toEqual([
{
kind: 'declaration',
property: '--foo',
value: '',
important: false,
},
])
})

Comment on lines +332 to +353
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added these just in case we broke something with unexpected semicolon detection

it('should parse a custom property with a block including nested "css"', () => {
expect(
parse(css`
Expand Down Expand Up @@ -1041,34 +1063,6 @@ describe.each(['Unix', 'Windows'])('Line endings: %s', (lineEndings) => {
},
])
})

// TODO: These should probably be errors in some cases?
it('discards invalid declarations', () => {
// This shouldn't parse as a custom property declaration
expect(parse(`--foo`)).toEqual([])

// This shouldn't parse as a rule followed by a declaration
expect(parse(`@plugin "foo" {};`)).toEqual([
{
kind: 'at-rule',
name: '@plugin',
params: '"foo"',
nodes: [],
},
])

// This shouldn't parse as consecutive declarations
expect(parse(`;;;`)).toEqual([])

// This shouldn't parse as a rule with a declaration inside of it
expect(parse(`.foo { bar }`)).toEqual([
{
kind: 'rule',
selector: '.foo',
nodes: [],
},
])
})
})

describe('errors', () => {
Expand Down Expand Up @@ -1125,5 +1119,39 @@ describe.each(['Unix', 'Windows'])('Line endings: %s', (lineEndings) => {
`),
).toThrowErrorMatchingInlineSnapshot(`[Error: Unterminated string: "Hello world!;"]`)
})

it('should error when incomplete custom properties are used', () => {
expect(() => parse('--foo')).toThrowErrorMatchingInlineSnapshot(
`[Error: Invalid custom property, expected a value]`,
)
})

it('should error when incomplete custom properties are used inside rules', () => {
expect(() => parse('.foo { --bar }')).toThrowErrorMatchingInlineSnapshot(
`[Error: Invalid custom property, expected a value]`,
)
})

it('should error when a declaration is incomplete', () => {
expect(() => parse('.foo { bar }')).toThrowErrorMatchingInlineSnapshot(
`[Error: Invalid declaration: \`bar\`]`,
)
})

it('should error when a semicolon exists after an at-rule with a body', () => {
expect(() => parse('@plugin "foo" {} ;')).toThrowErrorMatchingInlineSnapshot(
`[Error: Unexpected semicolon]`,
)
})

it('should error when consecutive semicolons exist', () => {
expect(() => parse(';;;')).toThrowErrorMatchingInlineSnapshot(`[Error: Unexpected: \`;;;\`]`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could also just throw the same Unexpected semicolon error, I'm indifferent on this one.

I think once we have proper span/location, we could highlight the full ;;; section.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about this but I think the error is fine as is

})

it('should error when consecutive semicolons exist after a declaration', () => {
expect(() => parse('.foo { color: red;;; }')).toThrowErrorMatchingInlineSnapshot(
`[Error: Unexpected: \`;;;\`]`,
)
})
})
})