Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Outdated documentation to help to navigate the source code #1461

Closed
alessandro308 opened this issue Aug 1, 2024 · 4 comments
Closed

Comments

@alessandro308
Copy link

I know there is an hack, as described in the code

throw `
Hey there, it looks like you're trying to import the danger module. Turns out
that the code you write in a Dangerfile.js is actually a bit of a sneaky hack. 

When running Danger, the import or require for Danger is removed before the code
is evaluated. Instead all of the imports are added to the global runtime, so if
you are importing Danger to use one of it's functions - you should instead just
use the global object for the root DSL elements.

There is a spectrum thread for discussion here:
  - https://spectrum.chat/?t=0a005b56-31ec-4919-9a28-ced623949d4d
`

but the link is outdated and I cannot figure out where the global functions are defined (e.g. warn(..) ).

Any help?

@fbartho
Copy link
Member

fbartho commented Aug 1, 2024

@alessandro308 I had some relevant comments & questions I posed in #1459

@alessandro308
Copy link
Author

I don't have this error. I'm trying to read the code to understand if another behaviour I experimented is right or not.
Reading the code I've found that snippet that is referencing an unreachable url.

I need an help to understand where I can found the implementation of the warn function, the global function exposed there

@fbartho
Copy link
Member

fbartho commented Aug 1, 2024

@alessandro308 here you go:

/** Creates a Danger context, this provides all of the global functions
* which are available to the Danger eval runtime.
*
* @param {DangerDSLType} dsl The DSL which is turned into `danger`
* @returns {DangerContext} a DangerContext-like API
*/
export function contextForDanger(dsl: DangerDSLType): DangerContext {
const results: DangerRuntimeContainer = {
fails: [],
warnings: [],
messages: [],
markdowns: [],
scheduled: [],
}
const schedule = (fn: any) => results.scheduled && results.scheduled.push(fn)
const fail = (message: MarkdownString, file?: string, line?: number) => results.fails.push({ message, file, line })
const warn = (message: MarkdownString, file?: string, line?: number) => results.warnings.push({ message, file, line })
const message = (
message: MarkdownString,
opts?: string | { file?: string; line?: number; icon?: MarkdownString },
lineArg?: number
) => {
let file: string | undefined
let line: number | undefined
let icon: MarkdownString | undefined
if (typeof opts === "string") {
file = opts
line = lineArg
} else if (typeof opts === "object") {
;({ file, line, icon } = opts)
}
results.messages.push({ message, file, line, icon })
}
const markdown = (message: MarkdownString, file?: string, line?: number) =>
results.markdowns.push({ message, file, line })
return {
schedule,
fail,
warn,
message,
markdown,
results,
danger: dsl,
peril: {} as any,
}
}

@alessandro308
Copy link
Author

It was easy! Thanks! I search it for a while!

@fbartho fbartho closed this as completed Aug 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants