immosquare-cleaner 1.3.0 → 1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7caffdb6ce093eae93ca5f22d1e25676d935bbd8f034805307d53bd20e69c201
4
- data.tar.gz: 3fa0fc3dc8f247f866f89108a13752aee5761b9e0fcb1f62071bb2de3eb35add
3
+ metadata.gz: 06af1c0a31ef811c54efdd4d3f845317c81f02db83112947da04390fd5c69e6c
4
+ data.tar.gz: d3c8617e585affa872699e5378ac6c90818d1fe0d6a3fe6f1bbcf62d727c7451
5
5
  SHA512:
6
- metadata.gz: e94943fb8aedca8ff9d71bd0dcc4a9a4360392806b814b8e72dbed789d37b3bdfe0952c95bf7c19bc3fc26038920de831577d44894f97fa032a5f9e2f6c8bb7c
7
- data.tar.gz: 1d27e92164cf050d08421c100af8d181a696ba8b3c0c25648d669ef269cff5b2b724fccf4fe8d40da5785949aa7fc99640dec8797e788640fcf37eda9cadf346
6
+ metadata.gz: 354c8df2a5913b65613909c4d000161aafb2fcb7ab66b09e1dd909f0df6d6b74c982f44a65c7f10bba1525ceb1a42203f1aa8df37b70eaf2a8bc327efd7b4808
7
+ data.tar.gz: b0df5b076458f940f997f92bd76643fa472d850c480f88cc9508eccf1fb7ab0cfd8b99429e80ce0d18c9bc85976c52423ae955e48983cfc7d58725d82fc5f641
@@ -1,3 +1,3 @@
1
1
  module ImmosquareCleaner
2
- VERSION = "1.3.0".freeze
2
+ VERSION = "1.3.1".freeze
3
3
  end
@@ -38,8 +38,6 @@
38
38
  // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
39
39
  // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
40
40
  //============================================================//
