Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(features): rename runtimeFlags to lwcRuntimeFlags #3004

Merged
merged 4 commits into from
Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 43 additions & 43 deletions packages/@lwc/features/src/__tests__/flags-prod.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,38 +38,38 @@ pluginTester({
}
`,
output: `
import features, { runtimeFlags } from '@lwc/features';
import features, { lwcRuntimeFlags } from '@lwc/features';

if (runtimeFlags.ENABLE_FEATURE_NULL) {
if (lwcRuntimeFlags.ENABLE_FEATURE_NULL) {
console.log('features.ENABLE_FEATURE_NULL');
}

if (!runtimeFlags.ENABLE_FEATURE_NULL) {
if (!lwcRuntimeFlags.ENABLE_FEATURE_NULL) {
console.log('!features.ENABLE_FEATURE_NULL');
}
`,
},
'should not transform null runtime flags': {
code: `
import { runtimeFlags } from '@lwc/features';
import { lwcRuntimeFlags } from '@lwc/features';

if (runtimeFlags.ENABLE_FEATURE_NULL) {
console.log('runtimeFlags.ENABLE_FEATURE_NULL');
if (lwcRuntimeFlags.ENABLE_FEATURE_NULL) {
console.log('lwcRuntimeFlags.ENABLE_FEATURE_NULL');
}

if (!runtimeFlags.ENABLE_FEATURE_NULL) {
console.log('!runtimeFlags.ENABLE_FEATURE_NULL');
if (!lwcRuntimeFlags.ENABLE_FEATURE_NULL) {
console.log('!lwcRuntimeFlags.ENABLE_FEATURE_NULL');
}
`,
output: `
import { runtimeFlags } from '@lwc/features';
import { lwcRuntimeFlags } from '@lwc/features';

if (runtimeFlags.ENABLE_FEATURE_NULL) {
console.log('runtimeFlags.ENABLE_FEATURE_NULL');
if (lwcRuntimeFlags.ENABLE_FEATURE_NULL) {
console.log('lwcRuntimeFlags.ENABLE_FEATURE_NULL');
}

if (!runtimeFlags.ENABLE_FEATURE_NULL) {
console.log('!runtimeFlags.ENABLE_FEATURE_NULL');
if (!lwcRuntimeFlags.ENABLE_FEATURE_NULL) {
console.log('!lwcRuntimeFlags.ENABLE_FEATURE_NULL');
}
`,
},
Expand All @@ -86,28 +86,28 @@ pluginTester({
}
`,
output: `
import features, { runtimeFlags } from '@lwc/features';
import features, { lwcRuntimeFlags } from '@lwc/features';
{
console.log('features.ENABLE_FEATURE_TRUE');
}
`,
},
'should transform boolean-true runtime flags': {
code: `
import { runtimeFlags } from '@lwc/features';
import { lwcRuntimeFlags } from '@lwc/features';

if (runtimeFlags.ENABLE_FEATURE_TRUE) {
console.log('runtimeFlags.ENABLE_FEATURE_TRUE');
if (lwcRuntimeFlags.ENABLE_FEATURE_TRUE) {
console.log('lwcRuntimeFlags.ENABLE_FEATURE_TRUE');
}

if (!runtimeFlags.ENABLE_FEATURE_TRUE) {
console.log('!runtimeFlags.ENABLE_FEATURE_TRUE');
if (!lwcRuntimeFlags.ENABLE_FEATURE_TRUE) {
console.log('!lwcRuntimeFlags.ENABLE_FEATURE_TRUE');
}
`,
output: `
import { runtimeFlags } from '@lwc/features';
import { lwcRuntimeFlags } from '@lwc/features';
{
console.log('runtimeFlags.ENABLE_FEATURE_TRUE');
console.log('lwcRuntimeFlags.ENABLE_FEATURE_TRUE');
}
`,
},
Expand All @@ -124,28 +124,28 @@ pluginTester({
}
`,
output: `
import features, { runtimeFlags } from '@lwc/features';
import features, { lwcRuntimeFlags } from '@lwc/features';
{
console.log('!features.ENABLE_FEATURE_FALSE');
}
`,
},
'should transform boolean-false runtime flags': {
code: `
import { runtimeFlags } from '@lwc/features';
import { lwcRuntimeFlags } from '@lwc/features';

if (runtimeFlags.ENABLE_FEATURE_FALSE) {
console.log('runtimeFlags.ENABLE_FEATURE_FALSE');
if (lwcRuntimeFlags.ENABLE_FEATURE_FALSE) {
console.log('lwcRuntimeFlags.ENABLE_FEATURE_FALSE');
}

if (!runtimeFlags.ENABLE_FEATURE_FALSE) {
console.log('!runtimeFlags.ENABLE_FEATURE_FALSE');
if (!lwcRuntimeFlags.ENABLE_FEATURE_FALSE) {
console.log('!lwcRuntimeFlags.ENABLE_FEATURE_FALSE');
}
`,
output: `
import { runtimeFlags } from '@lwc/features';
import { lwcRuntimeFlags } from '@lwc/features';
{
console.log('!runtimeFlags.ENABLE_FEATURE_FALSE');
console.log('!lwcRuntimeFlags.ENABLE_FEATURE_FALSE');
}
`,
},
Expand All @@ -158,7 +158,7 @@ pluginTester({
}
`,
output: `
import FEATURES, { runtimeFlags } from '@lwc/features';
import FEATURES, { lwcRuntimeFlags } from '@lwc/features';

if (isTrue(FEATURES.ENABLE_FEATURE_TRUE)) {
console.log('isTrue(ENABLE_FEATURE_TRUE)');
Expand All @@ -167,17 +167,17 @@ pluginTester({
},
'should not transform if-tests that are not member expressions (runtime)': {
code: `
import { runtimeFlags } from '@lwc/features';
import { lwcRuntimeFlags } from '@lwc/features';

if (isTrue(runtimeFlags.ENABLE_FEATURE_TRUE)) {
console.log('runtimeFlags.ENABLE_FEATURE_TRUE');
if (isTrue(lwcRuntimeFlags.ENABLE_FEATURE_TRUE)) {
console.log('lwcRuntimeFlags.ENABLE_FEATURE_TRUE');
}
`,
output: `
import { runtimeFlags } from '@lwc/features';
import { lwcRuntimeFlags } from '@lwc/features';

if (isTrue(runtimeFlags.ENABLE_FEATURE_TRUE)) {
console.log('runtimeFlags.ENABLE_FEATURE_TRUE');
if (isTrue(lwcRuntimeFlags.ENABLE_FEATURE_TRUE)) {
console.log('lwcRuntimeFlags.ENABLE_FEATURE_TRUE');
}
`,
},
Expand All @@ -187,7 +187,7 @@ pluginTester({
console.log(feats.ENABLE_FEATURE_NULL ? 'foo' : 'bar');
`,
output: `
import feats, { runtimeFlags } from '@lwc/features';
import feats, { lwcRuntimeFlags } from '@lwc/features';
console.log(feats.ENABLE_FEATURE_NULL ? 'foo' : 'bar');
`,
},
Expand Down Expand Up @@ -228,16 +228,16 @@ pluginTester({
}
`,
output: `
import features, { runtimeFlags } from '@lwc/features';
import features, { lwcRuntimeFlags } from '@lwc/features';

if (runtimeFlags.ENABLE_FEATURE_NULL) {
if (lwcRuntimeFlags.ENABLE_FEATURE_NULL) {
{
console.log('nested feature flags sounds like a vary bad idea');
}
}

{
if (runtimeFlags.ENABLE_FEATURE_NULL) {
if (lwcRuntimeFlags.ENABLE_FEATURE_NULL) {
console.log('nested feature flags sounds like a vary bad idea');
}
}
Expand All @@ -258,7 +258,7 @@ pluginTester({
}
`,
output: `
import featureFlag, { runtimeFlags } from '@lwc/features';
import featureFlag, { lwcRuntimeFlags } from '@lwc/features';

function awesome() {
const featureFlag = {
Expand All @@ -277,14 +277,14 @@ pluginTester({
},
'should not transform member expressions that are not runtime flag lookups': {
code: `
import { runtimeFlags } from '@lwc/features';
import { lwcRuntimeFlags } from '@lwc/features';

if (churroteria.ENABLE_FEATURE_TRUE) {
console.log('churroteria.ENABLE_FEATURE_TRUE');
}
`,
output: `
import { runtimeFlags } from '@lwc/features';
import { lwcRuntimeFlags } from '@lwc/features';

if (churroteria.ENABLE_FEATURE_TRUE) {
console.log('churroteria.ENABLE_FEATURE_TRUE');
Expand Down
48 changes: 24 additions & 24 deletions packages/@lwc/features/src/__tests__/flags.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ pluginTester({
}
`,
output: `
import features, { runtimeFlags } from '@lwc/features';
import features, { lwcRuntimeFlags } from '@lwc/features';

if (runtimeFlags.ENABLE_FEATURE_NULL) {
if (lwcRuntimeFlags.ENABLE_FEATURE_NULL) {
console.log('features.ENABLE_FEATURE_NULL');
}

if (!runtimeFlags.ENABLE_FEATURE_NULL) {
if (!lwcRuntimeFlags.ENABLE_FEATURE_NULL) {
console.log('!features.ENABLE_FEATURE_NULL');
}
`,
Expand All @@ -62,13 +62,13 @@ pluginTester({
}
`,
output: `
import features, { runtimeFlags } from '@lwc/features';
import features, { lwcRuntimeFlags } from '@lwc/features';

if (runtimeFlags.ENABLE_FEATURE_TRUE) {
if (lwcRuntimeFlags.ENABLE_FEATURE_TRUE) {
console.log('features.ENABLE_FEATURE_TRUE');
}

if (!runtimeFlags.ENABLE_FEATURE_TRUE) {
if (!lwcRuntimeFlags.ENABLE_FEATURE_TRUE) {
console.log('!features.ENABLE_FEATURE_TRUE');
}
`,
Expand All @@ -86,13 +86,13 @@ pluginTester({
}
`,
output: `
import features, { runtimeFlags } from '@lwc/features';
import features, { lwcRuntimeFlags } from '@lwc/features';

if (runtimeFlags.ENABLE_FEATURE_FALSE) {
if (lwcRuntimeFlags.ENABLE_FEATURE_FALSE) {
console.log('features.ENABLE_FEATURE_FALSE');
}

if (!runtimeFlags.ENABLE_FEATURE_FALSE) {
if (!lwcRuntimeFlags.ENABLE_FEATURE_FALSE) {
console.log('!features.ENABLE_FEATURE_FALSE');
}
`,
Expand All @@ -106,7 +106,7 @@ pluginTester({
}
`,
output: `
import FEATURES, { runtimeFlags } from '@lwc/features';
import FEATURES, { lwcRuntimeFlags } from '@lwc/features';

if (isTrue(FEATURES.ENABLE_FEATURE_TRUE)) {
console.log('isTrue(ENABLE_FEATURE_TRUE)');
Expand All @@ -119,17 +119,17 @@ pluginTester({
console.log(feats.ENABLE_FEATURE_NULL ? 'foo' : 'bar');
`,
output: `
import feats, { runtimeFlags } from '@lwc/features';
import feats, { lwcRuntimeFlags } from '@lwc/features';
console.log(feats.ENABLE_FEATURE_NULL ? 'foo' : 'bar');
`,
},
'should throw an error if runtimeFlags are imported': {
error: 'Invalid import of "runtimeFlags" from "@lwc/features". Use the default export from "@lwc/features" instead of the "runtimeFlags" export when implementing your feature behind a flag.',
'should throw an error if lwcRuntimeFlags are imported': {
error: 'Invalid import of "lwcRuntimeFlags" from "@lwc/features". Use the default export from "@lwc/features" instead of the "lwcRuntimeFlags" export when implementing your feature behind a flag.',
code: `
import { runtimeFlags } from '@lwc/features';
import { lwcRuntimeFlags } from '@lwc/features';

if (runtimeFlags.ENABLE_FEATURE_NULL) {
console.log('runtimeFlags.ENABLE_FEATURE_NULL');
if (lwcRuntimeFlags.ENABLE_FEATURE_NULL) {
console.log('lwcRuntimeFlags.ENABLE_FEATURE_NULL');
}
`,
},
Expand Down Expand Up @@ -170,16 +170,16 @@ pluginTester({
}
`,
output: `
import features, { runtimeFlags } from '@lwc/features';
import features, { lwcRuntimeFlags } from '@lwc/features';

if (runtimeFlags.ENABLE_FEATURE_NULL) {
if (runtimeFlags.ENABLE_FEATURE_TRUE) {
if (lwcRuntimeFlags.ENABLE_FEATURE_NULL) {
if (lwcRuntimeFlags.ENABLE_FEATURE_TRUE) {
console.log('nested feature flags sounds like a vary bad idea');
}
}

if (runtimeFlags.ENABLE_FEATURE_TRUE) {
if (runtimeFlags.ENABLE_FEATURE_NULL) {
if (lwcRuntimeFlags.ENABLE_FEATURE_TRUE) {
if (lwcRuntimeFlags.ENABLE_FEATURE_NULL) {
console.log('nested feature flags sounds like a vary bad idea');
}
}
Expand All @@ -204,7 +204,7 @@ pluginTester({
}
`,
output: `
import featureFlag, { runtimeFlags } from '@lwc/features';
import featureFlag, { lwcRuntimeFlags } from '@lwc/features';

function awesome() {
const featureFlag = {
Expand All @@ -230,7 +230,7 @@ pluginTester({
}
`,
output: `
import featureFlags, { runtimeFlags } from '@lwc/features';
import featureFlags, { lwcRuntimeFlags } from '@lwc/features';

if (churroteria.ENABLE_FEATURE_TRUE) {
console.log('churroteria.ENABLE_FEATURE_TRUE');
Expand All @@ -246,7 +246,7 @@ pluginTester({
}
`,
output: `
import featureFlags, { runtimeFlags } from '@lwc/features';
import featureFlags, { lwcRuntimeFlags } from '@lwc/features';

if (featureFlags['ENABLE_FEATURE_TRUE']) {
console.log("featureFlags['ENABLE_FEATURE_TRUE']");
Expand Down
2 changes: 1 addition & 1 deletion packages/@lwc/features/src/babel-plugin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
const defaultFeatureFlags = require('../../').default;

const RUNTIME_FLAGS_IDENTIFIER = 'runtimeFlags';
const RUNTIME_FLAGS_IDENTIFIER = 'lwcRuntimeFlags';
const FEATURES_PACKAGE_NAME = '@lwc/features';

function validate(name, value) {
Expand Down
10 changes: 6 additions & 4 deletions packages/@lwc/features/src/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ if (!globalThis.lwcRuntimeFlags) {
Object.defineProperty(globalThis, 'lwcRuntimeFlags', { value: create(null) });
}

export const runtimeFlags: Partial<FeatureFlagMap> = globalThis.lwcRuntimeFlags;
export const lwcRuntimeFlags: Partial<FeatureFlagMap> = globalThis.lwcRuntimeFlags;

/**
* Set the value at runtime of a given feature flag. This method only be invoked once per feature
Expand Down Expand Up @@ -54,18 +54,18 @@ export function setFeatureFlag(name: FeatureFlagName, value: FeatureFlagValue):
}
if (process.env.NODE_ENV !== 'production') {
// Allow the same flag to be set more than once outside of production to enable testing
runtimeFlags[name] = value;
lwcRuntimeFlags[name] = value;
} else {
// Disallow the same flag to be set more than once in production
const runtimeValue = runtimeFlags[name];
const runtimeValue = lwcRuntimeFlags[name];
if (!isUndefined(runtimeValue)) {
// eslint-disable-next-line no-console
console.error(
`Failed to set the value "${value}" for the runtime feature flag "${name}". "${name}" has already been set with the value "${runtimeValue}".`
);
return;
}
defineProperty(runtimeFlags, name, { value });
defineProperty(lwcRuntimeFlags, name, { value });
}
}

Expand All @@ -79,4 +79,6 @@ export function setFeatureFlagForTest(name: FeatureFlagName, value: FeatureFlagV
}
}

export const runtimeFlags = lwcRuntimeFlags; // backwards compatibility for before this was renamed
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is good. Just curious what usecase will require this backwards compatibility. Does anybody use runtimeFlags from @lwc/features outside of our repos?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not aware of any, but since this is a public package on npm that (in theory) anyone could use, and since the fix was a one-liner, I added it in. We could definitely remove it in a breaking release. I'll add that to the wishlist: #2964


export default features;