Skip to content

Commit

Permalink
feat: throw when component block has no name
Browse files Browse the repository at this point in the history
  • Loading branch information
hunyan-io committed May 10, 2023
1 parent 10cbd6c commit c3a1caf
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
4 changes: 3 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"extends": ["eslint-config-unjs"],
"rules": {
"@typescript-eslint/no-non-null-assertion": "off"
"@typescript-eslint/no-non-null-assertion": "off",
"unicorn/no-null": "off",
"unicorn/no-lonely-if": "off"
}
}
19 changes: 14 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,12 @@ export default function vueNestedSFC(): PluginOption {
}
return {
code: genExportsCode(request.filename, exportedComponents, code),
// eslint-disable-next-line unicorn/no-null
map: null,
};
} else if (request.query.type === "component" && request.query.name) {
} else if (request.query.type === "component") {
if (typeof request.query.name !== "string") {
throw new TypeError("Component blocks require a name attribute.");
}
const componentName = pascalCase(request.query.name);
cache.registerNestedComponent(request.filename, componentName);
return {
Expand All @@ -112,11 +114,12 @@ export default function vueNestedSFC(): PluginOption {
cache.updateFileCache(file, await read());
const nextDescriptor = cache.getDescriptor(file);

const mainModule = server.moduleGraph.getModuleById(file);

if (
prevDescriptor.customBlocks.length !==
nextDescriptor.customBlocks.length
) {
const mainModule = server.moduleGraph.getModuleById(file);
if (mainModule) {
affectedModules.add(mainModule);
}
Expand All @@ -127,6 +130,9 @@ export default function vueNestedSFC(): PluginOption {
continue;
}
if (typeof block.attrs.name !== "string") {
if (mainModule) {
affectedModules.add(mainModule);
}
continue;
}
const name = pascalCase(block.attrs.name);
Expand All @@ -136,8 +142,11 @@ export default function vueNestedSFC(): PluginOption {
typeof nextBlock.attrs.name === "string" &&
pascalCase(nextBlock.attrs.name) === name
);
if (!nextBlock || nextBlock.attrs.name !== block.attrs.name) {
const mainModule = server.moduleGraph.getModuleById(file);
if (
!nextBlock ||
nextBlock.attrs.name !== block.attrs.name ||
nextBlock.attrs.export !== block.attrs.export
) {
if (mainModule) {
affectedModules.add(mainModule);
}
Expand Down

0 comments on commit c3a1caf

Please sign in to comment.