1namespace ts.projectSystem { 2 describe("unittests:: tsserver:: condition check", () => { 3 it("works condition check", () => { 4 const aUser: File = { 5 path: "/a.ts", 6 content: `import { y } from "./b"; 7 if(canIUse("SystemCapability.ArkUI.Core")) { 8 y.test() 9 y.test2 10 } 11 y.test() 12 y.test2 13}` 14 }; 15 const bUser: File = { 16 path: "/b.ts", 17 content: ` 18/** 19 * @sysCap SystemCapability.ArkUI.Core1 20 */ 21interface InfoType { 22 23} 24 25export class y { 26/** 27 * @sysCap SystemCapability.ArkUI.Core 28 */ 29static test(): void { 30 31} 32/** 33 * @sysCap SystemCapability.ArkUI.Core 34 */ 35static test2: InfoType; 36}` 37 }; 38 const cUser: File = { 39 path: "/c.d.ts", 40 content: `declare function canIUse(key: string): boolean;` 41 }; 42 const tsconfigFile: File = { 43 path: "/tsconfig.json", 44 content: JSON.stringify({ 45 compilerOptions: { 46 target: "es6", 47 module: "es6", 48 baseUrl: "./", // all paths are relative to the baseUrl 49 paths: { 50 "~/*": ["*"] // resolve any `~/foo/bar` to `<baseUrl>/foo/bar` 51 } 52 }, 53 exclude: [ 54 "api", 55 "build", 56 "node_modules", 57 "public", 58 "seeds", 59 "sql_updates", 60 "tests.build" 61 ] 62 }) 63 }; 64 65 const projectFiles = [aUser, bUser, cUser, tsconfigFile]; 66 const host = createServerHost(projectFiles); 67 host.getFileCheckedModuleInfo = (sourceFilePath: string) => { 68 Debug.log(sourceFilePath); 69 return { 70 fileNeedCheck: true, 71 checkPayload: undefined, 72 currentFileName: "", 73 } 74 } 75 host.getJsDocNodeCheckedConfig = (jsDocFileCheckInfo: FileCheckModuleInfo, sourceFilePath: string) => { 76 Debug.log(jsDocFileCheckInfo.fileNeedCheck.toString()); 77 Debug.log(sourceFilePath); 78 return { 79 nodeNeedCheck: true, 80 checkConfig: [{ 81 tagName: ["sysCap"], 82 message: "The statement must be written use the function 'canIUse' under the if condition.", 83 needConditionCheck: true, 84 specifyCheckConditionFuncName: "canIUse", 85 type: DiagnosticCategory.Warning, 86 tagNameShouldExisted: false, 87 }] 88 }; 89 }; 90 host.getJsDocNodeConditionCheckedResult = (jsDocFileCheckedInfo: FileCheckModuleInfo, jsDocs: JsDocTagInfo[]) => { 91 Debug.log(jsDocFileCheckedInfo.fileNeedCheck.toString()); 92 Debug.log(jsDocs.toString()); 93 return { 94 valid: false, 95 message: "", 96 type: DiagnosticCategory.Warning 97 }; 98 }; 99 const session = createSession(host); 100 const projectService = session.getProjectService(); 101 const { configFileName } = projectService.openClientFile(aUser.path); 102 103 assert.isDefined(configFileName, `should find config`); 104 105 const project = projectService.configuredProjects.get(tsconfigFile.path)!; 106 const response = project.getLanguageService().getSuggestionDiagnostics(aUser.path); 107 assert.deepEqual(response[0].messageText, "The statement must be written use the function 'canIUse' under the if condition."); 108 }); 109 }); 110}