Skip to content

Commit

Permalink
feat(features): runtime flags can only be imported by compiler (#1690)
Browse files Browse the repository at this point in the history
* feat(features): runtime flags can only be imported by compiler

* chore: review feedback

* test: update error message test
  • Loading branch information
ekashida authored Jan 22, 2020
1 parent 242f0fb commit 5936229
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 90 deletions.
103 changes: 13 additions & 90 deletions packages/@lwc/features/src/__tests__/flags.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,30 +49,6 @@ pluginTester({
}
`,
},
'should not transform null runtime flags': {
code: `
import { runtimeFlags } from '@lwc/features';
if (runtimeFlags.ENABLE_FEATURE_NULL) {
console.log('runtimeFlags.ENABLE_FEATURE_NULL');
}
if (!runtimeFlags.ENABLE_FEATURE_NULL) {
console.log('!runtimeFlags.ENABLE_FEATURE_NULL');
}
`,
output: `
import { runtimeFlags } from '@lwc/features';
if (runtimeFlags.ENABLE_FEATURE_NULL) {
console.log('runtimeFlags.ENABLE_FEATURE_NULL');
}
if (!runtimeFlags.ENABLE_FEATURE_NULL) {
console.log('!runtimeFlags.ENABLE_FEATURE_NULL');
}
`,
},
'should transform boolean-true compile-time flags': {
code: `
import features from '@lwc/features';
Expand All @@ -97,30 +73,6 @@ pluginTester({
}
`,
},
'should not transform boolean-true runtime flags': {
code: `
import { runtimeFlags } from '@lwc/features';
if (runtimeFlags.ENABLE_FEATURE_TRUE) {
console.log('runtimeFlags.ENABLE_FEATURE_TRUE');
}
if (!runtimeFlags.ENABLE_FEATURE_TRUE) {
console.log('!runtimeFlags.ENABLE_FEATURE_TRUE');
}
`,
output: `
import { runtimeFlags } from '@lwc/features';
if (runtimeFlags.ENABLE_FEATURE_TRUE) {
console.log('runtimeFlags.ENABLE_FEATURE_TRUE');
}
if (!runtimeFlags.ENABLE_FEATURE_TRUE) {
console.log('!runtimeFlags.ENABLE_FEATURE_TRUE');
}
`,
},
'should transform boolean-false compile-time flags': {
code: `
import features from '@lwc/features';
Expand All @@ -145,30 +97,6 @@ pluginTester({
}
`,
},
'should not transform boolean-false runtime flags': {
code: `
import { runtimeFlags } from '@lwc/features';
if (runtimeFlags.ENABLE_FEATURE_FALSE) {
console.log('runtimeFlags.ENABLE_FEATURE_FALSE');
}
if (!runtimeFlags.ENABLE_FEATURE_FALSE) {
console.log('!runtimeFlags.ENABLE_FEATURE_FALSE');
}
`,
output: `
import { runtimeFlags } from '@lwc/features';
if (runtimeFlags.ENABLE_FEATURE_FALSE) {
console.log('runtimeFlags.ENABLE_FEATURE_FALSE');
}
if (!runtimeFlags.ENABLE_FEATURE_FALSE) {
console.log('!runtimeFlags.ENABLE_FEATURE_FALSE');
}
`,
},
'should not transform if-tests that are not member expressions (compile-time)': {
code: `
import FEATURES from '@lwc/features';
Expand All @@ -185,22 +113,6 @@ pluginTester({
}
`,
},
'should not transform if-tests that are not member expressions (runtime)': {
code: `
import { runtimeFlags } from '@lwc/features';
if (isTrue(runtimeFlags.ENABLE_FEATURE_TRUE)) {
console.log('runtimeFlags.ENABLE_FEATURE_TRUE');
}
`,
output: `
import { runtimeFlags } from '@lwc/features';
if (isTrue(runtimeFlags.ENABLE_FEATURE_TRUE)) {
console.log('runtimeFlags.ENABLE_FEATURE_TRUE');
}
`,
},
'should not transform feature flags when used with a ternary operator': {
code: `
import feats from '@lwc/features';
Expand All @@ -211,6 +123,17 @@ pluginTester({
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.',
code: `
import { runtimeFlags } from '@lwc/features';
if (runtimeFlags.ENABLE_FEATURE_NULL) {
console.log('runtimeFlags.ENABLE_FEATURE_NULL');
}
`,
},
'should throw an error if the flag is undefined': {
error: 'Invalid feature flag "ENABLE_THE_BEER". Flag is undefined.',
code: `
Expand Down Expand Up @@ -302,14 +225,14 @@ pluginTester({
},
'should not transform member expressions that are not runtime flag lookups': {
code: `
import { runtimeFlags } from '@lwc/features';
import featureFlags from '@lwc/features';
if (churroteria.ENABLE_FEATURE_TRUE) {
console.log('churroteria.ENABLE_FEATURE_TRUE');
}
`,
output: `
import { runtimeFlags } from '@lwc/features';
import featureFlags, { runtimeFlags } from '@lwc/features';
if (churroteria.ENABLE_FEATURE_TRUE) {
console.log('churroteria.ENABLE_FEATURE_TRUE');
Expand Down
5 changes: 5 additions & 0 deletions packages/@lwc/features/src/babel-plugin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ module.exports = function({ types: t }) {
const didImportRuntimeFlags = specifiers.some(specifier => {
return specifier.local && specifier.local.name === RUNTIME_FLAGS_IDENTIFIER;
});
if (didImportRuntimeFlags && !this.opts.prod) {
throw new Error(
`Invalid import of "${RUNTIME_FLAGS_IDENTIFIER}" from "${FEATURES_PACKAGE_NAME}". Use the default export from "${FEATURES_PACKAGE_NAME}" instead of the "${RUNTIME_FLAGS_IDENTIFIER}" export when implementing your feature behind a flag.`
);
}
if (!didImportRuntimeFlags) {
// Blindly import a binding for `runtimeFlags` if we haven't
// already. Tree-shaking will simply remove it if unused.
Expand Down

0 comments on commit 5936229

Please sign in to comment.