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: ` 18export class y { 19/** 20 * @sysCap SystemCapability.ArkUI.Core 21 */ 22static test(): void { 23 24} 25/** 26 * @sysCap SystemCapability.ArkUI.Core 27 */ 28static test2: number = 2; 29}` 30 }; 31 const cUser: File = { 32 path: "/c.d.ts", 33 content: `declare function canIUse(key: string): boolean;` 34 }; 35 const tsconfigFile: File = { 36 path: "/tsconfig.json", 37 content: JSON.stringify({ 38 compilerOptions: { 39 target: "es6", 40 module: "es6", 41 baseUrl: "./", // all paths are relative to the baseUrl 42 paths: { 43 "~/*": ["*"] // resolve any `~/foo/bar` to `<baseUrl>/foo/bar` 44 } 45 }, 46 exclude: [ 47 "api", 48 "build", 49 "node_modules", 50 "public", 51 "seeds", 52 "sql_updates", 53 "tests.build" 54 ] 55 }) 56 }; 57 58 const projectFiles = [aUser, bUser, cUser, tsconfigFile]; 59 const host = createServerHost(projectFiles); 60 host.getTagNameNeededCheckByFile = (containFilePath: string, sourceFilePath: string) => { 61 Debug.log(containFilePath); 62 Debug.log(sourceFilePath); 63 return { 64 needCheck: true, 65 checkConfig: [{ 66 tagName: "sysCap", 67 message: "The statement must be written use the function 'canIUse' under the if condition.", 68 needConditionCheck: true, 69 specifyCheckConditionFuncName: "canIUse", 70 type: DiagnosticCategory.Warning, 71 tagNameShouldExisted: false, 72 }] 73 }; 74 }; 75 host.getExpressionCheckedResultsByFile = (filePath: string, jsDocs: JSDocTagInfo[]) => { 76 Debug.log(filePath); 77 Debug.log(jsDocs.toString()); 78 return { 79 valid: false, 80 }; 81 }; 82 const session = createSession(host); 83 const projectService = session.getProjectService(); 84 const { configFileName } = projectService.openClientFile(aUser.path); 85 86 assert.isDefined(configFileName, `should find config`); 87 88 const project = projectService.configuredProjects.get(tsconfigFile.path)!; 89 const response = project.getLanguageService().getSuggestionDiagnostics(aUser.path); 90 assert.deepEqual(response[0].messageText, "The statement must be written use the function 'canIUse' under the if condition."); 91 }); 92 }); 93} 94