Skip to content

Commit

Permalink
Update to acorn 7 (#3061)
Browse files Browse the repository at this point in the history
* Update to acorn 7

* Adds test with a meta property other than `import.meta`
  • Loading branch information
lukastaegert committed Aug 16, 2019
1 parent f0b429c commit 8df2bc1
Show file tree
Hide file tree
Showing 11 changed files with 296 additions and 170 deletions.
57 changes: 42 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Expand Up @@ -58,21 +58,21 @@
"homepage": "https://github.com/rollup/rollup",
"dependencies": {
"@types/estree": "0.0.39",
"@types/node": "^12.7.1",
"acorn": "^6.3.0"
"@types/node": "^12.7.2",
"acorn": "^7.0.0"
},
"devDependencies": {
"@types/chokidar": "^2.1.3",
"@types/micromatch": "^3.1.0",
"@types/minimist": "^1.2.0",
"acorn-import-meta": "^1.0.0",
"acorn-jsx": "^5.0.1",
"acorn-walk": "^6.2.0",
"acorn-walk": "^7.0.0",
"buble": "^0.19.8",
"chokidar": "^2.1.6",
"codecov": "^3.5.0",
"console-group": "^0.3.3",
"core-js": "^3.1.4",
"core-js": "^3.2.1",
"date-time": "^3.1.0",
"es5-shim": "^4.5.13",
"es6-shim": "^0.35.5",
Expand Down
14 changes: 7 additions & 7 deletions src/Module.ts
Expand Up @@ -9,8 +9,8 @@ import ExportDefaultDeclaration from './ast/nodes/ExportDefaultDeclaration';
import ExportNamedDeclaration from './ast/nodes/ExportNamedDeclaration';
import FunctionDeclaration from './ast/nodes/FunctionDeclaration';
import Identifier from './ast/nodes/Identifier';
import Import from './ast/nodes/Import';
import ImportDeclaration from './ast/nodes/ImportDeclaration';
import ImportExpression from './ast/nodes/ImportExpression';
import ImportSpecifier from './ast/nodes/ImportSpecifier';
import { nodeConstructors } from './ast/nodes/index';
import Literal from './ast/nodes/Literal';
Expand Down Expand Up @@ -80,7 +80,7 @@ export interface ReexportDescription {
}

export interface AstContext {
addDynamicImport: (node: Import) => void;
addDynamicImport: (node: ImportExpression) => void;
addExport: (
node: ExportAllDeclaration | ExportNamedDeclaration | ExportDefaultDeclaration
) => void;
Expand All @@ -97,7 +97,7 @@ export interface AstContext {
getModuleName: () => string;
getReexports: () => string[];
importDescriptions: { [name: string]: ImportDescription };
includeDynamicImport: (node: Import) => void;
includeDynamicImport: (node: ImportExpression) => void;
includeVariable: (variable: Variable) => void;
isCrossChunkImport: (importDescription: ImportDescription) => boolean;
magicString: MagicString;
Expand Down Expand Up @@ -178,7 +178,7 @@ export default class Module {
dynamicallyImportedBy: Module[] = [];
dynamicDependencies: (Module | ExternalModule)[] = [];
dynamicImports: {
node: Import;
node: ImportExpression;
resolution: Module | ExternalModule | string | null;
}[] = [];
entryPointsHash: Uint8Array = new Uint8Array(10);
Expand Down Expand Up @@ -302,7 +302,7 @@ export default class Module {

getDynamicImportExpressions(): (string | Node)[] {
return this.dynamicImports.map(({ node }) => {
const importArgument = node.parent.arguments[0];
const importArgument = node.source;
if (
importArgument instanceof TemplateLiteral &&
importArgument.quasis.length === 1 &&
Expand Down Expand Up @@ -675,7 +675,7 @@ export default class Module {
this.graph.warn(warning);
}

private addDynamicImport(node: Import) {
private addDynamicImport(node: ImportExpression) {
this.dynamicImports.push({ node, resolution: null });
}

Expand Down Expand Up @@ -819,7 +819,7 @@ export default class Module {
}
}

private includeDynamicImport(node: Import) {
private includeDynamicImport(node: ImportExpression) {
const resolution = (this.dynamicImports.find(dynamicImport => dynamicImport.node === node) as {
resolution: string | Module | ExternalModule | undefined;
}).resolution;
Expand Down
26 changes: 15 additions & 11 deletions src/ast/nodes/Import.ts → src/ast/nodes/ImportExpression.ts
Expand Up @@ -2,27 +2,31 @@ import MagicString from 'magic-string';
import { findFirstOccurrenceOutsideComment, RenderOptions } from '../../utils/renderHelpers';
import { INTEROP_NAMESPACE_VARIABLE } from '../../utils/variableNames';
import NamespaceVariable from '../variables/NamespaceVariable';
import CallExpression from './CallExpression';
import * as NodeType from './NodeType';
import { NodeBase } from './shared/Node';
import { ExpressionNode, IncludeChildren, NodeBase } from './shared/Node';

interface DynamicImportMechanism {
left: string;
right: string;
}

export default class Import extends NodeBase {
parent!: CallExpression;
type!: NodeType.tImport;
source!: ExpressionNode;
type!: NodeType.tImportExpression;

private exportMode: 'none' | 'named' | 'default' | 'auto' = 'auto';
private inlineNamespace?: NamespaceVariable;

include() {
hasEffects(): boolean {
return true;
}

include(includeChildrenRecursively: IncludeChildren) {
if (!this.included) {
this.included = true;
this.context.includeDynamicImport(this);
}
this.source.include(includeChildrenRecursively);
}

initialise() {
Expand All @@ -34,8 +38,8 @@ export default class Import extends NodeBase {
const _ = options.compact ? '' : ' ';
const s = options.compact ? '' : ';';
code.overwrite(
this.parent.start,
this.parent.end,
this.start,
this.end,
`Promise.resolve().then(function${_}()${_}{${_}return ${this.inlineNamespace.getName()}${s}${_}})`
);
return;
Expand All @@ -44,11 +48,11 @@ export default class Import extends NodeBase {
const importMechanism = this.getDynamicImportMechanism(options);
if (importMechanism) {
code.overwrite(
this.parent.start,
findFirstOccurrenceOutsideComment(code.original, '(', this.parent.callee.end) + 1,
this.start,
findFirstOccurrenceOutsideComment(code.original, '(', this.start + 6) + 1,
importMechanism.left
);
code.overwrite(this.parent.end - 1, this.parent.end, importMechanism.right);
code.overwrite(this.end - 1, this.end, importMechanism.right);
}
}

Expand All @@ -57,7 +61,7 @@ export default class Import extends NodeBase {
if (format === 'amd' && resolution.startsWith("'.") && resolution.endsWith(".js'")) {
resolution = resolution.slice(0, -4) + "'";
}
code.overwrite(this.parent.arguments[0].start, this.parent.arguments[0].end, resolution);
code.overwrite(this.source.start, this.source.end, resolution);
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/ast/nodes/MetaProperty.ts
Expand Up @@ -19,6 +19,10 @@ export default class MetaProperty extends NodeBase {

private metaProperty?: string | null;

hasEffects(): boolean {
return false;
}

hasEffectsWhenAccessedAtPath(path: ObjectPathKey[]): boolean {
return path.length > 1;
}
Expand Down
2 changes: 2 additions & 0 deletions src/ast/nodes/NodeType.ts
Expand Up @@ -29,6 +29,7 @@ export type tIdentifier = 'Identifier';
export type tIfStatement = 'IfStatement';
export type tImport = 'Import';
export type tImportDeclaration = 'ImportDeclaration';
export type tImportExpression = 'ImportExpression';
export type tImportDefaultSpecifier = 'ImportDefaultSpecifier';
export type tImportNamespaceSpecifier = 'ImportNamespaceSpecifier';
export type tImportSpecifier = 'ImportSpecifier';
Expand Down Expand Up @@ -93,6 +94,7 @@ export const Identifier: tIdentifier = 'Identifier';
export const IfStatement: tIfStatement = 'IfStatement';
export const Import: tImport = 'Import';
export const ImportDeclaration: tImportDeclaration = 'ImportDeclaration';
export const ImportExpression: tImportExpression = 'ImportExpression';
export const ImportDefaultSpecifier: tImportDefaultSpecifier = 'ImportDefaultSpecifier';
export const ImportNamespaceSpecifier: tImportNamespaceSpecifier = 'ImportNamespaceSpecifier';
export const ImportSpecifier: tImportSpecifier = 'ImportSpecifier';
Expand Down
4 changes: 2 additions & 2 deletions src/ast/nodes/index.ts
Expand Up @@ -26,8 +26,8 @@ import FunctionDeclaration from './FunctionDeclaration';
import FunctionExpression from './FunctionExpression';
import Identifier from './Identifier';
import IfStatement from './IfStatement';
import Import from './Import';
import ImportDeclaration from './ImportDeclaration';
import ImportExpression from './ImportExpression';
import LabeledStatement from './LabeledStatement';
import Literal from './Literal';
import LogicalExpression from './LogicalExpression';
Expand Down Expand Up @@ -91,8 +91,8 @@ export const nodeConstructors: {
FunctionExpression,
Identifier,
IfStatement,
Import,
ImportDeclaration,
ImportExpression,
LabeledStatement,
Literal,
LogicalExpression,
Expand Down
3 changes: 3 additions & 0 deletions test/form/samples/new-target-meta-property/_config.js
@@ -0,0 +1,3 @@
module.exports = {
description: 'supports the new.target meta property'
};
7 changes: 7 additions & 0 deletions test/form/samples/new-target-meta-property/_expected.js
@@ -0,0 +1,7 @@
class Foo {
constructor() {
console.log(new.target.name);
}
}

const x = new Foo();
8 changes: 8 additions & 0 deletions test/form/samples/new-target-meta-property/main.js
@@ -0,0 +1,8 @@
class Foo {
constructor() {
console.log(new.target.name);
const unused = new.target.name;
}
}

const x = new Foo();

0 comments on commit 8df2bc1

Please sign in to comment.