-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add transform for toggling isEsm flag
- Loading branch information
1 parent
6f683af
commit c7514b4
Showing
4 changed files
with
125 additions
and
2 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
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,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; | ||
} | ||
}, | ||
}, | ||
}; | ||
} |
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,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); | ||
}); | ||
}); |
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