/
check-name-matches.ts
29 lines (25 loc) · 1.07 KB
/
check-name-matches.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import type { SourceFile } from 'typescript';
import { tsquery } from '@phenomnomnominal/tsquery';
export function checkOutputNameMatchesProjectName(
ast: SourceFile,
projectName: string
) {
const OUTPUT_SELECTOR =
'PropertyAssignment:has(Identifier[name=output]) > ObjectLiteralExpression:has(PropertyAssignment:has(Identifier[name=uniqueName]))';
const UNIQUENAME_SELECTOR =
'ObjectLiteralExpression > PropertyAssignment:has(Identifier[name=uniqueName]) > StringLiteral';
const outputNodes = tsquery(ast, OUTPUT_SELECTOR, { visitAllChildren: true });
if (outputNodes.length === 0) {
// If the output isnt set in the config, then we can still set the project name correctly
return true;
}
const uniqueNameNodes = tsquery(outputNodes[0], UNIQUENAME_SELECTOR, {
visitAllChildren: true,
});
if (uniqueNameNodes.length === 0) {
// If the uniqeName isnt set in the config, then we can still set the project name correctly
return true;
}
const uniqueName = uniqueNameNodes[0].getText().replace(/'/g, '');
return uniqueName === projectName;
}