@virmator/lint 14.12.2 → 14.13.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.
@@ -9,6 +9,7 @@ import globals from 'globals';
9
9
  import tsEslint from 'typescript-eslint';
10
10
  import preferIfElseChainRule from '../src/rules/prefer-if-else-chain.lint.js';
11
11
  import preferParseUrlRule from '../src/rules/prefer-parse-url.lint.js';
12
+ import preferSingleQuotesRule from '../src/rules/prefer-single-quotes.lint.js';
12
13
 
13
14
  export const globalVars = {
14
15
  ...globals.node,
@@ -72,6 +73,7 @@ export function defineEslintConfig(repoDir: string) {
72
73
  rules: {
73
74
  'prefer-if-else-chain': preferIfElseChainRule,
74
75
  'prefer-parse-url': preferParseUrlRule,
76
+ 'prefer-single-quotes': preferSingleQuotesRule,
75
77
  },
76
78
  },
77
79
  },
@@ -174,6 +176,7 @@ export function defineEslintConfig(repoDir: string) {
174
176
  '@typescript-eslint/no-unused-vars': 'error',
175
177
  '@virmator/prefer-if-else-chain': 'error',
176
178
  '@virmator/prefer-parse-url': 'error',
179
+ '@virmator/prefer-single-quotes': 'error',
177
180
  'no-lonely-if': 'error',
178
181
  curly: 'error',
179
182
  'object-shorthand': 'error',
@@ -0,0 +1,3 @@
1
+ import { type Rule } from 'eslint';
2
+ declare const rule: Rule.RuleModule;
3
+ export default rule;
@@ -0,0 +1,56 @@
1
+ function templateRawToSingleQuoted(raw) {
2
+ const escaped = raw.replaceAll(/\\`|\\\$\{|\\.|'/gs, (match) => {
3
+ if (match === '\\`') {
4
+ return '`';
5
+ }
6
+ else if (match === '\\${') {
7
+ return '${';
8
+ }
9
+ else if (match === "'") {
10
+ return String.raw `\'`;
11
+ }
12
+ return match;
13
+ });
14
+ return "'" + escaped + "'";
15
+ }
16
+ const rule = {
17
+ meta: {
18
+ type: 'suggestion',
19
+ fixable: 'code',
20
+ messages: {
21
+ preferSingleQuotes: 'Use single-quoted strings instead of template literals when there is no interpolation or newlines.',
22
+ },
23
+ },
24
+ create(context) {
25
+ const sourceCode = context.sourceCode;
26
+ return {
27
+ TemplateLiteral(node) {
28
+ if (node.expressions.length > 0 ||
29
+ node.parent.type === 'TaggedTemplateExpression') {
30
+ return;
31
+ }
32
+ const quasi = node.quasis[0];
33
+ if (!quasi) {
34
+ return;
35
+ }
36
+ const raw = quasi.value.raw;
37
+ if (raw.includes('\n') || raw.includes('\r')) {
38
+ return;
39
+ }
40
+ const cooked = quasi.value.cooked;
41
+ if (cooked != undefined && cooked.includes("'") && cooked.includes('"')) {
42
+ return;
43
+ }
44
+ context.report({
45
+ node,
46
+ messageId: 'preferSingleQuotes',
47
+ fix(fixer) {
48
+ const nodeRange = sourceCode.getRange(node);
49
+ return fixer.replaceTextRange(nodeRange, templateRawToSingleQuoted(raw));
50
+ },
51
+ });
52
+ },
53
+ };
54
+ },
55
+ };
56
+ export default rule;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@virmator/lint",
3
- "version": "14.12.2",
3
+ "version": "14.13.1",
4
4
  "description": "Default ESLint linting plugin for virmator.",
5
5
  "keywords": [
6
6
  "virmator",
@@ -32,31 +32,31 @@
32
32
  "test:update": "npm test update"
33
33
  },
