Skip to content

Commit 44feaaf

Browse files
Rename transformers to chunk-transformers (ampproject#284)
* Rename transformers to chunk-transformers * Type information for SourceTransform * Remove self = this references * Support Source and Chunk Transforms * dynamic test was missing advanced mode: * Start of mangling imports * Begin effort to repair sourcemaps using remapper * Fix all tests, flag source transforms as unstable and decommit * Create seperate file for sourcemapping * Move from travis to github actions * Disable code coverage temporarily * Dont run tests on Windows, need to normalize cr versus lr
1 parent e29d0cf commit 44feaaf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+1423
-558
lines changed
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: Tests
2+
3+
on: [pull_request]
4+
5+
jobs:
6+
build:
7+
strategy:
8+
matrix:
9+
platform: [ubuntu-latest]
10+
node-version: [12.x]
11+
runs-on: ${{ matrix.platform }}
12+
steps:
13+
- uses: actions/checkout@v1
14+
- name: Use Node.js ${{ matrix.node-version }}
15+
uses: actions/setup-node@v1
16+
with:
17+
node-version: ${{ matrix.node-version }}
18+
- run: yarn install
19+
- run: yarn pretest
20+
- run: yarn c8 ava
21+
- run: yarn c8 report --reporter=lcovonly
22+
- run: yarn codecov

.github/workflows/test.yml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: Tests
2+
3+
on: [pull_request]
4+
5+
jobs:
6+
build:
7+
strategy:
8+
matrix:
9+
platform: [ubuntu-latest, macos-latest]
10+
node-version: [12.x]
11+
runs-on: ${{ matrix.platform }}
12+
steps:
13+
- uses: actions/checkout@v1
14+
- name: Use Node.js ${{ matrix.node-version }}
15+
uses: actions/setup-node@v1
16+
with:
17+
node-version: ${{ matrix.node-version }}
18+
- run: yarn install
19+
- run: yarn test

.travis.yml

-19
This file was deleted.

package.json

+5-6
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"precoverage": "yarn pretest && c8 ava",
2626
"coverage": "c8 report --reporter=html",
2727
"postcoverage": "sirv coverage/",
28-
"build": "rimraf dist transpile && tsc -p tsconfig.json & wait",
28+
"build": "rimraf dist transpile transpile-tests && tsc -p tsconfig.json & wait",
2929
"postbuild": "rollup -c",
3030
"release": "np",
3131
"prepublishOnly": "npm-run-all build"
@@ -34,8 +34,9 @@
3434
"rollup": ">=1.27"
3535
},
3636
"dependencies": {
37+
"@ampproject/remapping": "0.2.0",
38+
"@kristoferbaxter/estree-walker": "2.0.0",
3739
"acorn": "7.1.0",
38-
"acorn-dynamic-import": "4.0.0",
3940
"acorn-walk": "7.0.0",
4041
"google-closure-compiler": "20200112.0.0",
4142
"magic-string": "0.25.6",
@@ -62,12 +63,10 @@
6263
},
6364
"lint-staged": {
6465
"*.ts": [
65-
"prettier --config .prettierrc --write",
66-
"git add"
66+
"prettier --config .prettierrc --write"
6767
],
6868
"*.test.js": [
69-
"prettier --config .prettierrc --write",
70-
"git add"
69+
"prettier --config .prettierrc --write"
7170
]
7271
},
7372
"husky": {

src/acorn-walk.d.ts

+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/**
2+
* Copyright 2020 The AMP HTML Authors. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS-IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
declare module 'acorn-walk' {
18+
type NodeType = import('estree').Node['type'];
19+
type DiscriminateUnion<T, K extends keyof T, V extends T[K] = T[K]> = T extends Record<K, V>
20+
? T
21+
: never;
22+
type NarrowNode<K extends NodeType> = DiscriminateUnion<import('estree').Node, 'type', K>;
23+
24+
type FullWalkerCallback<TState> = (
25+
node: import('estree').Node,
26+
state: TState,
27+
type: NodeType,
28+
) => void;
29+
30+
type FullAncestorWalkerCallback<TState> = (
31+
node: import('estree').Node,
32+
state: TState | import('estree').Node[],
33+
ancestors: import('estree').Node[],
34+
type: NodeType,
35+
) => void;
36+
type WalkerCallback<TState> = (node: import('estree').Node, state: TState) => void;
37+
38+
type SimpleWalkerFn<K extends NodeType, TState> = (node: NarrowNode<K>, state: TState) => void;
39+
40+
type AncestorWalkerFn<K extends NodeType, TState> = (
41+
node: NarrowNode<K>,
42+
state: TState | import('estree').Node[],
43+
ancestors: import('estree').Node[],
44+
) => void;
45+
46+
type RecursiveWalkerFn<K extends NodeType, TState> = (
47+
node: NarrowNode<K>,
48+
state: TState,
49+
callback: WalkerCallback<TState>,
50+
) => void;
51+
52+
type SimpleVisitors<Types extends NodeType, TState> = {
53+
[Type in Types]: SimpleWalkerFn<Type, TState>;
54+
};
55+
56+
type AncestorVisitors<Types extends NodeType, TState> = {
57+
[Type in Types]: AncestorWalkerFn<Type, TState>;
58+
};
59+
60+
type RecursiveVisitors<Types extends NodeType, TState> = {
61+
[Type in Types]: RecursiveWalkerFn<Type, TState>;
62+
};
63+
64+
type FindPredicate = (type: NodeType, node: import('estree').Node) => boolean;
65+
66+
interface Found<Type extends NodeType, TState> {
67+
node: NarrowNode<Type>;
68+
state: TState;
69+
}
70+
71+
export function simple<TState, K extends NodeType>(
72+
node: import('estree').Node,
73+
visitors: SimpleVisitors<K, TState>,
74+
base?: RecursiveVisitors<NodeType, TState>,
75+
state?: TState,
76+
): void;
77+
78+
export function ancestor<TState, K extends NodeType>(
79+
node: import('estree').Node,
80+
visitors: AncestorVisitors<K, TState>,
81+
base?: RecursiveVisitors<NodeType, TState>,
82+
state?: TState,
83+
): void;
84+
85+
export function recursive<TState, K extends NodeType>(
86+
node: import('estree').Node,
87+
state: TState,
88+
functions: RecursiveVisitors<K, TState>,
89+
base?: RecursiveVisitors<NodeType, TState>,
90+
): void;
91+
92+
export function full<TState>(
93+
node: import('estree').Node,
94+
callback: FullWalkerCallback<TState>,
95+
base?: RecursiveVisitors<NodeType, TState>,
96+
state?: TState,
97+
): void;
98+
99+
export function fullAncestor<TState>(
100+
node: import('estree').Node,
101+
callback: FullAncestorWalkerCallback<TState>,
102+
base?: RecursiveVisitors<NodeType, TState>,
103+
state?: TState,
104+
): void;
105+
106+
export function make<TState, K extends NodeType>(
107+
functions: RecursiveVisitors<K, TState>,
108+
base?: RecursiveVisitors<NodeType, TState>,
109+
): RecursiveVisitors<NodeType, TState>;
110+
111+
export function findNodeAt<TState, K extends NodeType>(
112+
node: import('estree').Node,
113+
start: number | undefined,
114+
end: number | undefined,
115+
type: K,
116+
base?: RecursiveVisitors<NodeType, TState>,
117+
state?: TState,
118+
): Found<K, TState> | undefined;
119+
120+
export function findNodeAt<TState>(
121+
node: import('estree').Node,
122+
start: number | undefined,
123+
end: number | undefined,
124+
type?: FindPredicate,
125+
base?: RecursiveVisitors<NodeType, TState>,
126+
state?: TState,
127+
): Found<NodeType, TState> | undefined;
128+
129+
export const findNodeAround: typeof findNodeAt;
130+
131+
export const findNodeAfter: typeof findNodeAt;
132+
}

src/acorn.ts

+76-16
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,92 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { Program } from 'estree';
18-
import { DYNAMIC_IMPORT_DECLARATION } from './types';
19-
const acorn = require('acorn');
17+
import {
18+
Program,
19+
BaseNode,
20+
Identifier,
21+
ImportDeclaration,
22+
VariableDeclarator,
23+
BlockStatement,
24+
ExportNamedDeclaration,
25+
ExportDefaultDeclaration,
26+
ExportAllDeclaration,
27+
FunctionDeclaration,
28+
VariableDeclaration,
29+
ClassDeclaration,
30+
ExportSpecifier,
31+
} from 'estree';
32+
// import { DYNAMIC_IMPORT_DECLARATION } from './types';
33+
import * as acorn from 'acorn';
34+
// const acorn = require('acorn');
2035
const acornWalk = require('acorn-walk');
21-
const dynamicImport = require('acorn-dynamic-import');
36+
// const dynamicImport = require('acorn-dynamic-import');
2237

23-
const DYNAMIC_IMPORT_BASEVISITOR = Object.assign({}, acornWalk.base, {
24-
[DYNAMIC_IMPORT_DECLARATION]: () => {},
25-
});
38+
// const DYNAMIC_IMPORT_BASEVISITOR = Object.assign({}, acornWalk.base, {
39+
// [DYNAMIC_IMPORT_DECLARATION]: () => {},
40+
// });
2641

2742
export const walk = {
28-
simple(node: Program, visitors: any): void {
29-
acornWalk.simple(node, visitors, DYNAMIC_IMPORT_BASEVISITOR);
30-
},
31-
ancestor(node: Program, visitors: any): void {
32-
acornWalk.ancestor(node, visitors, DYNAMIC_IMPORT_BASEVISITOR);
33-
},
43+
simple: acornWalk.simple,
44+
ancestor: acornWalk.ancestor,
45+
// simple(node: Program, visitors: any): void {
46+
// acornWalk.simple(node, visitors, DYNAMIC_IMPORT_BASEVISITOR);
47+
// },
48+
// ancestor(node: Program, visitors: any): void {
49+
// acornWalk.ancestor(node, visitors, DYNAMIC_IMPORT_BASEVISITOR);
50+
// },
3451
};
3552

3653
const DEFAULT_ACORN_OPTIONS = {
37-
ecmaVersion: 2019,
38-
sourceType: 'module',
54+
ecmaVersion: 2020 as any,
55+
sourceType: 'module' as any,
3956
preserveParens: false,
4057
ranges: true,
4158
};
4259

4360
export function parse(source: string): Program {
44-
return acorn.Parser.extend(dynamicImport.default).parse(source, DEFAULT_ACORN_OPTIONS);
61+
return (acorn.parse(source, DEFAULT_ACORN_OPTIONS) as unknown) as Program;
62+
// return acorn.Parser.extend(dynamicImport.default).parse(source, DEFAULT_ACORN_OPTIONS);
63+
}
64+
65+
export function isIdentifier(node: BaseNode): node is Identifier {
66+
return node.type === 'Identifier';
67+
}
68+
export function isImportDeclaration(node: BaseNode): node is ImportDeclaration {
69+
return node.type === 'ImportDeclaration';
70+
}
71+
export function isImportExpression(node: BaseNode): boolean {
72+
// @types/estree does not yet support 2020 addons to ECMA.
73+
// This includes ImportExpression ... import("thing")
74+
return node.type === 'ImportExpression';
75+
}
76+
export function isVariableDeclarator(node: BaseNode): node is VariableDeclarator {
77+
return node.type === 'VariableDeclarator';
78+
}
79+
export function isBlockStatement(node: BaseNode): node is BlockStatement {
80+
return node.type === 'BlockStatement';
81+
}
82+
export function isProgram(node: BaseNode): node is Program {
83+
return node.type === 'Program';
84+
}
85+
export function isExportNamedDeclaration(node: BaseNode): node is ExportNamedDeclaration {
86+
return node.type === 'ExportNamedDeclaration';
87+
}
88+
export function isExportDefaultDeclaration(node: BaseNode): node is ExportDefaultDeclaration {
89+
return node.type === 'ExportDefaultDeclaration';
90+
}
91+
export function isExportAllDeclaration(node: BaseNode): node is ExportAllDeclaration {
92+
return node.type === 'ExportAllDeclaration';
93+
}
94+
export function isFunctionDeclaration(node: BaseNode): node is FunctionDeclaration {
95+
return node.type === 'FunctionDeclaration';
96+
}
97+
export function isVariableDeclaration(node: BaseNode): node is VariableDeclaration {
98+
return node.type === 'VariableDeclaration';
99+
}
100+
export function isClassDeclaration(node: BaseNode): node is ClassDeclaration {
101+
return node.type === 'ClassDeclaration';
102+
}
103+
export function isExportSpecifier(node: BaseNode): node is ExportSpecifier {
104+
return node.type === 'ExportSpecifier';
45105
}

src/compiler.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ const {
1919
getNativeImagePath,
2020
getFirstSupportedPlatform,
2121
} = require('google-closure-compiler/lib/utils.js');
22-
import { Transform } from './types';
23-
import { postCompilation } from './transforms';
22+
import { postCompilation } from './transformers/chunk/transforms';
2423
import { RenderedChunk } from 'rollup';
24+
import { ChunkTransform } from './transform';
2525

2626
enum Platform {
2727
NATIVE = 'native',
@@ -73,7 +73,7 @@ function orderPlatforms(platformPreference: Platform | string): Array<Platform>
7373
export default function(
7474
compileOptions: CompileOptions,
7575
chunk: RenderedChunk,
76-
transforms: Array<Transform>,
76+
transforms: Array<ChunkTransform>,
7777
): Promise<string> {
7878
return new Promise((resolve: (stdOut: string) => void, reject: (error: any) => void) => {
7979
const [config, platform] = filterContent(compileOptions);
@@ -97,7 +97,8 @@ export default function(
9797
} else if (exitCode !== 0) {
9898
reject(new Error(`Google Closure Compiler exit ${exitCode}: ${stdErr}`));
9999
} else {
100-
resolve(await postCompilation(code, chunk, transforms));
100+
const postCompiled = await postCompilation(code, chunk, transforms);
101+
resolve(postCompiled.code);
101102
}
102103
});
103104
});

0 commit comments

Comments
 (0)