Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[labs/analyzer] Factor Module, Classes, Variables into separate modul…
…es (#2994) * [analyzer] Factor Module, Classes, Variables into separate modules * Rename "standard" to "javascript" * Remove isExported (not used yet) * fixup! Rename "standard" to "javascript" * Fix error message for package.json * Normalize source filename for Windows
- Loading branch information
1 parent
9472263
commit 7d8e2a3
Showing
6 changed files
with
194 additions
and
133 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
--- | ||
--- |
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
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,25 @@ | ||
/** | ||
* @license | ||
* Copyright 2022 Google LLC | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
*/ | ||
|
||
/** | ||
* @fileoverview | ||
* | ||
* Utilities for working with classes | ||
*/ | ||
|
||
import ts from 'typescript'; | ||
import {ClassDeclaration} from '../model.js'; | ||
import {ProgramContext} from '../program-context.js'; | ||
|
||
export const getClassDeclaration = ( | ||
declaration: ts.ClassDeclaration, | ||
_programContext: ProgramContext | ||
): ClassDeclaration => { | ||
return new ClassDeclaration({ | ||
name: declaration.name?.text, | ||
node: declaration, | ||
}); | ||
}; |
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,68 @@ | ||
/** | ||
* @license | ||
* Copyright 2022 Google LLC | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
*/ | ||
|
||
import ts from 'typescript'; | ||
import {Module} from '../model.js'; | ||
import { | ||
isLitElement, | ||
getLitElementDeclaration, | ||
} from '../lit-element/lit-element.js'; | ||
import * as path from 'path'; | ||
import {getClassDeclaration} from './classes.js'; | ||
import {getVariableDeclarations} from './variables.js'; | ||
import {ProgramContext} from '../program-context.js'; | ||
import {AbsolutePath, absoluteToPackage} from '../paths.js'; | ||
|
||
export const getModule = ( | ||
sourceFile: ts.SourceFile, | ||
programContext: ProgramContext | ||
) => { | ||
const sourcePath = absoluteToPackage( | ||
path.normalize(sourceFile.fileName) as AbsolutePath, | ||
programContext.packageRoot | ||
); | ||
const fullSourcePath = path.join(programContext.packageRoot, sourcePath); | ||
const jsPath = ts | ||
.getOutputFileNames(programContext.commandLine, fullSourcePath, false) | ||
.filter((f) => f.endsWith('.js'))[0]; | ||
// TODO(kschaaf): this could happen if someone imported only a .d.ts file; | ||
// we might need to handle this differently | ||
if (jsPath === undefined) { | ||
throw new Error(`Could not determine output filename for '${sourcePath}'`); | ||
} | ||
|
||
const module = new Module({ | ||
sourcePath, | ||
// The jsPath appears to come out of the ts API with unix | ||
// separators; since sourcePath uses OS separators, normalize | ||
// this so that all our model paths are OS-native | ||
jsPath: absoluteToPackage( | ||
path.normalize(jsPath) as AbsolutePath, | ||
programContext.packageRoot as AbsolutePath | ||
), | ||
sourceFile, | ||
}); | ||
|
||
programContext.currentModule = module; | ||
|
||
for (const statement of sourceFile.statements) { | ||
if (ts.isClassDeclaration(statement)) { | ||
module.declarations.push( | ||
isLitElement(statement, programContext) | ||
? getLitElementDeclaration(statement, programContext) | ||
: getClassDeclaration(statement, programContext) | ||
); | ||
} else if (ts.isVariableStatement(statement)) { | ||
module.declarations.push( | ||
...statement.declarationList.declarations | ||
.map((dec) => getVariableDeclarations(dec, dec.name, programContext)) | ||
.flat() | ||
); | ||
} | ||
} | ||
programContext.currentModule = undefined; | ||
return module; | ||
}; |
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,54 @@ | ||
/** | ||
* @license | ||
* Copyright 2022 Google LLC | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
*/ | ||
|
||
/** | ||
* @fileoverview | ||
* | ||
* Utilities for working with classes | ||
*/ | ||
|
||
import ts from 'typescript'; | ||
import {VariableDeclaration} from '../model.js'; | ||
import {ProgramContext} from '../program-context.js'; | ||
import {DiagnosticsError} from '../errors.js'; | ||
|
||
type VariableName = | ||
| ts.Identifier | ||
| ts.ObjectBindingPattern | ||
| ts.ArrayBindingPattern; | ||
|
||
export const getVariableDeclarations = ( | ||
dec: ts.VariableDeclaration, | ||
name: VariableName, | ||
programContext: ProgramContext | ||
): VariableDeclaration[] => { | ||
if (ts.isIdentifier(name)) { | ||
return [ | ||
new VariableDeclaration({ | ||
name: name.text, | ||
node: dec, | ||
type: programContext.getTypeForNode(name), | ||
}), | ||
]; | ||
} else if ( | ||
// Recurse into the elements of an array/object destructuring variable | ||
// declaration to find the identifiers | ||
ts.isObjectBindingPattern(name) || | ||
ts.isArrayBindingPattern(name) | ||
) { | ||
const els = name.elements.filter((el) => | ||
ts.isBindingElement(el) | ||
) as ts.BindingElement[]; | ||
return els | ||
.map((el) => getVariableDeclarations(dec, el.name, programContext)) | ||
.flat(); | ||
} else { | ||
throw new DiagnosticsError( | ||
dec, | ||
`Expected declaration name to either be an Identifier or a BindingPattern` | ||
); | ||
} | ||
}; |
Oops, something went wrong.