34
34
  "dependencies": {
35
- "@augment-vir/assert": "^31.68.3",
36
- "@augment-vir/common": "^31.68.3",
37
- "@augment-vir/node": "^31.68.3",
38
- "@virmator/core": "^14.12.2",
35
+ "@augment-vir/assert": "^31.69.0",
36
+ "@augment-vir/common": "^31.69.0",
37
+ "@augment-vir/node": "^31.69.0",
38
+ "@virmator/core": "^14.13.1",
39
39
  "jiti": "^2.6.1",
40
40
  "mri": "^1.2.0"
41
41
  },
42
42
  "devDependencies": {
43
- "@augment-vir/test": "^31.68.3",
43
+ "@augment-vir/test": "^31.69.0",
44
44
  "@eslint/eslintrc": "^3.3.5",
45
45
  "@eslint/js": "9.39.2",
46
46
  "@stylistic/eslint-plugin": "^5.10.0",
47
47
  "@stylistic/eslint-plugin-ts": "^4.4.1",
48
48
  "@types/estree": "^1.0.8",
49
- "@types/node": "^25.5.2",
49
+ "@types/node": "^25.6.0",
50
50
  "@typescript-eslint/eslint-plugin": "^8.58.0",
51
51
  "c8": "^11.0.0",
52
52
  "eslint": "^9.39.2",
53
53
  "eslint-config-prettier": "^10.1.8",
54
54
  "eslint-plugin-jsdoc": "^62.9.0",
55
55
  "eslint-plugin-monorepo-cop": "^1.0.2",
56
- "eslint-plugin-playwright": "^2.10.1",
56
+ "eslint-plugin-playwright": "^2.10.2",
57
57
  "eslint-plugin-prettier": "^5.5.5",
58
58
  "eslint-plugin-require-extensions": "^0.1.3",
59
- "eslint-plugin-sonarjs": "^4.0.2",
59
+ "eslint-plugin-sonarjs": "^4.0.3",
60
60
  "eslint-plugin-unicorn": "^64.0.0",
61
61
  "istanbul-smart-text-reporter": "^1.1.5",
62
62
  "typescript-eslint": "8.57.1"
@@ -0,0 +1,67 @@
1
+ import {type Rule} from 'eslint';
2
+ import type {TemplateLiteral} from 'estree';
3
+
4
+ function templateRawToSingleQuoted(raw: string): string {
5
+ const escaped = raw.replaceAll(/\\`|\\\$\{|\\.|'/gs, (match) => {
6
+ if (match === '\\`') {
7
+ return '`';
8
+ } else if (match === '\\${') {
9
+ return '${';
10
+ } else if (match === "'") {
11
+ return String.raw`\'`;
12
+ }
13
+ return match;
14
+ });
15
+ return "'" + escaped + "'";
16
+ }
17
+
18
+ const rule: Rule.RuleModule = {
19
+ meta: {
20
+ type: 'suggestion',
21
+ fixable: 'code',
22
+ messages: {
23
+ preferSingleQuotes:
24
+ 'Use single-quoted strings instead of template literals when there is no interpolation or newlines.',
25
+ },
26
+ },
27
+ create(context) {
28
+ const sourceCode = context.sourceCode;
29
+
30
+ return {
31
+ TemplateLiteral(node: TemplateLiteral & Rule.NodeParentExtension) {
32
+ if (
33
+ node.expressions.length > 0 ||
34
+ node.parent.type === 'TaggedTemplateExpression'
35
+ ) {
36
+ return;
37
+ }
38
+
39
+ const quasi = node.quasis[0];
40
+ if (!quasi) {
41
+ return;
42
+ }
43
+
44
+ const raw = quasi.value.raw;
45
+ if (raw.includes('\n') || raw.includes('\r')) {
46
+ return;
47
+ }
48
+
49
+ const cooked = quasi.value.cooked;
50
+ if (cooked != undefined && cooked.includes("'") && cooked.includes('"')) {
51
+ return;
52
+ }
53
+
54
+ context.report({
55
+ node,
56
+ messageId: 'preferSingleQuotes',
57
+ fix(fixer) {
58
+ const nodeRange = sourceCode.getRange(node);
59
+ return fixer.replaceTextRange(nodeRange, templateRawToSingleQuoted(raw));
60
+ },
61
+ });
62
+ },
63
+ };
64
+ },
65
+ };
66
+
67
+ export default rule;