-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add error marker when invalid main function (#9489)
## Before ![image](https://github.com/user-attachments/assets/f6af6721-0896-48b5-8556-9d2a9c19de06) ## After ![image](https://github.com/user-attachments/assets/c59407c8-8244-4906-9d05-713909a19c33)
- Loading branch information
Showing
4 changed files
with
123 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
...w/workflow-steps/workflow-actions/utils/__tests__/getWrongExportedFunctionMarkers.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { getWrongExportedFunctionMarkers } from '@/workflow/workflow-steps/workflow-actions/utils/getWrongExportedFunctionMarkers'; | ||
|
||
describe('getWrongExportedFunctionMarkers', () => { | ||
it('should return marker when no exported function', () => { | ||
const value = 'const main = async () => {}'; | ||
const result = getWrongExportedFunctionMarkers(value); | ||
expect(result.length).toEqual(1); | ||
expect(result[0].message).toEqual( | ||
'An exported "main" arrow function is required.', | ||
); | ||
}); | ||
|
||
it('should return marker when no wrong exported function', () => { | ||
const value = 'export const wrongMain = async () => {}'; | ||
const result = getWrongExportedFunctionMarkers(value); | ||
expect(result.length).toEqual(1); | ||
}); | ||
|
||
it('should return no marker when valid exported function', () => { | ||
const value = 'export const main = async () => {}'; | ||
const result = getWrongExportedFunctionMarkers(value); | ||
expect(result.length).toEqual(0); | ||
}); | ||
|
||
it('should return handle multiple spaces', () => { | ||
const value = 'export const main = async () => {}'; | ||
const result = getWrongExportedFunctionMarkers(value); | ||
expect(result.length).toEqual(0); | ||
}); | ||
}); |
60 changes: 60 additions & 0 deletions
60
...modules/workflow/workflow-steps/workflow-actions/utils/getWrongExportedFunctionMarkers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { isDefined } from 'twenty-ui'; | ||
|
||
const getSubstringCoordinate = ( | ||
text: string, | ||
substring: string, | ||
): { line: number; column: number } | null => { | ||
const lines = text.split('\n'); | ||
|
||
for (let lineIndex = 0; lineIndex < lines.length; lineIndex++) { | ||
const columnIndex = lines[lineIndex].indexOf(substring); | ||
if (columnIndex !== -1) { | ||
return { | ||
line: lineIndex + 1, // 1-based line number | ||
column: columnIndex + 1, // 1-based column number | ||
}; | ||
} | ||
} | ||
|
||
return null; | ||
}; | ||
|
||
export const getWrongExportedFunctionMarkers = (value: string) => { | ||
const validRegex = /export\s+const\s+main\s*=/g; | ||
const invalidRegex = /export\s+const\s+\S*/g; | ||
const exportRegex = /export\s+const/g; | ||
const validMatch = value.match(validRegex); | ||
const invalidMatch = value.match(invalidRegex); | ||
const exportMatch = value.match(exportRegex); | ||
const markers = []; | ||
|
||
if (!validMatch && !!invalidMatch) { | ||
const coordinates = getSubstringCoordinate(value, invalidMatch[0]); | ||
if (isDefined(coordinates)) { | ||
const endColumn = invalidMatch[0].length + coordinates.column; | ||
markers.push({ | ||
severity: 8, //MarkerSeverity.Error, | ||
message: 'Exported arrow function should be named "main"', | ||
code: 'export const main', | ||
startLineNumber: coordinates.line, | ||
startColumn: coordinates.column, | ||
endLineNumber: 1, | ||
endColumn, | ||
}); | ||
} | ||
} | ||
|
||
if (!exportMatch) { | ||
markers.push({ | ||
severity: 8, //MarkerSeverity.Error, | ||
message: 'An exported "main" arrow function is required.', | ||
code: 'export const main', | ||
startLineNumber: 1, | ||
startColumn: 1, | ||
endLineNumber: 1, | ||
endColumn: 1, | ||
}); | ||
} | ||
|
||
return markers; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters