Skip to content

Commit

Permalink
Include code and AST in module info
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Oct 8, 2020
1 parent 8514f10 commit dfd9dcb
Show file tree
Hide file tree
Showing 10 changed files with 1,185 additions and 98 deletions.
2 changes: 2 additions & 0 deletions src/Graph.ts
Expand Up @@ -137,6 +137,8 @@ export default class Graph {
const foundModule = this.modulesById.get(moduleId);
if (!foundModule) return null;
return {
ast: (foundModule as Module).ast?.esTreeNode || null,
code: foundModule instanceof Module ? foundModule.code : null,
get dynamicallyImportedIds() {
if (foundModule instanceof Module) {
const dynamicallyImportedIds: string[] = [];
Expand Down
26 changes: 13 additions & 13 deletions src/Module.ts
Expand Up @@ -119,7 +119,7 @@ function tryParse(
acornOptions: acorn.Options
): acorn.Node {
try {
return Parser.parse(module.code, {
return Parser.parse(module.code!, {
...acornOptions,
onComment: (block: boolean, text: string, start: number, end: number) =>
module.comments.push({ block, text, start, end })
Expand Down Expand Up @@ -184,9 +184,10 @@ function getVariableForExportNameRecursive(
}

export default class Module {
ast: Program | null = null;
chunkFileNames = new Set<string>();
chunkName: string | null = null;
code!: string;
code: string | null = null;
comments: CommentDescription[] = [];
dependencies = new Set<Module | ExternalModule>();
dynamicDependencies = new Set<Module | ExternalModule>();
Expand Down Expand Up @@ -225,7 +226,6 @@ export default class Module {

private allExportNames: Set<string> | null = null;
private alwaysRemovedCode!: [number, number][];
private ast!: Program;
private astContext!: AstContext;
private readonly context: string;
private customTransformCache!: boolean;
Expand Down Expand Up @@ -260,7 +260,7 @@ export default class Module {
}

bindReferences() {
this.ast.bind();
this.ast!.bind();
}

error(props: RollupError, pos: number): never {
Expand Down Expand Up @@ -527,13 +527,13 @@ export default class Module {
hasEffects() {
return (
this.moduleSideEffects === 'no-treeshake' ||
(this.ast.included && this.ast.hasEffects(createHasEffectsContext()))
(this.ast!.included && this.ast!.hasEffects(createHasEffectsContext()))
);
}

include(): void {
const context = createInclusionContext();
if (this.ast.shouldBeIncluded(context)) this.ast.include(context, false);
if (this.ast!.shouldBeIncluded(context)) this.ast!.include(context, false);
}

includeAllExports(includeNamespaceMembers: boolean) {
Expand Down Expand Up @@ -571,11 +571,11 @@ export default class Module {
}

includeAllInBundle() {
this.ast.include(createInclusionContext(), true);
this.ast!.include(createInclusionContext(), true);
}

isIncluded() {
return this.ast.included || this.namespace.included;
return this.ast!.included || this.namespace.included;
}

linkImports() {
Expand Down Expand Up @@ -607,7 +607,7 @@ export default class Module {

render(options: RenderOptions): MagicString {
const magicString = this.magicString.clone();
this.ast.render(magicString, options);
this.ast!.render(magicString, options);
this.usesTopLevelAwait = this.astContext.usesTopLevelAwait;
return magicString;
}
Expand Down Expand Up @@ -708,8 +708,8 @@ export default class Module {
toJSON(): ModuleJSON {
return {
alwaysRemovedCode: this.alwaysRemovedCode,
ast: this.ast.esTreeNode,
code: this.code,
ast: this.ast!.esTreeNode,
code: this.code!,
customTransformCache: this.customTransformCache,
dependencies: Array.from(this.dependencies, getId),
id: this.id,
Expand Down Expand Up @@ -888,7 +888,7 @@ export default class Module {
props.id = this.id;
props.pos = pos;
let code = this.code;
let { column, line } = locate(code, pos, { offsetLine: 1 });
let { column, line } = locate(code!, pos, { offsetLine: 1 });
try {
({ column, line } = getOriginalLocation(this.sourcemapChain, { column, line }));
code = this.originalCode;
Expand All @@ -905,7 +905,7 @@ export default class Module {
pos
});
}
augmentCodeLocation(props, { column, line }, code, this.id);
augmentCodeLocation(props, { column, line }, code!, this.id);
}

private addModulesToImportDescriptions(importDescription: {
Expand Down
3 changes: 3 additions & 0 deletions src/rollup/types.d.ts
Expand Up @@ -158,6 +158,8 @@ export type EmitChunk = (id: string, options?: { name?: string }) => string;
export type EmitFile = (emittedFile: EmittedFile) => string;

interface ModuleInfo {
ast: AcornNode | null;
code: string | null;
dynamicallyImportedIds: readonly string[];
dynamicImporters: readonly string[];
hasModuleSideEffects: boolean | 'no-treeshake';
Expand Down Expand Up @@ -347,6 +349,7 @@ export interface PluginHooks extends OutputPluginHooks {
buildStart: (this: PluginContext, options: NormalizedInputOptions) => Promise<void> | void;
load: LoadHook;
options: (this: MinimalPluginContext, options: InputOptions) => InputOptions | null | undefined;
// TODO Lukas parsedModule hook
resolveDynamicImport: ResolveDynamicImportHook;
resolveId: ResolveIdHook;
transform: TransformHook;
Expand Down
Expand Up @@ -24,7 +24,52 @@ module.exports = {
});
},
buildEnd() {
assert.deepStrictEqual(this.getModuleInfo(ID_MAIN), {
assert.deepStrictEqual(JSON.parse(JSON.stringify(this.getModuleInfo(ID_MAIN))), {
ast: {
type: 'Program',
start: 0,
end: 51,
body: [
{
type: 'ImportDeclaration',
start: 0,
end: 30,
specifiers: [
{
type: 'ImportSpecifier',
start: 9,
end: 14,
imported: { type: 'Identifier', start: 9, end: 14, name: 'value' },
local: { type: 'Identifier', start: 9, end: 14, name: 'value' }
}
],
source: { type: 'Literal', start: 22, end: 29, value: './lib', raw: "'./lib'" }
},
{
type: 'ExpressionStatement',
start: 31,
end: 50,
expression: {
type: 'CallExpression',
start: 31,
end: 49,
callee: {
type: 'MemberExpression',
start: 31,
end: 42,
object: { type: 'Identifier', start: 31, end: 38, name: 'console' },
property: { type: 'Identifier', start: 39, end: 42, name: 'log' },
computed: false,
optional: false
},
arguments: [{ type: 'Identifier', start: 43, end: 48, name: 'value' }],
optional: false
}
}
],
sourceType: 'module'
},
code: "import { value } from './lib';\nconsole.log(value);\n",
dynamicallyImportedIds: [],
dynamicImporters: [],
hasModuleSideEffects: true,
Expand All @@ -37,7 +82,52 @@ module.exports = {
isExternal: false,
meta: {}
});
assert.deepStrictEqual(this.getModuleInfo(ID_DEP), {
assert.deepStrictEqual(JSON.parse(JSON.stringify(this.getModuleInfo(ID_DEP))), {
ast: {
type: 'Program',
start: 0,
end: 51,
body: [
{
type: 'ImportDeclaration',
start: 0,
end: 30,
specifiers: [
{
type: 'ImportSpecifier',
start: 9,
end: 14,
imported: { type: 'Identifier', start: 9, end: 14, name: 'value' },
local: { type: 'Identifier', start: 9, end: 14, name: 'value' }
}
],
source: { type: 'Literal', start: 22, end: 29, value: './lib', raw: "'./lib'" }
},
{
type: 'ExpressionStatement',
start: 31,
end: 50,
expression: {
type: 'CallExpression',
start: 31,
end: 49,
callee: {
type: 'MemberExpression',
start: 31,
end: 42,
object: { type: 'Identifier', start: 31, end: 38, name: 'console' },
property: { type: 'Identifier', start: 39, end: 42, name: 'log' },
computed: false,
optional: false
},
arguments: [{ type: 'Identifier', start: 43, end: 48, name: 'value' }],
optional: false
}
}
],
sourceType: 'module'
},
code: "import { value } from './lib';\nconsole.log(value);\n",
dynamicallyImportedIds: [],
dynamicImporters: [],
hasModuleSideEffects: true,
Expand Down
Expand Up @@ -20,7 +20,52 @@ module.exports = {
});
},
buildEnd() {
assert.deepStrictEqual(this.getModuleInfo(ID_MAIN), {
assert.deepStrictEqual(JSON.parse(JSON.stringify(this.getModuleInfo(ID_MAIN))), {
ast: {
type: 'Program',
start: 0,
end: 51,
body: [
{
type: 'ImportDeclaration',
start: 0,
end: 30,
specifiers: [
{
type: 'ImportSpecifier',
start: 9,
end: 14,
imported: { type: 'Identifier', start: 9, end: 14, name: 'value' },
local: { type: 'Identifier', start: 9, end: 14, name: 'value' }
}
],
source: { type: 'Literal', start: 22, end: 29, value: './lib', raw: "'./lib'" }
},
{
type: 'ExpressionStatement',
start: 31,
end: 50,
expression: {
type: 'CallExpression',
start: 31,
end: 49,
callee: {
type: 'MemberExpression',
start: 31,
end: 42,
object: { type: 'Identifier', start: 31, end: 38, name: 'console' },
property: { type: 'Identifier', start: 39, end: 42, name: 'log' },
computed: false,
optional: false
},
arguments: [{ type: 'Identifier', start: 43, end: 48, name: 'value' }],
optional: false
}
}
],
sourceType: 'module'
},
code: "import { value } from './lib';\nconsole.log(value);\n",
dynamicallyImportedIds: [],
dynamicImporters: [],
hasModuleSideEffects: true,
Expand All @@ -33,7 +78,52 @@ module.exports = {
isExternal: false,
meta: {}
});
assert.deepStrictEqual(this.getModuleInfo(ID_DEP), {
assert.deepStrictEqual(JSON.parse(JSON.stringify(this.getModuleInfo(ID_DEP))), {
ast: {
type: 'Program',
start: 0,
end: 51,
body: [
{
type: 'ImportDeclaration',
start: 0,
end: 30,
specifiers: [
{
type: 'ImportSpecifier',
start: 9,
end: 14,
imported: { type: 'Identifier', start: 9, end: 14, name: 'value' },
local: { type: 'Identifier', start: 9, end: 14, name: 'value' }
}
],
source: { type: 'Literal', start: 22, end: 29, value: './lib', raw: "'./lib'" }
},
{
type: 'ExpressionStatement',
start: 31,
end: 50,
expression: {
type: 'CallExpression',
start: 31,
end: 49,
callee: {
type: 'MemberExpression',
start: 31,
end: 42,
object: { type: 'Identifier', start: 31, end: 38, name: 'console' },
property: { type: 'Identifier', start: 39, end: 42, name: 'log' },
computed: false,
optional: false
},
arguments: [{ type: 'Identifier', start: 43, end: 48, name: 'value' }],
optional: false
}
}
],
sourceType: 'module'
},
code: "import { value } from './lib';\nconsole.log(value);\n",
dynamicallyImportedIds: [],
dynamicImporters: [],
hasModuleSideEffects: true,
Expand Down

0 comments on commit dfd9dcb

Please sign in to comment.