Skip to content

Commit

Permalink
suppresswarnings now covers user props
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobrosenberg committed Oct 25, 2019
1 parent f516128 commit 887be93
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 28 deletions.
17 changes: 5 additions & 12 deletions src/Route.svelte
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<script>
import { demandObject, suppressWarnings } from "./scripts.js";
import { options } from "generatedRoutes.js";
export let url, route, routes;
export let components = [];
export let url;
export let route;
export let routes;
export let rootScope = {};
export let layoutScope = {};
Expand All @@ -11,17 +11,10 @@
$: component = components.shift();
$: components = components.slice(0); //clone or components starts disappearing on every layoutScope update
function demandObject(obj) {
const isObj = Object.prototype.toString.call(obj) === "[object Object]";
if (isObj || !obj) return true;
else
throw new Error(
`"${obj}" is not an object. "scoped" prop must an object`
);
}
$: props = { url, route, routes, scoped: { ...rootScope }, ...rootScope };
$: childProps = { url, route, routes, rootScope, components };
$: if (!options.unknownPropWarnings) suppressWarnings(Object.keys(props));
</script>

<svelte:component this={component} {...props} let:scoped={layoutScope}>
Expand Down
11 changes: 4 additions & 7 deletions src/rollup-plugin-filerouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = function fileRouter(options = {}) {
ignore: [],
unknownPropWarnings: true
}, options)


return {
name: 'file-router',
Expand All @@ -29,12 +29,9 @@ module.exports = function fileRouter(options = {}) {
}
}

async function createGeneratedRoutes(options){
async function createGeneratedRoutes(options) {
clientOptions = { unknownPropWarnings: options.unknownPropWarnings }
let str = await filesToRoutes(options)

if(!options.unknownPropWarnings){
str += `\n\n const suppressWarnings = ${scripts.suppressWarnings.toString()}`
str += `\n suppressWarnings()`
}
str += `\n\n export const options = ${JSON.stringify(clientOptions)}`
return str
}
37 changes: 28 additions & 9 deletions src/scripts.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@


module.exports.suppressWarnings = function () {
module.exports.suppressWarnings = (function () {
const _ignoreList = []
let initialized = false
const _warn = console.warn
console.warn = function (...params) {
const msg = params[0]
const ignores = ['scoped', 'route', 'routes', 'url']

const match = ignores.filter(prop => msg.match(new RegExp(`was created with unknown prop '${prop}'`))).length
if (!match)
_warn(...params)
return function (newIgnores = []) {
newIgnores.forEach(key => {
if (!_ignoreList.includes(key))
_ignoreList.push(key)
})

if (!initialized) {
initialized = true
console.warn = function (...params) {
const msg = params[0]
const match = _ignoreList.filter(prop => msg.match(new RegExp(`was created with unknown prop '${prop}'`))).length
if (!match)
_warn(...params)
}
}
}
}
})()

module.exports.demandObject = function (obj) {
const isObj = Object.prototype.toString.call(obj) === "[object Object]";
if (isObj || !obj) return true;
else
throw new Error(
`"${obj}" is not an object. "scoped" prop must an object`
);
}

0 comments on commit 887be93

Please sign in to comment.