Skip to content

Commit

Permalink
add transform for toggling isEsm flag
Browse files Browse the repository at this point in the history
  • Loading branch information
ddelgrosso1 committed Aug 31, 2023
1 parent 6f683af commit c7514b4
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 2 deletions.
1 change: 0 additions & 1 deletion tools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
"rimraf": "^5.0.1",
"@babel/core": "^7.22.5",
"@babel/traverse": "^7.22.5",
"@types/rimraf": "^3.0.2",
"uglify-js": "^3.17.0",
"walkdir": "^0.4.0"
},
Expand Down
44 changes: 44 additions & 0 deletions tools/src/toggleESMFlagVariable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2023 Google LLC
//
// 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.

import {Visitor, types} from '@babel/core';

export interface PluginOptions {
opts?: {
variableIdentifier?: string;
replacementValue?: boolean;
};
}

export default function toggleESMFlagVariable(): {
visitor: Visitor<PluginOptions>;
} {
return {
visitor: {
VariableDeclarator(path, state) {
const opts = state.opts || {};
const variableIdentifier = opts.variableIdentifier || 'isEsm';
const replacementValue = opts.replacementValue || false;
const {node} = path;
const identifier = node.id as types.Identifier;
if (
identifier.name === variableIdentifier &&
node.init?.type === 'BooleanLiteral'
) {
node.init.value = replacementValue;
}
},
},
};
}
73 changes: 73 additions & 0 deletions tools/test/toggleESMFlagVariable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright 2023 Google LLC
//
// 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.

import {describe, it} from 'mocha';
import * as babel from '@babel/core';
import toggleESMFlagVariable from '../src/toggleESMFlagVariable';
import * as assert from 'assert';

describe('toggle ESM flag variable', () => {
it('does not replace a non-boolean variable named isEsm', async () => {
const program = 'const isEsm = 100;';
const result = await babel.transformAsync(program, {
plugins: [toggleESMFlagVariable],
});

assert.strictEqual(result?.code, program);
});

it('replaces isEsm with the default value of false when no argument provided', async () => {
const program = 'const isEsm = true;';
const expected = 'const isEsm = false;';
const result = await babel.transformAsync(program, {
plugins: [toggleESMFlagVariable],
});

assert.strictEqual(result?.code, expected);
});

it('does not replace the user defined variable name when it is non-boolean', async () => {
const program = 'const foo = 100;';
const result = await babel.transformAsync(program, {
plugins: [[toggleESMFlagVariable, {variableIdentifier: 'foo'}]],
});

assert.strictEqual(result?.code, program);
});

it('replaces the user defined variable name with the default value of false', async () => {
const program = 'const foo = true;';
const expected = 'const foo = false;';
const result = await babel.transformAsync(program, {
plugins: [[toggleESMFlagVariable, {variableIdentifier: 'foo'}]],
});

assert.strictEqual(result?.code, expected);
});

it('replaces the user defined variable name with the user defined value', async () => {
const program = 'const foo = false;';
const expected = 'const foo = true;';
const result = await babel.transformAsync(program, {
plugins: [
[
toggleESMFlagVariable,
{variableIdentifier: 'foo', replacementValue: true},
],
],
});

assert.strictEqual(result?.code, expected);
});
});
9 changes: 8 additions & 1 deletion tools/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,12 @@
"noImplicitAny": true,
"resolveJsonModule": true,
},
"include": ["src/*.ts","test/minify.ts", "test/compileProtos.ts", "test/replaceImportMetaUrl.ts", "test/replaceESMMockingLib.ts"]
"include": [
"src/*.ts",
"test/minify.ts",
"test/compileProtos.ts",
"test/replaceImportMetaUrl.ts",
"test/replaceESMMockingLib.ts",
"test/toggleESMFlagVariable.ts"
]
}

0 comments on commit c7514b4

Please sign in to comment.