message: Move script finding to DocumentConverter constructor
diff: @@ -156,6 +156,7 @@ class DocumentConverter {
  module: JsModule;
  analysisConverter: AnalysisConverter;
  document: Document;
  + scriptDocument: Document;
  program: Program;
  currentStatementIndex = 0;
  @@ -169,8 +170,19 @@ class DocumentConverter {
  exports: new Set<string>(),
  importedReferences: new Map<string, Set<string>>(),
  };
  -
  analysisConverter.modules.set(this.jsUrl, this.module);
  +
  + const scripts = this.document.getFeatures({kind: 'js-document'});
  + if (scripts.size === 0) {
  + this.program = J.program([]);
  + } else if (scripts.size > 1) {
  + console.log('multiple scripts');
  + return;
  + } else {
  + this.scriptDocument = scripts.values().next().value;
  + const jsDoc = this.scriptDocument.parsedDocument as ParsedJavaScriptDocument;
  + this.program = jsDoc.ast;
  + }
  }
  addExport(namespaceName: string, name: string) {
  @@ -183,26 +195,10 @@ class DocumentConverter {
  convert() {
  this.convertDependencies();
  - const scripts = this.document.getFeatures({kind: 'js-document'});
  -
  - let scriptDocument: Document|undefined = undefined;
  -
  - if (scripts.size === 0) {
  - this.program = J.program([]);
  - } else if (scripts.size > 1) {
  - console.log('multiple scripts');
  - return;
  - } else {
  - scriptDocument = scripts.values().next().value;
  - const jsDoc = scriptDocument.parsedDocument as ParsedJavaScriptDocument;
  - this.program = jsDoc.ast;
  - }
  -
  this.unwrapModuleBody();
  this.rewriteNamespacedReferences();
  this.addJsImports();
  - if (scriptDocument !== undefined) {
  // Replace namespace assignments with exports
  while (this.currentStatementIndex < this.program.body.length) {
  const statement = this.program.body[this.currentStatementIndex] as Statement;
  @@ -212,7 +208,7 @@ class DocumentConverter {
  const {namespace, value} = exported;
  const namespaceName = namespace.join('.');
  - if (isNamespace(statement, scriptDocument)) {
  + if (this.isNamespace(statement)) {
  this.rewriteNamespace(namespaceName, value, statement);
  } else if (value.type === 'Identifier') {
  // An 'export' of the form:
  @@ -272,7 +268,6 @@ class DocumentConverter {
  }
  this.currentStatementIndex++;
  }
  - }
  this.module.source = escodegen.generate(this.program, {
  comment: true,
  @@ -413,6 +408,20 @@ class DocumentConverter {
  });
  this.addExport(name, '*');
  }
  +
  + isNamespace(node: Node) {
  + if (this.scriptDocument == null) {
  + return false;
  + }
  + const namespaces = this.scriptDocument.getFeatures({kind: 'namespace'});
  + for (const namespace of namespaces) {
  + if (namespace.astNode === node) {
  + return true;
  + }
  + }
  + return false;
  + }
  +
  }
  function getNamespaceExports(namespace: ObjectExpression) {
  @@ -473,16 +482,6 @@ function isDeclaration(node: Node) {
  return type === 'ClassDeclaration';
  }
  -function isNamespace(node: Node, document: Document) {
  - const namespaces = document.getFeatures({kind: 'namespace'});
  - for (const namespace of namespaces) {
  - if (namespace.astNode === node) {
  - return true;
  - }
  - }
  - return false;
  -}
  -
  /**
  * If a statement appears to be an export, returns the exported declaration.
  *
  