Skip to content

Commit

Permalink
feat: Call namespace error as a warning (#3475)
Browse files Browse the repository at this point in the history
* feat: call namespace as a warning

* Fix prettier config

* Add tagged template literals to test to improve coverage

Co-authored-by: Lukas Taegert-Atkinson <lukas.taegert-atkinson@tngtech.com>
  • Loading branch information
guybedford and lukastaegert committed Mar 31, 2020
1 parent 5eb01fb commit c69c41b
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 49 deletions.
6 changes: 4 additions & 2 deletions .prettierrc
@@ -1,5 +1,7 @@
{
"arrowParens": "avoid",
"printWidth": 100,
"singleQuote": true,
"useTabs": true,
"printWidth": 100
"trailingComma": "none",
"useTabs": true
}
12 changes: 6 additions & 6 deletions src/ast/nodes/CallExpression.ts
Expand Up @@ -3,7 +3,7 @@ import { BLANK } from '../../utils/blank';
import {
findFirstOccurrenceOutsideComment,
NodeRenderOptions,
RenderOptions
RenderOptions,
} from '../../utils/renderHelpers';
import { CallOptions } from '../CallOptions';
import { DeoptimizableEntity } from '../DeoptimizableEntity';
Expand All @@ -13,7 +13,7 @@ import {
ObjectPath,
PathTracker,
SHARED_RECURSION_TRACKER,
UNKNOWN_PATH
UNKNOWN_PATH,
} from '../utils/PathTracker';
import { LiteralValueOrUnknown, UnknownValue, UNKNOWN_EXPRESSION } from '../values';
import Identifier from './Identifier';
Expand All @@ -40,10 +40,10 @@ export default class CallExpression extends NodeBase implements DeoptimizableEnt
const variable = this.scope.findVariable(this.callee.name);

if (variable.isNamespace) {
return this.context.error(
this.context.warn(
{
code: 'CANNOT_CALL_NAMESPACE',
message: `Cannot call a namespace ('${this.callee.name}')`
message: `Cannot call a namespace ('${this.callee.name}')`,
},
this.start
);
Expand All @@ -54,7 +54,7 @@ export default class CallExpression extends NodeBase implements DeoptimizableEnt
{
code: 'EVAL',
message: `Use of eval is strongly discouraged, as it poses security risks and may cause issues with minification`,
url: 'https://rollupjs.org/guide/en/#avoiding-eval'
url: 'https://rollupjs.org/guide/en/#avoiding-eval',
},
this.start
);
Expand Down Expand Up @@ -212,7 +212,7 @@ export default class CallExpression extends NodeBase implements DeoptimizableEnt
initialise() {
this.callOptions = {
args: this.arguments,
withNew: false
withNew: false,
};
}

Expand Down
8 changes: 4 additions & 4 deletions src/ast/nodes/TaggedTemplateExpression.ts
Expand Up @@ -20,10 +20,10 @@ export default class TaggedTemplateExpression extends NodeBase {
const variable = this.scope.findVariable(name);

if (variable.isNamespace) {
return this.context.error(
this.context.warn(
{
code: 'CANNOT_CALL_NAMESPACE',
message: `Cannot call a namespace ('${name}')`
message: `Cannot call a namespace ('${name}')`,
},
this.start
);
Expand All @@ -34,7 +34,7 @@ export default class TaggedTemplateExpression extends NodeBase {
{
code: 'EVAL',
message: `Use of eval is strongly discouraged, as it poses security risks and may cause issues with minification`,
url: 'https://rollupjs.org/guide/en/#avoiding-eval'
url: 'https://rollupjs.org/guide/en/#avoiding-eval',
},
this.start
);
Expand All @@ -52,7 +52,7 @@ export default class TaggedTemplateExpression extends NodeBase {
initialise() {
this.callOptions = {
args: NO_ARGS,
withNew: false
withNew: false,
};
}
}
27 changes: 10 additions & 17 deletions test/function/samples/cannot-call-external-namespace/_config.js
@@ -1,21 +1,14 @@
const path = require('path');
const assert = require('assert');

module.exports = {
description: 'errors if code calls an external namespace',
error: {
code: 'CANNOT_CALL_NAMESPACE',
message: `Cannot call a namespace ('foo')`,
pos: 28,
watchFiles: [path.resolve(__dirname, 'main.js')],
loc: {
file: path.resolve(__dirname, 'main.js'),
line: 2,
column: 0
},
frame: `
1: import * as foo from 'foo';
2: foo();
^
`
description: 'warns if code calls an external namespace',
options: {
external: ['fs']
},
warnings(warnings) {
assert.deepStrictEqual(warnings.map(String), [
"main.js (4:1) Cannot call a namespace ('foo')",
"main.js (8:1) Cannot call a namespace ('foo')"
]);
}
};
11 changes: 9 additions & 2 deletions test/function/samples/cannot-call-external-namespace/main.js
@@ -1,2 +1,9 @@
import * as foo from 'foo';
foo();
import * as foo from 'fs';

try {
foo();
} catch (e) {}

try {
foo``;
} catch (e) {}
24 changes: 7 additions & 17 deletions test/function/samples/cannot-call-internal-namespace/_config.js
@@ -1,21 +1,11 @@
const path = require('path');
const assert = require('assert');

module.exports = {
description: 'errors if code calls an internal namespace',
error: {
code: 'CANNOT_CALL_NAMESPACE',
message: `Cannot call a namespace ('foo')`,
pos: 33,
watchFiles: [path.resolve(__dirname, 'main.js'), path.resolve(__dirname, 'foo.js')],
loc: {
file: path.resolve(__dirname, 'main.js'),
line: 2,
column: 0
},
frame: `
1: import * as foo from './foo.js';
2: foo();
^
`
description: 'warns if code calls an internal namespace',
warnings(warnings) {
assert.deepStrictEqual(warnings.map(String), [
"main.js (4:1) Cannot call a namespace ('foo')",
"main.js (8:1) Cannot call a namespace ('foo')"
]);
}
};
9 changes: 8 additions & 1 deletion test/function/samples/cannot-call-internal-namespace/main.js
@@ -1,2 +1,9 @@
import * as foo from './foo.js';
foo();

try {
foo();
} catch {}

try {
foo``;
} catch {}

0 comments on commit c69c41b

Please sign in to comment.