1- /* eslint-disable @typescript-eslint/no-var-requires */
2- /* eslint-disable no-inner-declarations */
1+ /**
2+ * Copyright 2025, Optimizely
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+
317/**
418 * Platform Utilities
519 *
6- * Shared utilities for platform isolation validation used by both
7- * the validation script and ESLint rule.
20+ * Shared utilities for platform isolation validation
821 */
922
23+
24+ /* eslint-disable @typescript-eslint/no-var-requires */
25+ /* eslint-disable no-inner-declarations */
26+
1027const fs = require ( 'fs' ) ;
1128const path = require ( 'path' ) ;
1229const ts = require ( 'typescript' ) ;
@@ -26,58 +43,52 @@ function getValidPlatforms(workspaceRoot) {
2643 return validPlatformsCache ;
2744 }
2845
29- try {
30- const platformSupportPath = path . join ( workspaceRoot , 'lib' , 'platform_support.ts' ) ;
31-
32- if ( ! fs . existsSync ( platformSupportPath ) ) {
33- throw new Error ( `platform_support.ts not found at ${ platformSupportPath } ` ) ;
34- }
46+ const platformSupportPath = path . join ( workspaceRoot , 'lib' , 'platform_support.ts' ) ;
47+
48+ if ( ! fs . existsSync ( platformSupportPath ) ) {
49+ throw new Error ( `platform_support.ts not found at ${ platformSupportPath } ` ) ;
50+ }
3551
36- const content = fs . readFileSync ( platformSupportPath , 'utf8' ) ;
37- const sourceFile = ts . createSourceFile (
38- platformSupportPath ,
39- content ,
40- ts . ScriptTarget . Latest ,
41- true
42- ) ;
43-
44- const platforms = [ ] ;
45-
46- // Visit all nodes in the AST
47- function visit ( node ) {
48- // Look for: export type Platform = 'browser' | 'node' | ...
49- if ( ts . isTypeAliasDeclaration ( node ) &&
50- node . name . text === 'Platform' &&
51- node . modifiers ?. some ( m => m . kind === ts . SyntaxKind . ExportKeyword ) ) {
52-
53- // Parse the union type
54- if ( ts . isUnionTypeNode ( node . type ) ) {
55- for ( const type of node . type . types ) {
56- if ( ts . isLiteralTypeNode ( type ) && ts . isStringLiteral ( type . literal ) ) {
57- platforms . push ( type . literal . text ) ;
58- }
52+ const content = fs . readFileSync ( platformSupportPath , 'utf8' ) ;
53+ const sourceFile = ts . createSourceFile (
54+ platformSupportPath ,
55+ content ,
56+ ts . ScriptTarget . Latest ,
57+ true
58+ ) ;
59+
60+ const platforms = [ ] ;
61+
62+ // Visit all nodes in the AST
63+ function visit ( node ) {
64+ // Look for: export type Platform = 'browser' | 'node' | ...
65+ if ( ts . isTypeAliasDeclaration ( node ) &&
66+ node . name . text === 'Platform' &&
67+ node . modifiers ?. some ( m => m . kind === ts . SyntaxKind . ExportKeyword ) ) {
68+
69+ // Parse the union type
70+ if ( ts . isUnionTypeNode ( node . type ) ) {
71+ for ( const type of node . type . types ) {
72+ if ( ts . isLiteralTypeNode ( type ) && ts . isStringLiteral ( type . literal ) ) {
73+ platforms . push ( type . literal . text ) ;
5974 }
60- } else if ( ts . isLiteralTypeNode ( node . type ) && ts . isStringLiteral ( node . type . literal ) ) {
61- // Handle single literal type: type Platform = 'browser';
62- platforms . push ( node . type . literal . text ) ;
6375 }
76+ } else if ( ts . isLiteralTypeNode ( node . type ) && ts . isStringLiteral ( node . type . literal ) ) {
77+ // Handle single literal type: type Platform = 'browser';
78+ platforms . push ( node . type . literal . text ) ;
6479 }
65-
66- ts . forEachChild ( node , visit ) ;
6780 }
6881
69- visit ( sourceFile ) ;
70-
71- if ( platforms . length > 0 ) {
72- validPlatformsCache = platforms ;
73- return validPlatformsCache ;
74- }
75- } catch ( error ) {
76- console . warn ( 'Could not parse platform_support.ts, using fallback values:' , error . message ) ;
82+ ts . forEachChild ( node , visit ) ;
83+ }
84+
85+ visit ( sourceFile ) ;
86+
87+ if ( platforms . length === 0 ) {
88+ throw new Error ( `Could not extract Platform type from ${ platformSupportPath } ` ) ;
7789 }
7890
79- // Fallback to default platforms
80- validPlatformsCache = [ 'browser' , 'node' , 'react_native' , '__universal__' ] ;
91+ validPlatformsCache = platforms ;
8192 return validPlatformsCache ;
8293}
8394
@@ -93,11 +104,10 @@ function getValidPlatforms(workspaceRoot) {
93104 * - invalidValues: string[] - list of invalid platform values (for INVALID_VALUES type)
94105 *
95106 * @param {ts.SourceFile } sourceFile - TypeScript source file AST
96- * @param {string } filePath - File path for context in error messages
97107 * @param {string[] } validPlatforms - Array of valid platform values
98108 * @returns {Object }
99109 */
100- function extractPlatformsFromAST ( sourceFile , filePath , validPlatforms ) {
110+ function extractPlatformsFromAST ( sourceFile , validPlatforms ) {
101111 let found = false ;
102112 let isArray = false ;
103113 let platforms = [ ] ;
@@ -222,21 +232,22 @@ function extractPlatformsFromAST(sourceFile, filePath, validPlatforms) {
222232/**
223233 * Extract platforms from a file path with detailed error reporting
224234 *
225- * @param {string } filePath - Absolute path to the file
226- * @param {string } workspaceRoot - Workspace root for resolving valid platforms
235+ * @param {string } filePath - Relative path to the file (from workspaceRoot)
236+ * @param {string } workspaceRoot - Workspace root directory
227237 * @returns {Object } Result object with success, platforms, and error information
228238 */
229239function extractPlatformsFromFile ( filePath , workspaceRoot ) {
230240 try {
231241 const validPlatforms = workspaceRoot ? getValidPlatforms ( workspaceRoot ) : null ;
232- const content = fs . readFileSync ( filePath , 'utf-8' ) ;
242+ const absolutePath = path . resolve ( workspaceRoot , filePath ) ;
243+ const content = fs . readFileSync ( absolutePath , 'utf-8' ) ;
233244 const sourceFile = ts . createSourceFile (
234- filePath ,
245+ absolutePath ,
235246 content ,
236247 ts . ScriptTarget . Latest ,
237248 true
238249 ) ;
239- return extractPlatformsFromAST ( sourceFile , filePath , validPlatforms ) ;
250+ return extractPlatformsFromAST ( sourceFile , validPlatforms ) ;
240251 } catch ( error ) {
241252 return {
242253 success : false ,
0 commit comments