Skip to content

Commit dc0927f

Browse files
committed
const check
1 parent ff034eb commit dc0927f

File tree

2 files changed

+84
-3
lines changed

2 files changed

+84
-3
lines changed

eslint-local-rules/require-platform-declaration.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ module.exports = {
107107
filename.endsWith('.spec.js') ||
108108
filename.endsWith('.tests.js') ||
109109
filename.endsWith('.test-d.ts') ||
110+
filename.endsWith('.gen.ts') ||
110111
filename.endsWith('.d.ts') ||
111112
filename.includes('/__mocks__/') ||
112113
filename.includes('/tests/')) {

scripts/validate-platform-isolation-ts.js

Lines changed: 83 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,19 @@ const filesWithoutExport = [];
113113
// Track files with invalid platform values
114114
const filesWithInvalidPlatforms = [];
115115

116+
// Track files with non-const declaration
117+
const filesWithNonConst = [];
118+
119+
// Track files with non-literal values
120+
const filesWithNonLiterals = [];
121+
116122
/**
117123
* Extracts __platforms array from AST
118124
*/
119125
function extractSupportedPlatformsFromAST(sourceFile) {
120126
let platforms = null;
127+
let hasNonStringLiteral = false;
128+
let isNotConst = false;
121129

122130
function visit(node) {
123131
// Look for: export const __platforms = [...]
@@ -128,11 +136,18 @@ function extractSupportedPlatformsFromAST(sourceFile) {
128136
);
129137

130138
if (hasExport) {
139+
// Check if declaration is const
140+
const isConst = (node.declarationList.flags & ts.NodeFlags.Const) !== 0;
141+
131142
for (const declaration of node.declarationList.declarations) {
132143
if (ts.isVariableDeclaration(declaration) &&
133144
ts.isIdentifier(declaration.name) &&
134145
declaration.name.text === '__platforms') {
135146

147+
if (!isConst) {
148+
isNotConst = true;
149+
}
150+
136151
let initializer = declaration.initializer;
137152

138153
// Handle "as const" assertion: [...] as const
@@ -151,6 +166,9 @@ function extractSupportedPlatformsFromAST(sourceFile) {
151166
for (const element of initializer.elements) {
152167
if (ts.isStringLiteral(element)) {
153168
platforms.push(element.text);
169+
} else {
170+
// Non-string literal found (variable, computed value, etc.)
171+
hasNonStringLiteral = true;
154172
}
155173
}
156174
return; // Found it, stop visiting
@@ -164,6 +182,16 @@ function extractSupportedPlatformsFromAST(sourceFile) {
164182
}
165183

166184
visit(sourceFile);
185+
186+
if (platforms !== null) {
187+
if (isNotConst) {
188+
return 'NOT_CONST';
189+
}
190+
if (hasNonStringLiteral) {
191+
return 'NOT_LITERALS';
192+
}
193+
}
194+
167195
return platforms;
168196
}
169197

@@ -226,6 +254,20 @@ function getSupportedPlatforms(filePath) {
226254
// Extract platforms from AST
227255
const supportedPlatforms = extractSupportedPlatformsFromAST(sourceFile);
228256

257+
if (supportedPlatforms === 'NOT_CONST') {
258+
filesWithNonConst.push(filePath);
259+
result = 'NOT_CONST';
260+
platformCache.set(filePath, result);
261+
return result;
262+
}
263+
264+
if (supportedPlatforms === 'NOT_LITERALS') {
265+
filesWithNonLiterals.push(filePath);
266+
result = 'NOT_LITERALS';
267+
platformCache.set(filePath, result);
268+
return result;
269+
}
270+
229271
if (supportedPlatforms && supportedPlatforms.length > 0) {
230272
// Validate platform values
231273
const validation = validatePlatformValues(supportedPlatforms, filePath);
@@ -309,8 +351,10 @@ function isUniversal(platforms) {
309351
* - Platform-specific files can only import from universal or files supporting all their platforms
310352
*/
311353
function isPlatformCompatible(filePlatforms, importPlatforms) {
312-
// If either is missing platforms, not compatible
313-
if (filePlatforms === 'MISSING' || importPlatforms === 'MISSING') {
354+
// If either has any error state, not compatible
355+
if (filePlatforms === 'MISSING' || importPlatforms === 'MISSING' ||
356+
filePlatforms === 'NOT_CONST' || importPlatforms === 'NOT_CONST' ||
357+
filePlatforms === 'NOT_LITERALS' || importPlatforms === 'NOT_LITERALS') {
314358
return false;
315359
}
316360

@@ -524,14 +568,15 @@ function findSourceFiles(dir, files = []) {
524568
findSourceFiles(fullPath, files);
525569
}
526570
} else if (entry.isFile()) {
527-
// Only include TypeScript and JavaScript files, skip test files
571+
// Only include TypeScript and JavaScript files, skip test files and generated files
528572
if ((entry.name.endsWith('.ts') || entry.name.endsWith('.js')) &&
529573
!entry.name.endsWith('.spec.ts') &&
530574
!entry.name.endsWith('.test.ts') &&
531575
!entry.name.endsWith('.tests.ts') &&
532576
!entry.name.endsWith('.tests.js') &&
533577
!entry.name.endsWith('.umdtests.js') &&
534578
!entry.name.endsWith('.test-d.ts') &&
579+
!entry.name.endsWith('.gen.ts') &&
535580
!entry.name.endsWith('.d.ts')) {
536581
files.push(fullPath);
537582
}
@@ -585,6 +630,41 @@ function main() {
585630

586631
console.log('✅ All files have __platforms export\n');
587632

633+
// Report files with non-const declaration
634+
if (filesWithNonConst.length > 0) {
635+
console.error(`❌ Found ${filesWithNonConst.length} file(s) with __platforms not declared as const:\n`);
636+
637+
for (const file of filesWithNonConst) {
638+
const relativePath = path.relative(process.cwd(), file);
639+
console.error(` 📄 ${relativePath}`);
640+
}
641+
642+
console.error('\n');
643+
console.error('REQUIRED: __platforms must be declared as const');
644+
console.error('Use: export const __platforms: Platform[] = [...]\n');
645+
646+
process.exit(1);
647+
}
648+
649+
// Report files with non-literal values
650+
if (filesWithNonLiterals.length > 0) {
651+
console.error(`❌ Found ${filesWithNonLiterals.length} file(s) with __platforms containing non-literal values:\n`);
652+
653+
for (const file of filesWithNonLiterals) {
654+
const relativePath = path.relative(process.cwd(), file);
655+
console.error(` 📄 ${relativePath}`);
656+
}
657+
658+
console.error('\n');
659+
console.error('REQUIRED: __platforms array must contain only string literals');
660+
console.error('Use: export const __platforms: Platform[] = [\'browser\', \'node\']');
661+
console.error('Do NOT use variables, computed values, or spread operators\n');
662+
663+
process.exit(1);
664+
}
665+
666+
console.log('✅ All __platforms declarations are const with string literals\n');
667+
588668
// Report files with invalid platform values
589669
if (filesWithInvalidPlatforms.length > 0) {
590670
console.error(`❌ Found ${filesWithInvalidPlatforms.length} file(s) with invalid platform values:\n`);

0 commit comments

Comments
 (0)