diff --git a/lib/standard-clojure-style.js b/lib/standard-clojure-style.js index d64cb80..bc19cae 100644 --- a/lib/standard-clojure-style.js +++ b/lib/standard-clojure-style.js @@ -720,6 +720,10 @@ return node.name === 'token' && node.text === 'ns' } + function isUseNode (node) { + return node && isString(node.text) && (node.text === ':use' || node.text === 'use') + } + function isRequireNode (node) { return node && isString(node.text) && (node.text === ':require' || node.text === 'require') } @@ -2425,6 +2429,7 @@ result.genClass.prefix.value = strConcat3('"', getTextFromStringNode(node), '"') genClassToggle = 0 genClassValueLineNo = lineNo + // other :gen-class values } else if (insideGenClass && idx > genClassNodeIdx && isTextNode && isTokenNode2 && genClassToggle === 1) { // :name, :extends, :init, :post-init, :factory, :state, :impl-ns @@ -2447,6 +2452,10 @@ } } // FIXME: we need to handle :implements, :constructors, :methods, :exposes, :exposes-methods, here + + // throw an error if we encounter :use + } else if (insideNsForm && isTokenNode2 && parenNestingDepth >= 1 && isUseNode(node)) { + throw new Error('Standard Clojure Style does not support :use inside of the ns form. Please refactor with :require as appropriate.') } // increment the lineNo for the next node if we are on a newline node @@ -3902,7 +3911,7 @@ out: inputTxt } } else { - // parse the ns data structure from the nodes + // parse the ns data structure from the nodes let parsedNs = null try { parsedNs = parseNs(nodesArr) diff --git a/test/parse_ns.test.js b/test/parse_ns.test.js index 68d964a..5f15dc0 100644 --- a/test/parse_ns.test.js +++ b/test/parse_ns.test.js @@ -80,22 +80,27 @@ allTestCases.forEach(testCase => { expectedObj = JSON.parse(testCase.expectedTxt) } catch (e) {} - // expectedTxt should be valid JSON + // the Expected text should be valid JSON expect(expectedObj).not.toBeNull() const inputNodes = scsLib.parse(testCase.input) const flatNodes = scsLib._flattenTree(inputNodes) - const nsParsed1 = scsLib._parseNs(flatNodes) + try { + const nsParsed1 = scsLib._parseNs(flatNodes) - const nsParsed2 = immutable.fromJS(nsParsed1) - const nsExpected = immutable.fromJS(expectedObj) - const resultIsTheSame = immutable.is(nsParsed2, nsExpected) + const nsParsed2 = immutable.fromJS(nsParsed1) + const nsExpected = immutable.fromJS(expectedObj) + const resultIsTheSame = immutable.is(nsParsed2, nsExpected) - if (!resultIsTheSame) { - console.log('ns parsed:', JSON.stringify(nsParsed1, null, 2)) - } + if (!resultIsTheSame) { + console.log('ns parsed:', JSON.stringify(nsParsed1, null, 2)) + } - expect(resultIsTheSame).toBe(true) + expect(resultIsTheSame).toBe(true) + } catch (e) { + expect(expectedObj.parsingShouldError).toBe(true) + expect(e.message).toEqual(expectedObj.errorMessage) + } }) } }) diff --git a/test_format/ns.eno b/test_format/ns.eno index 53b9e47..9607a92 100644 --- a/test_format/ns.eno +++ b/test_format/ns.eno @@ -1903,3 +1903,32 @@ (:require [other :refer :all :rename {foo bar}])) --Expected + +# GitHub Issue #152 - :use outside of the ns form should work normally + +--Input +(ns com.example.my-app) + + (use 'com.example.foo) +--Input + +--Expected +(ns com.example.my-app) + +(use 'com.example.foo) +--Expected + +# GitHub Issue #152 - use symbol inside the ns form is ok + +--Input +(ns com.example.my-app + (:require + [com.example.my-app.util :refer [use abc] + ])) +--Input + +--Expected +(ns com.example.my-app + (:require + [com.example.my-app.util :refer [abc use]])) +--Expected diff --git a/test_parse_ns/parse_ns.eno b/test_parse_ns/parse_ns.eno index e9ee977..4850ecd 100644 --- a/test_parse_ns/parse_ns.eno +++ b/test_parse_ns/parse_ns.eno @@ -2970,3 +2970,53 @@ ] } --Expected + +# GitHub Issue #152 - :use in the ns form should error + +> https://github.com/oakmac/standard-clojure-style-js/issues/152 + +--Input +(ns com.example.my-app + (:use com.example-my-app.util)) +--Input + +--Expected +{ + "parsingShouldError": true, + "errorMessage": "Standard Clojure Style does not support :use inside of the ns form. Please refactor with :require as appropriate." +} +--Expected + +# GitHub Issue #152 - :use outside of the ns form should work normally + +--Input +(ns com.example.my-app) + + (use 'com.example.foo) +--Input + +--Expected +{ + "nsSymbol": "com.example.my-app" +} +--Expected + +# GitHub Issue #152 - use symbol inside the ns form is ok + +--Input +(ns com.example.my-app + (:require + [com.example.my-app.util :refer [use]])) +--Input + +--Expected +{ + "nsSymbol": "com.example.my-app", + "requires": [ + { + "symbol": "com.example.my-app.util", + "refer": [{"symbol": "use"}] + } + ] +} +--Expected