Skip to content

Commit

Permalink
🚀 Change const to let VariableDeclarations for ESM Output (#26727)
Browse files Browse the repository at this point in the history
* Babel plugin for const -> let
  • Loading branch information
kristoferbaxter authored Feb 11, 2020
1 parent ca175d7 commit b245bc2
Show file tree
Hide file tree
Showing 11 changed files with 300 additions and 0 deletions.
35 changes: 35 additions & 0 deletions build-system/babel-plugins/babel-plugin-const-transformer/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Copyright 2020 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* Changes the
* The above said variables are in src/mode.js file.
* @param {Object} babelTypes
* @return {!Object}
*/
module.exports = function({types: t}) {
return {
visitor: {
VariableDeclaration(path) {
if (path.node.kind !== 'const') {
return;
}

path.replaceWith(t.variableDeclaration('let', path.node.declarations));
},
},
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Copyright 2020 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

let foo = true;
let bar = 1;
let baz = NaN;
let yep = 'hello';
let another = {};
let array = [];

function thing() {
let foo = true;
let bar = 1;
let baz = NaN;
let yep = 'hello';
let another = {};
let array = [];
}

thing: {
let foo = true;
let bar = 1;
let baz = NaN;
let yep = 'hello';
let another = {};
let array = [];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"plugins": ["../../../../../babel-plugin-const-transformer"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Copyright 2020 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
let foo = true;
let bar = 1;
let baz = NaN;
let yep = 'hello';
let another = {};
let array = [];

function thing() {
let foo = true;
let bar = 1;
let baz = NaN;
let yep = 'hello';
let another = {};
let array = [];
}

thing: {
let foo = true;
let bar = 1;
let baz = NaN;
let yep = 'hello';
let another = {};
let array = [];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Copyright 2020 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

var foo = true;
var bar = 1;
var baz = NaN;
var yep = 'hello';
var another = {};
var array = [];

function thing() {
var foo = true;
var bar = 1;
var baz = NaN;
var yep = 'hello';
var another = {};
var array = [];
}

thing: {
var foo = true;
var bar = 1;
var baz = NaN;
var yep = 'hello';
var another = {};
var array = [];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"plugins": ["../../../../../babel-plugin-const-transformer"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Copyright 2020 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var foo = true;
var bar = 1;
var baz = NaN;
var yep = 'hello';
var another = {};
var array = [];

function thing() {
var foo = true;
var bar = 1;
var baz = NaN;
var yep = 'hello';
var another = {};
var array = [];
}

thing: {
var foo = true;
var bar = 1;
var baz = NaN;
var yep = 'hello';
var another = {};
var array = [];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Copyright 2020 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

const foo = true;
const bar = 1;
const baz = NaN;
const yep = 'hello';
const another = {};
const array = [];

function thing() {
const foo = true;
const bar = 1;
const baz = NaN;
const yep = 'hello';
const another = {};
const array = [];
}

thing: {
const foo = true;
const bar = 1;
const baz = NaN;
const yep = 'hello';
const another = {};
const array = [];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"plugins": ["../../../../../babel-plugin-const-transformer"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Copyright 2020 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
let foo = true;
let bar = 1;
let baz = NaN;
let yep = 'hello';
let another = {};
let array = [];

function thing() {
let foo = true;
let bar = 1;
let baz = NaN;
let yep = 'hello';
let another = {};
let array = [];
}

thing: {
let foo = true;
let bar = 1;
let baz = NaN;
let yep = 'hello';
let another = {};
let array = [];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Copyright 2020 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

const runner = require('@babel/helper-plugin-test-runner').default;

runner(__dirname);

0 comments on commit b245bc2

Please sign in to comment.