41
- const hasRequire = /require\(/
42
- const spaceMatcher = /(\s*)((?:\|\||&&|\?\?|\+|-|\*|\/|%|&|\^|\||<<|>>|\*\*|>>>)?=)/
43
41
 
44
42
  const assignmentOperators = ["=", "+=", "-=", "*=", "/=", "%=", "&=", "^=", "|=", "<<=", ">>=", "**=", ">>>=", "||=", "&&=", "??="]
45
43
 
@@ -100,35 +98,31 @@ const rule = {
100
98
  previousNode = groupNode
101
99
  }
102
100
 
103
- const getPrefix = (node) => {
104
- const nodeBefore = isAssignmentExpression(node) ? node.left : node.declarations.find((dcl) => dcl.type === "VariableDeclarator").id
105
- return nodeBefore.loc.end.column - nodeBefore.loc.start.column
101
+ const assignmentTokenOf = (node) => {
102
+ const left = isAssignmentExpression(node) ? node.left : node.declarations[0]?.id
103
+ if (!left) return null
104
+ const token = sourceCode.getTokenAfter(left)
105
+ return token && assignmentOperators.includes(token.value) ? token : null
106
106
  }
107
107
 
108
- const findAssigment = (node) => {
109
- const prefix = getPrefix(node)
110
- const source = sourceCode.getText(node)
111
- const match = source.slice(prefix).match(spaceMatcher)
112
- return match ? match.index + prefix + match[2].length : null
108
+ const findAssignment = (node) => {
109
+ const token = assignmentTokenOf(node)
110
+ return token ? token.loc.end.column - sourceCode.getFirstToken(node).loc.start.column - 1 : null
113
111
  }
114
112
 
115
113
  const assignmentOnFirstLine = (node) => {
116
- if (isAssignmentExpression(node)) return node.left.loc.start.line === node.right.loc.start.line
117
-
118
- const source = sourceCode.getText(node)
119
- const lines = source.split("\n")
120
- return lines[0].includes("=")
114
+ const token = assignmentTokenOf(node)
115
+ return token && !isMultiline(sourceCode.getFirstToken(node), token)
121
116
  }
122
117
 
123
118
  const areAligned = (maxPos, nodes) => nodes
124
119
  .filter(assignmentOnFirstLine)
125
- .map((node) => sourceCode.getText(node))
126
- .every((source) => source.charAt(maxPos) === "=")
120
+ .every((node) => findAssignment(node) === maxPos)
127
121
 
128
122
  const getMaxPos = (nodes) => nodes
129
123
  .filter(assignmentOnFirstLine)
130
- .map(findAssigment)
131
- .reduce((last, current) => Math.max(last, current), [])
124
+ .map(findAssignment)
125
+ .reduce((last, current) => Math.max(last, current), 0)
132
126
 
133
127
  const check = (group) => {
134
128
  const maxPos = getMaxPos(group)
@@ -141,27 +135,21 @@ const rule = {
141
135
  end: getLast(group).loc.end
142
136
  },
143
137
  message: "This group of assignments is not aligned",
144
- fix: (fixer) => group.map((node) => {
145
- const tokens = sourceCode.getTokens(node)
146
- const firstToken = tokens[0]
147
- const assignmentToken = tokens.find((token) => assignmentOperators.includes(token.value))
148
- const line = sourceCode.getText(node)
149
- const lineIsAligned = line.charAt(maxPos) === "="
150
-
151
- if (lineIsAligned || !assignmentToken || isMultiline(firstToken, assignmentToken)) return fixer.replaceText(node, line)
138
+ fix: (fixer) => group.flatMap((node) => {
139
+ const firstToken = sourceCode.getFirstToken(node)
140
+ const assignmentToken = assignmentTokenOf(node)
141
+ if (!assignmentToken || isMultiline(firstToken, assignmentToken) || findAssignment(node) === maxPos) return []
142
+ const previousToken = sourceCode.getTokenBefore(assignmentToken)
143
+ const gap = [previousToken.range[1], assignmentToken.range[0]]
144
+ if (sourceCode.commentsExistBetween(previousToken, assignmentToken)) return []
152
145
 
153
146
  //============================================================//
154
- // Source line may include spaces, we need to accomodate
155
- // for that.
147
+ // Only the spacing before the operator belongs to this rule. Replacing a declaration
148
+ // can cut a template processor's placeholder at an AST boundary inside that marker.
156
149
  //============================================================//
157
- const spacePrefix = firstToken.loc.start.column
158
- const startDelimiter = assignmentToken.loc.start.column - spacePrefix
159
- const endDelimiter = assignmentToken.loc.end.column - spacePrefix
160
- const start = line.slice(0, startDelimiter).replace(/\s+$/m, "")
161
- const ending = line.slice(endDelimiter).replace(/^\s+/m, "")
162
- const spacesRequired = maxPos - start.length - assignmentToken.value.length + 1
163
- const spaces = " ".repeat(spacesRequired)
164
- return fixer.replaceText(node, `${start}${spaces}${assignmentToken.value} ${ending}`)
150
+ const prefixWidth = previousToken.loc.end.column - firstToken.loc.start.column
151
+ const spacesRequired = maxPos - prefixWidth - assignmentToken.value.length + 1
152
+ return [fixer.replaceTextRange(gap, " ".repeat(spacesRequired))]
165
153
  })
166
154
  })
167
155
  }
@@ -169,7 +157,7 @@ const rule = {
169
157
  return {
170
158
  VariableDeclaration: (node) => {
171
159
  const source = sourceCode.getText(node)
172
- if (requiresOnly && !hasRequire.test(source)) return
160
+ if (requiresOnly && !source.includes("require(")) return
173
161
 
174
162
  addNode(node, node)
175
163
  },
data/package.json CHANGED
@@ -7,7 +7,7 @@
7
7
  ],
8
8
  "private": true,
9
9
  "dependencies": {
10
- "@babel/parser": "^8.0.7",
10
+ "@babel/parser": "^8.0.8",
11
11
  "@eslint/js": "^10.0.1",
12
12
  "@typescript-eslint/eslint-plugin": "^8.72.0",
13
13
  "@typescript-eslint/parser": "^8.72.0",
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: immosquare-cleaner
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - immosquare