Skip to content
This repository has been archived by the owner on Oct 12, 2023. It is now read-only.

Commit

Permalink
feat: check ios class (#251)
Browse files Browse the repository at this point in the history
  • Loading branch information
echosoar authored Dec 29, 2021
1 parent a4557f9 commit 5970eab
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/cli-plugin-check/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"@midwayjs/luckyeye": "^1.0.2",
"chalk": "^4.1.1",
"fs-extra": "^8.1.0",
"globby": "^10.0.1",
"js-yaml": "^4.1.0"
},
"devDependencies": {
Expand Down
55 changes: 54 additions & 1 deletion packages/cli-plugin-check/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as chalk from 'chalk';
import * as YAML from 'js-yaml';
import { Locator, AnalyzeResult } from '@midwayjs/locate';
import { transformToRelative } from './utils';
import * as globby from 'globby';

enum ProjectType {
FaaS = 'faas',
Expand Down Expand Up @@ -120,6 +121,7 @@ export class CheckPlugin extends BasePlugin {
const ruleList: RunnerItem[] = [
await this.projectStruct(),
await this.packageJson(),
await this.ruleIoc(),
];

if (this.projectType === ProjectType.FaaS) {
Expand Down Expand Up @@ -374,7 +376,7 @@ export class CheckPlugin extends BasePlugin {
async ruleFaaSDecorator(): Promise<RunnerItem> {
// 校验是否存在 decorator 重名
// 校验 @Logger 装饰器所在class是否被继承
return runner => {};
return () => {};
}

// 校验yaml格式
Expand Down Expand Up @@ -555,6 +557,57 @@ export class CheckPlugin extends BasePlugin {
};
}

async ruleIoc() {
return runner => {
runner.group('ioc check').check('class define', async () => {
const { tsCodeRoot } = this.globalData;
if (!existsSync(tsCodeRoot)) {
return [CHECK_SKIP];
}

const tsSourceFileList = await globby(['**/*.ts'], {
cwd: tsCodeRoot,
});
const classNameMap = {};
for (const tsSourceFile of tsSourceFileList) {
const file = join(tsCodeRoot, tsSourceFile);
const code = readFileSync(file).toString();
// @Provider() export default class xxx extends xxx {}
const reg =
/@(?:provider|controller)\([^)]*\)(?:\n|\s)*(export)?(\s+default\s+)?\s*class\s+(.*?)\s+/gi;
let execRes;
while ((execRes = reg.exec(code))) {
const className = execRes[3];
// export
if (!execRes[1]) {
return [
false,
`class ${className} need export in ${tsSourceFile}`,
];
}

// export default
if (execRes[2]) {
return [
false,
`class ${className} can not export "default" in ${tsSourceFile}`,
];
}

if (classNameMap[className]) {
return [
false,
`there is a duplicate class name(${className}) in ${classNameMap[className]} and ${tsSourceFile}`,
];
}
classNameMap[className] = tsSourceFile;
}
}
return [true];
});
};
}

private getCheckReporter() {
return {
reportGroup: data => {
Expand Down
6 changes: 6 additions & 0 deletions packages/cli-plugin-check/test/config-export.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,10 @@ describe('test/config-export.test.ts', () => {
)
);
});
it('export class', async () => {
const cwd = join(__dirname, 'fixtures/config-export');
const logs = await runCheck(cwd);
const logStr = logs.join('\n');
assert(logStr.includes('class A need export in index.ts'));
});
});
6 changes: 6 additions & 0 deletions packages/cli-plugin-check/test/faas.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,17 @@ describe('test/faas.test.ts', () => {
assert(logStr.includes('tsconfig target need ≤ es2018'));
assert(logStr.includes('YAML package.include type should be Array'));
assert(logStr.includes("function 'test' http.trigger need path attribute"));
assert(logStr.includes('class A can not export "default" in index.ts'));
});
it('configuration', async () => {
const cwd = join(__dirname, 'fixtures/faas-config');
const logs = await runCheck(cwd);
const logStr = logs.join('\n');
assert(logStr.includes('no prod or default config'));
assert(
logStr.includes(
'there is a duplicate class name(A) in index.ts and index2.ts'
)
);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { Provider } from "@midwayjs/decorator";

@Provider()
class A {}
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
console.log("hello midway")
import { Provider } from "@midwayjs/decorator";

@Provider()
export class A {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Controller } from "@midwayjs/decorator";

@Provider()
@Controller('/')
export class A {}
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
console.log("hello midway")
import { Provider } from "@midwayjs/decorator";

@Provider()
export default class A {}

0 comments on commit 5970eab

Please sign in to comment.