Skip to content

fix: eslint error and rerender issue that wouldn't use props #110

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

Merged
merged 1 commit into from
Dec 23, 2020
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports = {
],
rules: {
'max-len': ['warn', { code: 100 }],
'simple-import-sort/sort': 'error',
'simple-import-sort/imports': 'error',
'no-multiple-empty-lines': ['error', { max: 2, maxBOF: 2, maxEOF: 0 }]
},
overrides: [
Expand Down
45 changes: 26 additions & 19 deletions src/pure.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,35 @@ const render = (
target = target || container.appendChild(document.createElement('div'))

const ComponentConstructor = Component.default || Component
const isProps = !Object.keys(options).some(option => svleteComponentOptions.includes(option))

// Check if any props and Svelte options were accidentally mixed.
if (!isProps) {
const unrecognizedOptions = Object
.keys(options)
.filter(option => !svleteComponentOptions.includes(option))

if (unrecognizedOptions.length > 0) {
throw Error(`
Unknown options were found [${unrecognizedOptions}]. This might happen if you've mixed
passing in props with Svelte options into the render function. Valid Svelte options
are [${svleteComponentOptions}]. You can either change the prop names, or pass in your
props for that component via the \`props\` option.\n\n
Eg: const { /** Results **/ } = render(MyComponent, { props: { /** props here **/ } })\n\n
`)

const checkProps = (options) => {
const isProps = !Object.keys(options).some(option => svleteComponentOptions.includes(option))

// Check if any props and Svelte options were accidentally mixed.
if (!isProps) {
const unrecognizedOptions = Object
.keys(options)
.filter(option => !svleteComponentOptions.includes(option))

if (unrecognizedOptions.length > 0) {
throw Error(`
Unknown options were found [${unrecognizedOptions}]. This might happen if you've mixed
passing in props with Svelte options into the render function. Valid Svelte options
are [${svleteComponentOptions}]. You can either change the prop names, or pass in your
props for that component via the \`props\` option.\n\n
Eg: const { /** Results **/ } = render(MyComponent, { props: { /** props here **/ } })\n\n
`)
}

return options
}

return { props: options }
}

const component = new ComponentConstructor({
target,
...(isProps ? { props: options } : options)
...checkProps(options)
})

containerCache.set(container, { target, component })
Expand All @@ -53,8 +60,8 @@ const render = (

// eslint-disable-next-line no-new
const newComponent = new ComponentConstructor({
...options,
target
target,
...checkProps(options)
})

containerCache.set(container, { target, newComponent })
Expand Down