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

feat: add vue-strong component-files rule #68

Merged
merged 2 commits into from
Jul 23, 2024
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
3 changes: 3 additions & 0 deletions src/analyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { checkFunctionSize, reportFunctionSize } from './rules/rrd/functionSize'
import { checkParameterCount, reportParameterCount } from './rules/rrd/parameterCount'
import { checkShortVariableName, reportShortVariableName } from './rules/rrd/shortVariableName'
import { checkSimpleComputed, reportSimpleComputed } from './rules/vue-strong/simpleComputed'
import { checkComponentFiles, reportComponentFiles } from './rules/vue-strong/componentFiles'
import { checkImplicitParentChildCommunication, reportImplicitParentChildCommunication } from './rules/vue-caution/implicitParentChildCommunication'

let filesCount = 0
Expand Down Expand Up @@ -82,6 +83,7 @@ export const analyze = (dir: string) => {
checkSimpleProp(script, filePath)

checkPropNameCasing(script, filePath)
checkComponentFiles(script, filePath)

checkScriptLength(script, filePath)
checkCyclomaticComplexity(script, filePath)
Expand Down Expand Up @@ -126,6 +128,7 @@ export const analyze = (dir: string) => {
errors += reportQuotedAttributeValues()
errors += reportDirectiveShorthands()
errors += reportSimpleComputed()
errors += reportComponentFiles()

// vue-reccomended rules

Expand Down
46 changes: 46 additions & 0 deletions src/rules/vue-strong/componentFiles.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { describe, expect, it, vi } from 'vitest';
import { SFCScriptBlock } from '@vue/compiler-sfc';
import { BG_ERR, BG_RESET, BG_WARN } from '../asceeCodes';
import { checkComponentFiles, reportComponentFiles } from './componentFiles';

const mockConsoleLog = vi.spyOn(console, 'log').mockImplementation(() => {});

describe('checkComponentFiles', () => {
it('should not report files where each component is its own file', () => {
const script = {
content: `
<script setup>
console.log('This is just fine');
</script>
`
} as SFCScriptBlock
const filename = 'component.vue'
checkComponentFiles(script, filename)
expect(reportComponentFiles()).toBe(0)
expect(mockConsoleLog).not.toHaveBeenCalled()
})

it('should report files each component is not its own file', () => {
const script = {
content: `
<script setup>
app.component('TodoList', {
// ...
})

app.component('TodoItem', {
// ...
})
</script>
`
} as SFCScriptBlock
const filename = 'component.vue'
const lineNumber = 6;
checkComponentFiles(script, filename)
expect(reportComponentFiles()).toBe(2)
expect(mockConsoleLog).toHaveBeenCalled()
expect(mockConsoleLog).toHaveBeenLastCalledWith(
`- ${filename}#${lineNumber} ${BG_WARN}(TodoItem)${BG_RESET} 🚨`
)
})
})
39 changes: 39 additions & 0 deletions src/rules/vue-strong/componentFiles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { SFCScriptBlock } from "@vue/compiler-sfc";
import { BG_RESET, BG_WARN, TEXT_WARN, TEXT_RESET, TEXT_INFO, BG_ERR } from '../asceeCodes'
import { getUniqueFilenameCount } from "../../helpers";
import getLineNumber from "../getLineNumber";

type ComponentFiles = { filename: string, message: string };

const componentFiles: ComponentFiles[] = [];

const checkComponentFiles = (script: SFCScriptBlock, filePath: string) => {
// regular expression to match `.component('anyString', { ... })` pattern
const regex = /app\.component\('([^']+)',\s*\{[^}]*\}\)/g;

const matches = [...script.content.matchAll(regex)].map(match => match[1].trim());

matches.forEach(match => {
const lineNumber = getLineNumber(script.content.trim(), match);
const firstPart = match.split('\n').at(0)?.trim() || ''
componentFiles.push({ filename: filePath, message: `${filePath}#${lineNumber} ${BG_WARN}(${firstPart})${BG_RESET}` })
})
}

const reportComponentFiles = () => {
if (componentFiles.length > 0) {
// count only non duplicated objects (by its `filename` property)
const fileCount = getUniqueFilenameCount<ComponentFiles>(componentFiles, 'filename');

console.log(
`\n${TEXT_INFO}vue-strong${TEXT_RESET} ${BG_ERR}component files${BG_RESET} detected in ${fileCount} files.`
)
console.log(`👉 ${TEXT_WARN}Whenever a build system is available to concatenate files, each component should be in its own file.${TEXT_RESET} See: https://vuejs.org/style-guide/rules-strongly-recommended.html#component-files`)
componentFiles.forEach(file => {
console.log(`- ${file.message} 🚨`)
})
}
return componentFiles.length;
}

export { checkComponentFiles, reportComponentFiles };
Loading