Skip to content

Commit

Permalink
GitHub Issue #152 - error on :use inside of the ns form (#171)
Browse files Browse the repository at this point in the history
  • Loading branch information
oakmac authored Dec 28, 2024
1 parent 41b5791 commit aef21c7
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 10 deletions.
11 changes: 10 additions & 1 deletion lib/standard-clojure-style.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
23 changes: 14 additions & 9 deletions test/parse_ns.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
})
Expand Down
29 changes: 29 additions & 0 deletions test_format/ns.eno
Original file line number Diff line number Diff line change
Expand Up @@ -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
50 changes: 50 additions & 0 deletions test_parse_ns/parse_ns.eno
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit aef21c7

Please sign in to comment.