File tree Expand file tree Collapse file tree
index/src/lib/post-process Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ import { GetSemanticTokens } from '@idl/parsing/semantic-tokens';
33import {
44 IParsed ,
55 PopulateScopeDetailAndResetTokenCache ,
6+ ResetVariables ,
67} from '@idl/parsing/syntax-tree' ;
78
89import { GetSyntaxProblems } from '../helpers/get-syntax-problems' ;
@@ -26,6 +27,11 @@ export function PostProcessParsed(
2627 */
2728 PopulateScopeDetailAndResetTokenCache ( parsed , cancel ) ;
2829
30+ /**
31+ * Reset variables so we can update types
32+ */
33+ ResetVariables ( parsed ) ;
34+
2935 /**
3036 * Populate types of local variables and validate them
3137 */
Original file line number Diff line number Diff line change @@ -59,6 +59,7 @@ export * from './lib/recursion-and-callbacks/tree-recurser-basic';
5959export * from './lib/remove-scope-detail' ;
6060export * from './lib/remove-scope-detail-and-reset-token-cache' ;
6161export * from './lib/reset-token-cache' ;
62+ export * from './lib/reset-variables' ;
6263export * from './lib/set-token-cache' ;
6364export * from './lib/syntax-problem-with' ;
6465export * from './lib/validator.class.interface' ;
Original file line number Diff line number Diff line change @@ -203,6 +203,7 @@ export function PopulateGlobalLocalCompileOpts(
203203 meta : {
204204 display : 'self' ,
205205 isDefined : true ,
206+ canReset : false ,
206207 docs : IDL_TRANSLATION . docs . hover . params . self ,
207208 source : GLOBAL_TOKEN_SOURCE_LOOKUP . USER ,
208209 type : ParseIDLType ( split [ 0 ] ) ,
@@ -319,6 +320,7 @@ export function PopulateGlobalLocalCompileOpts(
319320 meta : {
320321 display : 'self' ,
321322 isDefined : true ,
323+ canReset : false ,
322324 docs : IDL_TRANSLATION . docs . hover . params . self ,
323325 source : GLOBAL_TOKEN_SOURCE_LOOKUP . USER ,
324326 type : ParseIDLType ( split [ 0 ] ) ,
Original file line number Diff line number Diff line change @@ -33,6 +33,17 @@ export const LOCAL_TOKEN_LOOKUP: ILocalTokenNameLookup = {
3333export interface ILocalVariableTokenMetadata extends IBaseValueDetails {
3434 /** A flag indicating if our variable has been defined or not */
3535 isDefined : boolean ;
36+ /**
37+ * For variables that we detect, can we reset them during our post-processing?
38+ *
39+ * This is needed because we have variables from arguments and keywords which can't reset as
40+ * they are static, but we have variables inside code from things like `a = plot()` where, if
41+ * the function "plot()" changes, we have to reset the variable and re-determine the type for
42+ * said variable.
43+ *
44+ * We don't do this for args/keywords though because they are static.
45+ */
46+ canReset : boolean ;
3647 /** Flag indicating if our variable represents a static class or not */
3748 isStaticClass ?: boolean ;
3849 /**
Original file line number Diff line number Diff line change 1+ import { IParsed } from './parsed.interface' ;
2+ import { LocalToken } from './populators/populate-local.interface' ;
3+
4+ /** Resets array of tokens */
5+ function _ResetVariables ( vars : LocalToken [ ] ) {
6+ for ( let i = 0 ; i < vars . length ; i ++ ) {
7+ if ( vars [ i ] . meta . canReset ) {
8+ vars [ i ] . meta . isDefined = false ;
9+ }
10+ }
11+ }
12+
13+ /**
14+ * Resets variables that can be reset so we can update types when we
15+ * just post-process code and don't do a full parse.
16+ *
17+ * This is needed because some variables are dynamic and some are static (i.e.
18+ * arguments and keywords)
19+ */
20+ export function ResetVariables ( parsed : IParsed ) : void {
21+ /**
22+ * Reset functions
23+ */
24+ const funcs = Object . keys ( parsed . local . func ) ;
25+ for ( let i = 0 ; i < funcs . length ; i ++ ) {
26+ _ResetVariables ( Object . values ( parsed . local . func [ funcs [ i ] ] ) ) ;
27+ }
28+
29+ /**
30+ * Reset functions
31+ */
32+ const pros = Object . keys ( parsed . local . pro ) ;
33+ for ( let i = 0 ; i < pros . length ; i ++ ) {
34+ _ResetVariables ( Object . values ( parsed . local . pro [ pros [ i ] ] ) ) ;
35+ }
36+
37+ /**
38+ * Reset main
39+ */
40+ _ResetVariables ( Object . values ( parsed . local . main ) ) ;
41+ }
You can’t perform that action at this time.
0 commit comments