File tree Expand file tree Collapse file tree
extensions/ql-vscode/src/view Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import { basename } from "../path" ;
2+
3+ describe ( basename . name , ( ) => {
4+ const testCases = [
5+ { path : "test.ql" , expected : "test.ql" } ,
6+ { path : "PLACEHOLDER/q0.ql" , expected : "q0.ql" } ,
7+ {
8+ path : "/home/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql" ,
9+ expected : "example.ql" ,
10+ } ,
11+ {
12+ path : "C:\\Users\\github\\projects\\vscode-codeql-starter\\codeql-custom-queries-javascript\\example.ql" ,
13+ expected : "example.ql" ,
14+ } ,
15+ {
16+ path : "/home/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript//" ,
17+ expected : "codeql-custom-queries-javascript" ,
18+ } ,
19+ {
20+ path : "C:\\Users\\github\\projects\\vscode-codeql-starter\\codeql-custom-queries-javascript\\" ,
21+ expected : "codeql-custom-queries-javascript" ,
22+ } ,
23+ {
24+ path : "/etc/hosts" ,
25+ expected : "hosts" ,
26+ } ,
27+ {
28+ path : "/etc/hosts/" ,
29+ expected : "hosts" ,
30+ } ,
31+ {
32+ path : "/etc/hosts\\test" ,
33+ expected : "hosts\\test" ,
34+ } ,
35+ ] ;
36+
37+ test . each ( testCases ) (
38+ "basename of $path is $expected" ,
39+ ( { path, expected } ) => {
40+ expect ( basename ( path ) ) . toEqual ( expected ) ;
41+ } ,
42+ ) ;
43+ } ) ;
Original file line number Diff line number Diff line change 1+ // Returns the basename of a path. Trailing directory separators are ignored.
2+ // Works for both POSIX and Windows paths.
3+ export const basename = ( path : string ) : string => {
4+ // If the path contains a forward slash, that means it's a POSIX path. Windows does not allow
5+ // forward slashes in file names.
6+ if ( path . includes ( "/" ) ) {
7+ // Trim trailing slashes
8+ path = path . replace ( / \/ + $ / , "" ) ;
9+
10+ const index = path . lastIndexOf ( "/" ) ;
11+ return index === - 1 ? path : path . slice ( index + 1 ) ;
12+ }
13+
14+ // Otherwise, it's a Windows path. We can use the backslash as a separator.
15+
16+ // Trim trailing slashes
17+ path = path . replace ( / \\ + $ / , "" ) ;
18+
19+ const index = path . lastIndexOf ( "\\" ) ;
20+ return index === - 1 ? path : path . slice ( index + 1 ) ;
21+ } ;
Original file line number Diff line number Diff line change 1- import * as path from "path" ;
21import * as React from "react" ;
32import { useMemo } from "react" ;
43import styled from "styled-components" ;
@@ -12,6 +11,7 @@ import { QueryDetails } from "./QueryDetails";
1211import { VariantAnalysisActions } from "./VariantAnalysisActions" ;
1312import { VariantAnalysisStats } from "./VariantAnalysisStats" ;
1413import { parseDate } from "../../pure/date" ;
14+ import { basename } from "../common/path" ;
1515
1616export type VariantAnalysisHeaderProps = {
1717 variantAnalysis : VariantAnalysis ;
@@ -68,7 +68,7 @@ export const VariantAnalysisHeader = ({
6868 < Row >
6969 < QueryDetails
7070 queryName = { variantAnalysis . query . name }
71- queryFileName = { path . basename ( variantAnalysis . query . filePath ) }
71+ queryFileName = { basename ( variantAnalysis . query . filePath ) }
7272 onOpenQueryFileClick = { onOpenQueryFileClick }
7373 onViewQueryTextClick = { onViewQueryTextClick }
7474 />
You can’t perform that action at this time.
0 commit comments