"Modules that use an anonymous define() call must be loaded with a require() call" - sapui5

In the walkthrough step 7: JSON Model example, the app apparently works as documented but I see the following error in the console:
Error: Modules that use an anonymous define() call must be loaded with a require() call; they must not be executed via script tag or nested into other modules.
The only other instance of this message that I could find seems, to my untrained eye, to deal with a wholly different scenario.
I've tried both Firefox and Chromium, CDN hosting vs local hosting, two different UI5 versions (1.77.0 and 1.79.0), both minified and plain, so I'd suppose this is really something in the code itself.
What could it be? Also, is it something I can safely ignore and why?

Anonymous define
Calling sap.ui.define([...],...) defines a module anonymously because the 1st argument is not a string (module name) but a list of the module's dependencies. If the module name is omitted, the framework automatically determines it based on how the module script was referenced.
Use anonymous sap.ui.define once at top-level of the JS file content, not multiple times.
Replace sap.ui.define with sap.ui.require when simply requiring existing modules.
Cf. my comment at https://github.com/SAP/openui5/issues/2203#issuecomment-420918457.
Named module define
The 1st argument in sap.ui.define("MyModule",[...] ,...) defines the name of the module manually which must be passed when:
Defining a nested module within an existing module definition in a single JS file content.
Defining a module which was initiated by a <script> tag from HTML.
The walkthrough is fixed with SAP/openui5#6302b8f and SAP/openui5-docs#43 accordingly.

Related

Calling function in swift application from dependent module

I have a Swift application that uses a module, and I need to call a global function that is in the application from the module, is this possible?
To perhaps explain a little better, this is a test app structure:
CallbackTestApp contains a function foo(), I would like to call it from Module1 or File, will swift allow this?
edit #1
More details have been requested on what is the background of my issue, hopefully, this will not turn out to be an XY situation.
There's a tool developed by my company that process the application source* code and in some places add function call (ignore the why etc, have to be generic here.). Those function calls are exactly to foo() which then does some magic (btw, no return value and no arguments are allowed), if the application does not use modules or if modules are excluded from the processing then all is fine (Linker does not complain that the function is not defined), if there are modules then nothing works since I did not found a way to inject foo() (yet).
*Not exactly the source code, actually the bitcode is processed, the tool get the source, use llvm toolchain to generate bitcode, do some more magic and then add the call to foo() by generating it's mangled name and adding a swiftcall
Not actually sure those additional details will help.

How to access the ICElements of local variables(variables inside function) and variables in header file?

Objective is to access the elements of C-file in eclipse to check customized naming rules for C-elements(global variable, local variable, function declarations).
Tried to access the C-file elements as mentioned below. In this case, only able to access global variables and function names in the .c file.
How local variables(variables inside functions) & variables in included header files can be accessed?
ITranslationUnit tu = CUIPlugin.getDefault().getWorkingCopyManager().getWorkingCopy(input);
ICElement[] ele= src.getChildren();
Local variables
ICElement is mostly used for representing code elements in CDT's various views, such as the Outline View or Type Hierarchy. As such, local variables (which do not appear in these views) do not have an ICElement representation.
For code analysis use cases like this, it's probably better to use the AST API. The AST is a detailed representation of the entire code in a file. It can be accessed via ITranslationUnit.getAST(). You can then use an ASTVisitor to traverse the AST and visit any declarations you like and check their names.
Variables in included header files
There are two sub-categories here: header files inside the project directory, and header files outside the project directory.
Header files inside the project directory have their own ITranslationUnit, and you can use either the ICElement API or the AST API to analyze them with that ITranslationUnit as a starting point. Note that a file does not need to be open in an editor to obtain an ITranslationUnit for it. You can traverse all of the files in the project with something like ICElementVisitor, with the ICProject as a stating point.
Header files outside the project directory do not have an ITranslationUnit, and there is no straightforward way to obtain an AST for them. However, assuming your project's indexer is enabled, the indexer does create ASTs for them and store information from those ASTs in the project's index, which you could examine. There are index APIs that can be used to traverse the index; some relevant ones are IIndexManager.getIndex(ICProject), IIndex.getAllFiles(), and IIndexFile.findNames().
Edit: Additional Tips
1) How to differentiate between function declarations and simple declarations.
I can think of two ways:
Syntactically, based on the structure of the AST. For function definitions, the type of the declaration node will be IASTFunctionDefintion. For variable declarations, it will be IASTSimpleDeclaration, with the decl-specifier being IASTSimpleDeclSpecifier or IASTNamedTypeSpecifier (you additionally want to check that the declarator is not an IASTFunctionDeclarator, to filter out function declarations that are not definitions).
Semantically. If you find the IASTName for the declaration, you can call IASTName.resolveBinding(), and check whether the returned binding is an IFunction or an IVariable.
2) How to get the return type of a function and the variable type?
For these tasks, you need to get the binding. A variable's type can be queried by IVariable.getType(), and a function's return type via IFunction.getType().getReturnType().
3) Is there a way to get an ICElement from an IASTSimpleDeclaration?
There isn't a simple way that I know of. However, you shouldn't need to - if you're traversing the AST, all the information you could want can be found in the AST.

