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

Fix cannot read properties of undefined (reading 'modifier') #9656

Merged
merged 5 commits into from
Nov 3, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add test for making sure calling variantFn doesn't crash
  • Loading branch information
RobinMalfait committed Nov 3, 2022
commit 5fd1ad1b8d286dd008769c914be2f5ed2d571667
22 changes: 16 additions & 6 deletions src/lib/setupContextUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ function buildPluginApi(tailwindConfig, context, { variantList, variantMap, offs
variantFunctions = [].concat(variantFunctions).map((variantFunction) => {
if (typeof variantFunction !== 'string') {
// Safelist public API functions
return (api) => {
return (api = {}) => {
let { args, modifySelectors, container, separator, wrap, format } = api
let result = variantFunction(
Object.assign(
Expand Down Expand Up @@ -581,11 +581,19 @@ function buildPluginApi(tailwindConfig, context, { variantList, variantMap, offs

api.addVariant(
isSpecial ? `${variant}${key}` : `${variant}-${key}`,
({ args, container }) =>
variantFn(
({ args, container }) => {
// For backwards compatibility reasons for older versions of the vscode intellisense
// plugin and the JetBrains plugin.
if (!args) {
return null
}

return variantFn(
value,
modifiersEnabled ? { modifier: (args ?? {}).modifier, container } : { container }
),
modifiersEnabled ? { modifier: args.modifier, container } : { container }
)
},

{
...options,
value,
Expand All @@ -601,10 +609,12 @@ function buildPluginApi(tailwindConfig, context, { variantList, variantMap, offs
api.addVariant(
variant,
({ args, container }) => {
// For backwards compatibility reasons for older versions of the vscode intellisense
// plugin and the JetBrains plugin.
if (!args) {
return null
}

if (args.value === sharedState.NONE && !hasDefault) {
Copy link

@theskollie theskollie Oct 28, 2022

Choose a reason for hiding this comment

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

if (!args ||  args.value === sharedState.NONE && !hasDefault) {
return null;
}

return null
}
Expand Down
66 changes: 66 additions & 0 deletions tests/match-variants.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import resolveConfig from '../src/public/resolve-config'
import { createContext } from '../src/lib/setupContextUtils'

import { run, html, css } from './util/run'

test('partial arbitrary variants', () => {
Expand Down Expand Up @@ -788,3 +791,66 @@ it('should be possible to use `undefined` as a DEFAULT value', () => {
`)
})
})

it('should be possible to use `undefined` as a DEFAULT value', () => {
let config = {
content: [
{
raw: html`
<div>
<div class="foo:underline"></div>
</div>
`,
},
],
corePlugins: { preflight: false },
plugins: [
({ matchVariant }) => {
matchVariant('foo', (value) => `.foo${value === undefined ? '-good' : '-bad'} &`, {
values: { DEFAULT: undefined },
})
},
],
}

let input = css`
@tailwind utilities;
`

return run(input, config).then((result) => {
expect(result.css).toMatchFormattedCss(css`
.foo-good .foo\:underline {
text-decoration-line: underline;
}
`)
})
})

it('should not break things', () => {
let config = {
plugins: [
({ matchVariant }) => {
matchVariant(
'foo',
(_value, { modifier: _modifier }) => {
expect(true).toBe(true) // Should not be called
return `.foo &`
},
{
modifiers: {
bar: 'bar',
},
}
)
},
],
}

let context = createContext(resolveConfig(config))
let [[, fn]] = context.variantMap.get('foo')
expect(fn()).toBe(null)
expect(fn({})).toBe(null)
expect(fn({ format() {} })).toBe(null)

expect.assertions(3)
})