-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🚀 babel/terser: rename all amp privates with sentinel suffix (#36143)
- Loading branch information
Showing
11 changed files
with
279 additions
and
21 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
65 changes: 65 additions & 0 deletions
65
build-system/babel-plugins/babel-plugin-transform-rename-privates/index.js
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,65 @@ | ||
/** | ||
* Renames all of AMP's class properties to have a unique suffix, `AMP_PRIVATE_`. | ||
* This acts as an indicator for terser to mangle the names even when combined with 3p files. | ||
* | ||
* @param {babel} babel | ||
* @interface {babel.PluginPass} | ||
* @return {babel.PluginObj} | ||
*/ | ||
module.exports = function (babel) { | ||
const {types: t} = babel; | ||
|
||
/** | ||
* Adds trailing AMP_PRIVATE_ suffix to private identifiers. | ||
* @param {string} field | ||
* @return {function(*,*):void} | ||
*/ | ||
function renamePrivate(field) { | ||
return function (path, state) { | ||
if (!isAmpSrc(state)) { | ||
return; | ||
} | ||
if (path.node.computed) { | ||
return; | ||
} | ||
|
||
const key = path.get(field); | ||
if (!key.isIdentifier()) { | ||
return; | ||
} | ||
|
||
const {name} = key.node; | ||
if (name.endsWith('_AMP_PRIVATE_')) { | ||
return; | ||
} | ||
|
||
if (!name.endsWith('_')) { | ||
return; | ||
} | ||
key.replaceWith(t.identifier(`${name}AMP_PRIVATE_`)); | ||
}; | ||
} | ||
|
||
return { | ||
visitor: { | ||
Method: renamePrivate('key'), | ||
Property: renamePrivate('key'), | ||
MemberExpression: renamePrivate('property'), | ||
OptionalMemberExpression: renamePrivate('property'), | ||
}, | ||
}; | ||
}; | ||
|
||
/** | ||
* @param {*} state | ||
* @return {boolean} | ||
*/ | ||
function isAmpSrc(state) { | ||
const filename = state.file.opts.filenameRelative; | ||
if (!filename) { | ||
throw new Error('Cannot use plugin without providing a filename'); | ||
} | ||
return !( | ||
filename.startsWith('node_modules') || filename.startsWith('third_party') | ||
); | ||
} |
67 changes: 67 additions & 0 deletions
67
...bel-plugin-transform-rename-privates/test/fixtures/transform/rename-amp-privates/input.js
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,67 @@ | ||
const obj = { | ||
test_: 1, | ||
method_() {}, | ||
get getter_() {}, | ||
set setter_(v) {}, | ||
shorthand_, | ||
|
||
[test_]: 1, | ||
[method_]() {}, | ||
get [getter_]() {}, | ||
set [setter_](v) {}, | ||
|
||
'test_': 1, | ||
'method_'() {}, | ||
get 'getter_'() {}, | ||
set 'setter_'(v) {}, | ||
}; | ||
|
||
class Instance { | ||
test_ = 1; | ||
method_() {} | ||
get getter_() {} | ||
set setter_(v) {} | ||
|
||
[test_] = 1; | ||
[method_]() {} | ||
get [getter_]() {} | ||
set [setter_](v) {} | ||
|
||
'test_' = 1 | ||
'method_'() {} | ||
get 'getter_'() {} | ||
set 'setter_'(v) {} | ||
} | ||
|
||
class Static { | ||
static test_ = 1; | ||
static method_() {} | ||
static get getter_() {} | ||
static set setter_(v) {} | ||
|
||
static [test_] = 1; | ||
static [method_]() {} | ||
static get [getter_]() {} | ||
static set [setter_](v) {} | ||
|
||
static 'test_' = 1 | ||
static 'method_'() {} | ||
static get 'getter_'() {} | ||
static set 'setter_'(v) {} | ||
} | ||
|
||
foo.bar_; | ||
foo[bar_]; | ||
foo['bar_']; | ||
|
||
foo?.bar_; | ||
foo?.[bar_]; | ||
foo?.['bar_']; | ||
|
||
deep.foo?.bar_; | ||
deep.foo?.[bar_]; | ||
deep.foo?.['bar_']; | ||
|
||
deep?.foo.bar_; | ||
deep?.foo.[bar_]; | ||
deep?.foo.['bar_']; |
7 changes: 7 additions & 0 deletions
7
...plugin-transform-rename-privates/test/fixtures/transform/rename-amp-privates/options.json
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,7 @@ | ||
{ | ||
"plugins": [ | ||
"../../../.." | ||
], | ||
"sourceType": "module", | ||
"filenameRelative": "src/foo.js" | ||
} |
94 changes: 94 additions & 0 deletions
94
...l-plugin-transform-rename-privates/test/fixtures/transform/rename-amp-privates/output.mjs
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,94 @@ | ||
const obj = { | ||
test_AMP_PRIVATE_: 1, | ||
|
||
method_AMP_PRIVATE_() {}, | ||
|
||
get getter_AMP_PRIVATE_() {}, | ||
|
||
set setter_AMP_PRIVATE_(v) {}, | ||
|
||
shorthand_AMP_PRIVATE_: shorthand_, | ||
[test_]: 1, | ||
|
||
[method_]() {}, | ||
|
||
get [getter_]() {}, | ||
|
||
set [setter_](v) {}, | ||
|
||
'test_': 1, | ||
|
||
'method_'() {}, | ||
|
||
get 'getter_'() {}, | ||
|
||
set 'setter_'(v) {} | ||
|
||
}; | ||
|
||
class Instance { | ||
test_AMP_PRIVATE_ = 1; | ||
|
||
method_AMP_PRIVATE_() {} | ||
|
||
get getter_AMP_PRIVATE_() {} | ||
|
||
set setter_AMP_PRIVATE_(v) {} | ||
|
||
[test_] = 1; | ||
|
||
[method_]() {} | ||
|
||
get [getter_]() {} | ||
|
||
set [setter_](v) {} | ||
|
||
'test_' = 1; | ||
|
||
'method_'() {} | ||
|
||
get 'getter_'() {} | ||
|
||
set 'setter_'(v) {} | ||
|
||
} | ||
|
||
class Static { | ||
static test_AMP_PRIVATE_ = 1; | ||
|
||
static method_AMP_PRIVATE_() {} | ||
|
||
static get getter_AMP_PRIVATE_() {} | ||
|
||
static set setter_AMP_PRIVATE_(v) {} | ||
|
||
static [test_] = 1; | ||
|
||
static [method_]() {} | ||
|
||
static get [getter_]() {} | ||
|
||
static set [setter_](v) {} | ||
|
||
static 'test_' = 1; | ||
|
||
static 'method_'() {} | ||
|
||
static get 'getter_'() {} | ||
|
||
static set 'setter_'(v) {} | ||
|
||
} | ||
|
||
foo.bar_AMP_PRIVATE_; | ||
foo[bar_]; | ||
foo['bar_']; | ||
foo?.bar_AMP_PRIVATE_; | ||
foo?.[bar_]; | ||
foo?.['bar_']; | ||
deep.foo?.bar_AMP_PRIVATE_; | ||
deep.foo?.[bar_]; | ||
deep.foo?.['bar_']; | ||
deep?.foo.bar_AMP_PRIVATE_; | ||
deep?.foo[bar_]; | ||
deep?.foo['bar_']; |
9 changes: 9 additions & 0 deletions
9
...el-plugin-transform-rename-privates/test/fixtures/transform/skip-nonamp-privates/input.js
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,9 @@ | ||
class Foo { | ||
constructor() { | ||
/** | ||
* @type {string} | ||
* @private | ||
*/ | ||
this.hello_ = 'world'; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...lugin-transform-rename-privates/test/fixtures/transform/skip-nonamp-privates/options.json
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,9 @@ | ||
{ | ||
"plugins": [ | ||
[ | ||
"../../../.." | ||
] | ||
], | ||
"sourceType": "module", | ||
"filenameRelative": "node_modules/foo.js" | ||
} |
10 changes: 10 additions & 0 deletions
10
...-plugin-transform-rename-privates/test/fixtures/transform/skip-nonamp-privates/output.mjs
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,10 @@ | ||
class Foo { | ||
constructor() { | ||
/** | ||
* @type {string} | ||
* @private | ||
*/ | ||
this.hello_ = 'world'; | ||
} | ||
|
||
} |
3 changes: 3 additions & 0 deletions
3
build-system/babel-plugins/babel-plugin-transform-rename-privates/test/index.js
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,3 @@ | ||
const runner = require('@babel/helper-plugin-test-runner').default; | ||
|
||
runner(__dirname); |
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