Perl shallow syntax check? ie. do not check syntax of imports

How can I perform a "shallow" syntax check on perl files. The standard perl -c is useful but it checks the syntax of imports. This is sometimes nice but not great when you work in a code repository and push to a running environment and you have a function defined in the repository but not yet pushed to the running environment. It fails checking a function because the imports reference system paths (ie. use Custom::Project::Lib qw(foo bar baz)).
It can't practically be done, because imports have the ability to influence the parsing of the code that follows. For example use strict makes it so that barewords aren't parsed as strings (and changes the rules for how variable names can be used), use constant causes constant subs to be defined, and use Try::Tiny changes the parse of expressions involving try, catch, or finally (by giving them & prototypes). More generally, any module that exports anything into the caller's namespace can influence parsing because the perl parser resolves ambiguity in different ways when a name refers to an existing subroutine than when it doesn't.
There are two problems with this:
How to not fail -c if the required modules are missing?
There are two solutions:
A. Add a fake/stub module in production
B. In all your modules, use a special catch-all #INC subroutine entry (using subs in #INC is explained here). This obviously has a problem of having the module NOT fail in real production runtime if the libraries are missing - DoublePlusNotGood in my book.
Even if you could somehow skip failing on missing modules, you would STILL fail on any use of the identifiers imported from the missing module or used explicitly from that module's namespace.
The only realistic solution to this is to go back to #1a and use a fake stub module, but this time one that has a declared and (as needed) exported identifier for every public interface. E.g. do-nothing subs or dummy variables.
However, even that will fail for some advanced modules that dynamically determine what to create in their own namespace and what to export in runtime (and the caller code could dynamically determine which subs to call - heck, sometimes which modules to import).
But this approach would work just fine for normal "Java/C-like" OO or procedural code that only calls statically named predefined public subs, methods and accesses exported variables.
I would suggest that it's better to include your code repository in your syntax check. perl -I/path/to/working/code/repo/local_perl/ -c or set PERL5LIB=/path/to/working/code/repo/local_perl/ prior to running perl -c. Either option should allow you to check against your working code, assuming you have it in a directory structure similar to your live code.
I guess you could make stubs for the missing libraries in your home folder.
Have you looked into PPI? I think it does follow imports, however it could perhaps be more easily modified to guess what looks like a function name.

How do I associate a CoffeeScript file with a view?

Just installed rails 3.1 rc1 and am trying to grok the best way to manage javascript with the new asset pipeline
By default all coffeescript is compiled into a single application.js file, this is a good thing.
Each seperate coffee script file is appended to the js file and wrapped in an anonymous function which is executed via the call method
A common scenario would be to use some jquery to turn various forms into ajax forms, update UI, etc...
Many of these scripts will be specific to a controller or action, I am trying to grok the 'conventional' way to handle this,
since everything is wrapped in an anonymous function how do I only execute just
the code for a particular controller / action, by default all of the anonymous functions are being executed
I did play around with some hacks where I load the controller and action name into js variables and then in
coffeescript check those to conditionally run code, I don't like that very much
my initial thought was that each coffee file would contain a js namespace/object and I would call the specific ones from the view,
going to spike this using the default_bare = true configuration
see How can I use option "--bare" in Rails 3.1 for CoffeeScript?
EDIT
Looking around some more: this looks like it might be the correct approach - "Can't find variable" error with Rails 3.1 and Coffeescript
There are two common approaches:
Make behavior conditional on the presence of a particular element. For instance, code to run a signup sheet should be prefaced with something like
if $('#signup').length > 0
Make behavior conditional on a class on the body element. You can set the body class using ERB. This is often desirable for stylesheets as well. The code would be something like
if $('body').hasClass 'user'
gistyle is a simple gem that helps you running action-specific javascript codes.
By following its setup, you set some data attributes in your body element, representing the current controller and action names. Then it will only call that action when the corresponding view is loaded.

GWTTestcase accessing javascript object defined in an external javascript file fails

I have defined a GWT module that includes an external javascript file using tag. I have written a GWTTestCase that returns the above described module's name. When my testcase accesses a javascript object I see the following exception
Caused by: com.google.gwt.core.client.JavaScriptException: (null): null
Any idea on how to fix this?
Am I right in assuming that the scripts included in the gwt module definition file will be available when executing the GWTTestCase?
I have fixed it myself. Apparently, when accessing such objects it should be referenced using $wnd variable.
Example: test.js defined object test. In order to access it from GWT one should use, $wnd.test
Hope this answers saves somebody else' time.