Skip to content
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
30 changes: 30 additions & 0 deletions src/commands/usages/usages.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import fs from 'fs'
import os from 'os'
import path from 'path'
import { test } from '@oclif/test'
import { expect } from 'vitest'
import { BASE_URL } from '../../api/common'
Expand Down Expand Up @@ -135,4 +138,31 @@ describe('usages', () => {
expect(ctx.stdout).toMatchSnapshot()
},
)

const htmlOutputDir = fs.mkdtempSync(path.join(os.tmpdir(), 'dvc-usages-'))
const htmlOutputPath = path.join(htmlOutputDir, 'usages.json')
afterAll(() => fs.rmSync(htmlOutputDir, { recursive: true, force: true }))

test.stdout()
.command([
'usages',
'--include',
'test-utils/fixtures/usages/sample.html',
'--match-pattern',
'html=isFeatureEnabled\\("([^"]*)"\\)',
'--format',
'json',
'--output',
htmlOutputPath,
])
.it(
'sets language from the file extension for custom match patterns',
() => {
const [usage] = JSON.parse(
fs.readFileSync(htmlOutputPath, 'utf-8'),
)
expect(usage.key).toEqual('html-flag')
expect(usage.references[0].language).toEqual('html')
},
)
})
4 changes: 2 additions & 2 deletions src/utils/parsers/BaseParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
VariableUsageMatch,
} from './types'
import * as usage from '../../commands/usages/types'
import { LANGUAGE_MAP } from '../parsers'
import { getLanguageForExtension } from '../parsers'

type MatchWithRange = {
// Variable name
Expand Down Expand Up @@ -395,7 +395,7 @@ export abstract class BaseParser {
},
fileName: file.name,
content: bufferedContent,
language: LANGUAGE_MAP[fileExtension],
language: getLanguageForExtension(fileExtension),
...(match.isUnknown ? { isUnknown: true } : {}),
})
})
Expand Down
13 changes: 13 additions & 0 deletions src/utils/parsers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,16 @@ export const LANGUAGE_MAP: Record<string, string> = {
php: 'php',
dart: 'dart',
}

const SAFE_EXTENSION_PATTERN = /^[a-z0-9]{1,16}$/i

/**
* Map a file extension to a language, falling back to the extension itself
* for custom match patterns. Unsafe extensions fall back to 'unknown'.
*/
export const getLanguageForExtension = (extension: string): string => {
if (Object.hasOwn(LANGUAGE_MAP, extension)) return LANGUAGE_MAP[extension]
return SAFE_EXTENSION_PATTERN.test(extension)
? extension.toLowerCase()
: 'unknown'
}
23 changes: 23 additions & 0 deletions src/utils/parsers/language.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { expect } from 'vitest'
import { getLanguageForExtension } from '.'

describe('getLanguageForExtension', () => {
it('uses the mapped language for built-in extensions', () => {
expect(getLanguageForExtension('ts')).toEqual('typescript')
expect(getLanguageForExtension('kt')).toEqual('kotlin')
})

it('falls back to the extension for unmapped alphanumeric extensions', () => {
expect(getLanguageForExtension('html')).toEqual('html')
expect(getLanguageForExtension('Vue')).toEqual('vue')
expect(getLanguageForExtension('constructor')).toEqual('constructor')
})

it('falls back to unknown for unsafe or empty extensions', () => {
expect(getLanguageForExtension('<img src=x onerror=alert(1)>')).toEqual(
'unknown',
)
expect(getLanguageForExtension('a'.repeat(17))).toEqual('unknown')
expect(getLanguageForExtension('')).toEqual('unknown')
})
})
9 changes: 9 additions & 0 deletions test-utils/fixtures/usages/sample.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<html>
<body>
<script>
if (isFeatureEnabled("html-flag")) {
showBanner()
}
</script>
</body>
</html>
Loading