From c69c41be862d3413af2f600eea8985292d64f37f Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Tue, 31 Mar 2020 14:09:07 -0600 Subject: [PATCH] feat: Call namespace error as a warning (#3475) * feat: call namespace as a warning * Fix prettier config * Add tagged template literals to test to improve coverage Co-authored-by: Lukas Taegert-Atkinson --- .prettierrc | 6 +++-- src/ast/nodes/CallExpression.ts | 12 ++++----- src/ast/nodes/TaggedTemplateExpression.ts | 8 +++--- .../cannot-call-external-namespace/_config.js | 27 +++++++------------ .../cannot-call-external-namespace/main.js | 11 ++++++-- .../cannot-call-internal-namespace/_config.js | 24 +++++------------ .../cannot-call-internal-namespace/main.js | 9 ++++++- 7 files changed, 48 insertions(+), 49 deletions(-) diff --git a/.prettierrc b/.prettierrc index 2a1740fd7ae..ea138e8454c 100644 --- a/.prettierrc +++ b/.prettierrc @@ -1,5 +1,7 @@ { + "arrowParens": "avoid", + "printWidth": 100, "singleQuote": true, - "useTabs": true, - "printWidth": 100 + "trailingComma": "none", + "useTabs": true } diff --git a/src/ast/nodes/CallExpression.ts b/src/ast/nodes/CallExpression.ts index 90ee05daa53..bb336ce4339 100644 --- a/src/ast/nodes/CallExpression.ts +++ b/src/ast/nodes/CallExpression.ts @@ -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'; @@ -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'; @@ -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 ); @@ -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 ); @@ -212,7 +212,7 @@ export default class CallExpression extends NodeBase implements DeoptimizableEnt initialise() { this.callOptions = { args: this.arguments, - withNew: false + withNew: false, }; } diff --git a/src/ast/nodes/TaggedTemplateExpression.ts b/src/ast/nodes/TaggedTemplateExpression.ts index daadda5e85e..84e9dffc3fe 100644 --- a/src/ast/nodes/TaggedTemplateExpression.ts +++ b/src/ast/nodes/TaggedTemplateExpression.ts @@ -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 ); @@ -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 ); @@ -52,7 +52,7 @@ export default class TaggedTemplateExpression extends NodeBase { initialise() { this.callOptions = { args: NO_ARGS, - withNew: false + withNew: false, }; } } diff --git a/test/function/samples/cannot-call-external-namespace/_config.js b/test/function/samples/cannot-call-external-namespace/_config.js index 1dd8124c147..20b6a35ada5 100644 --- a/test/function/samples/cannot-call-external-namespace/_config.js +++ b/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')" + ]); } }; diff --git a/test/function/samples/cannot-call-external-namespace/main.js b/test/function/samples/cannot-call-external-namespace/main.js index c1ad7c6f778..3c5998d3518 100644 --- a/test/function/samples/cannot-call-external-namespace/main.js +++ b/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) {} diff --git a/test/function/samples/cannot-call-internal-namespace/_config.js b/test/function/samples/cannot-call-internal-namespace/_config.js index a75a74e7f5d..b61157e6158 100644 --- a/test/function/samples/cannot-call-internal-namespace/_config.js +++ b/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')" + ]); } }; diff --git a/test/function/samples/cannot-call-internal-namespace/main.js b/test/function/samples/cannot-call-internal-namespace/main.js index 74f0acb2482..dcb5f372c44 100644 --- a/test/function/samples/cannot-call-internal-namespace/main.js +++ b/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 {}