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

define Namespace as module ? #3

Open
NicolasAnquetil opened this issue Jul 20, 2023 · 3 comments
Open

define Namespace as module ? #3

NicolasAnquetil opened this issue Jul 20, 2023 · 3 comments

Comments

@NicolasAnquetil
Copy link

  • In AST-Viewer, Typescript namespaces are called Module (ModuleDeclaration and ModuleBlock)
  • FamixTNamespace by default does not contain anything
  • we want TypescriptNamespaces to contain named entitis (variables, classes, ...)

it seems a good idea to say that FamixTypeScriptNamespace use FamixTModule and all FamixNamedEntities use FamixTDefinedInModule ...

@NicolasAnquetil
Copy link
Author

so Module do exist in typescript and they are not the same thing.
So maybe this idea is not correct, but we need to find a way to define that a namespace can contain entities ...

@fuhrmanator
Copy link
Owner

so Module do exist in typescript and they are not the same thing.

Correct. https://www.typescriptlang.org/docs/handbook/namespaces-and-modules.html explains a lot.

Some big differences:

  • Namespaces are TypeScript-specific, whereas Modules are a JavaScript (Ecmascript 2015) concept.
  • Files become Modules when there have top-level import/export statements. See https://www.typescriptlang.org/docs/handbook/modules.html:

    In TypeScript, just as in ECMAScript 2015, any file containing a top-level import or export is considered a module. Conversely, a file without any top-level import or export declarations is treated as a script whose contents are available in the global scope (and therefore to modules as well).

  • A given Namespace can cross several source files. See https://www.typescriptlang.org/docs/handbook/namespaces.html#multi-file-namespaces for a simple example. This is probably an important difference in the metamodel. A module is only one file.
  • Both namespaces and modules can be nested, and can contain classes, functions, statements, etc.

Note that the AST Viewer will show Module for a namespace. My guess is that it's because of the old (TS 1.5?) syntax where namespaces were called "internal modules". But concretely, one can know if a file is a "module" as defined by the TypScript docs in ts-morph by looking for import/export statements:

function isModule(sourceFile) {
  // Check if the file has any exports or imports to be considered a module.
  if (sourceFile.getExportedDeclarations().size > 0 
        || sourceFile.getImportDeclarations().size > 0) {
    return true;
  }
}

So maybe this idea is not correct, but we need to find a way to define that a namespace can contain entities ...

I think modules and namespaces would be identical in regards to what they can contain.

@fuhrmanator
Copy link
Owner

The importer has support for nested module/namespace (internal module) but the MM still doesn't have it. I commented out the property in scoping_entity.ts to reduce the number of Unknown property messages when importing a model.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants