Skip to content

Commit 679373c

Browse files
Remove some easy to remove semicolons for ASI (#321)
1 parent d34ed7c commit 679373c

File tree

211 files changed

+261
-209
lines changed

Some content is hidden

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

211 files changed

+261
-209
lines changed

src/transformers/chunk/asi.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* Copyright 2018 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+
import { ChunkTransform } from '../../transform';
18+
import { Range } from '../../types';
19+
import { parse } from '../../acorn';
20+
import MagicString from 'magic-string';
21+
22+
export default class ASITransform extends ChunkTransform {
23+
public name = 'ASITransform';
24+
25+
/**
26+
* Small reduction in semi-colons, removing from end of block statements.
27+
* @param source source following closure compiler minification
28+
*/
29+
public async post(source: MagicString): Promise<MagicString> {
30+
const code = source.toString();
31+
const program = parse(code);
32+
33+
if (program.body) {
34+
const lastStatement = program.body[program.body.length - 1];
35+
if (lastStatement) {
36+
const [start, end] = lastStatement.range as Range;
37+
if (lastStatement.type === 'EmptyStatement') {
38+
source.remove(start, end);
39+
} else {
40+
const lastStatementSource = code.substring(start, end);
41+
if (lastStatementSource.endsWith(';')) {
42+
source.overwrite(start, end, code.substring(start, end - 1));
43+
}
44+
}
45+
}
46+
}
47+
48+
return source;
49+
}
50+
}

src/transformers/chunk/const.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export default class ConstTransform extends ChunkTransform {
3131
*/
3232
public async pre(source: MagicString): Promise<MagicString> {
3333
const code = source.toString();
34-
const program = parse(source.toString());
34+
const program = parse(code);
3535

3636
walk.simple(program, {
3737
VariableDeclaration(node: VariableDeclaration) {

src/transformers/chunk/transforms.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import ExportTransform from './exports';
3030
import ImportTransform from './imports';
3131
import StrictTransform from './strict';
3232
import ConstTransform from './const';
33+
import ASITransform from './asi';
3334
import { ChunkTransform, chunkLifecycle } from '../../transform';
3435
import { Mangle } from '../mangle';
3536
import { Ebbinghaus } from '../ebbinghaus';
@@ -46,6 +47,7 @@ const TRANSFORMS: Array<typeof ChunkTransform> = [
4647
StrictTransform,
4748
ExportTransform,
4849
ImportTransform,
50+
ASITransform,
4951
// Acorn cannot parse content starting here.
5052
HashbangApplyTransform,
5153
];
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export var multipleArguments=(a,b)=>console.log(a,b);
1+
export var multipleArguments=(a,b)=>console.log(a,b)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export var multipleArguments=(a,b)=>console.log(a,b);
1+
export var multipleArguments=(a,b)=>console.log(a,b)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export function multipleArguments(a,b){return console.log(a,b)};
1+
export function multipleArguments(a,b){return console.log(a,b)}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11

2-
export var multipleArguments=(a, b) => console.log(a, b);
2+
export var multipleArguments=(a, b) => console.log(a, b)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export var singleArgument=a=>console.log(a);
1+
export var singleArgument=a=>console.log(a)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export var singleArgument=a=>console.log(a);
1+
export var singleArgument=a=>console.log(a)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export function singleArgument(a){return console.log(a)};
1+
export function singleArgument(a){return console.log(a)}

0 commit comments

Comments
 (0)