@compiled/eslint-plugin
Advanced tools
Comparing version
import { RuleTester } from 'eslint'; | ||
export declare const tester: RuleTester; | ||
export declare const typeScriptTester: RuleTester; | ||
export declare const createAliasedInvalidTestCase: (test: RuleTester.InvalidTestCase, replaceCode: (code: string) => string, replaceOutput: (output: string) => string) => RuleTester.InvalidTestCase; | ||
export declare const createDeclarationInvalidTestCases: (test: RuleTester.InvalidTestCase, name: string, replaceCode: (code: string, prefix: string) => string, replaceOutput: (output: string, prefix: string) => string) => RuleTester.InvalidTestCase[]; | ||
export declare const createTypedInvalidTestCase: (test: RuleTester.InvalidTestCase, replaceCode: (code: string) => string, replaceOutput: (output: string) => string) => RuleTester.InvalidTestCase; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createDeclarationInvalidTestCases = exports.createAliasedInvalidTestCase = exports.tester = void 0; | ||
exports.createTypedInvalidTestCase = exports.createDeclarationInvalidTestCases = exports.createAliasedInvalidTestCase = exports.typeScriptTester = exports.tester = void 0; | ||
const path_1 = require("path"); | ||
@@ -19,3 +19,3 @@ const eslint_1 = require("eslint"); | ||
}; | ||
exports.tester = new eslint_1.RuleTester({ | ||
const baseTesterConfig = { | ||
parser: require.resolve('babel-eslint'), | ||
@@ -26,3 +26,5 @@ parserOptions: { | ||
}, | ||
}); | ||
}; | ||
exports.tester = new eslint_1.RuleTester(baseTesterConfig); | ||
exports.typeScriptTester = new eslint_1.RuleTester(Object.assign(Object.assign({}, baseTesterConfig), { parser: require.resolve('@typescript-eslint/parser') })); | ||
const createAliasedInvalidTestCase = (test, replaceCode, replaceOutput) => (Object.assign(Object.assign({}, test), { filename: `aliased-${(0, path_1.basename)(test.filename)}.ts`, code: replaceCode(test.code), output: replaceOutput(test.output) })); | ||
@@ -42,2 +44,4 @@ exports.createAliasedInvalidTestCase = createAliasedInvalidTestCase; | ||
exports.createDeclarationInvalidTestCases = createDeclarationInvalidTestCases; | ||
const createTypedInvalidTestCase = (test, replaceCode, replaceOutput) => (Object.assign(Object.assign({}, test), { filename: `typed-${(0, path_1.basename)(test.filename)}.ts`, code: replaceCode(test.code), output: replaceOutput(test.output) })); | ||
exports.createTypedInvalidTestCase = createTypedInvalidTestCase; | ||
//# sourceMappingURL=test-utils.js.map |
@@ -17,3 +17,3 @@ "use strict"; | ||
*fix(fixer) { | ||
const { quasi, tag } = node; | ||
const { quasi } = node; | ||
const source = context.getSourceCode(); | ||
@@ -28,5 +28,17 @@ // TODO Eventually handle comments instead of skipping them | ||
} | ||
yield fixer.insertTextBefore(node, source.getText(tag) + | ||
const oldCode = source.getText(node); | ||
// Remove quasi: | ||
// styled.div<Props>` | ||
// color: red; | ||
// ` | ||
// becomes | ||
// styled.div<Props> | ||
const withoutQuasi = oldCode.replace(source.getText(quasi), ''); | ||
const newCode = withoutQuasi + | ||
// Indent the arguments after the tagged template expression range | ||
(0, generate_1.generate)((0, to_arguments_1.toArguments)(source, quasi), (0, get_tagged_template_expression_offset_1.getTaggedTemplateExpressionOffset)(node))); | ||
(0, generate_1.generate)((0, to_arguments_1.toArguments)(source, quasi), (0, get_tagged_template_expression_offset_1.getTaggedTemplateExpressionOffset)(node)); | ||
if (oldCode === newCode) { | ||
return; | ||
} | ||
yield fixer.insertTextBefore(node, newCode); | ||
yield fixer.remove(node); | ||
@@ -33,0 +45,0 @@ }, |
{ | ||
"name": "@compiled/eslint-plugin", | ||
"version": "0.4.2", | ||
"version": "0.4.3", | ||
"description": "A familiar and performant compile time CSS-in-JS library for React.", | ||
@@ -23,2 +23,3 @@ "homepage": "https://compiledcssinjs.com/docs/pkg-eslint-plugin", | ||
"devDependencies": { | ||
"@typescript-eslint/parser": "^5.43.0", | ||
"babel-eslint": "^10.1.0", | ||
@@ -25,0 +26,0 @@ "eslint": "^7.32.0", |
@@ -8,3 +8,4 @@ import { basename } from 'path'; | ||
createDeclarationInvalidTestCases, | ||
tester, | ||
createTypedInvalidTestCase, | ||
typeScriptTester as tester, | ||
} from '../../../test-utils'; | ||
@@ -35,2 +36,4 @@ import { noStyledTaggedTemplateExpressionRule } from '../index'; | ||
const replaceType = (str: string) => str.replace('styled.div', 'styled.div<{color: string}>'); | ||
const createInvalidTestCases = (tests: InvalidTestCase[]) => | ||
@@ -47,3 +50,4 @@ tests | ||
.flatMap((t) => [t, createComposedComponentTestCase(t)]) | ||
.flatMap((t) => [t, createAliasedInvalidTestCase(t, replaceAlias, replaceAlias)]); | ||
.flatMap((t) => [t, createAliasedInvalidTestCase(t, replaceAlias, replaceAlias)]) | ||
.flatMap((t) => [t, createTypedInvalidTestCase(t, replaceType, replaceType)]); | ||
@@ -50,0 +54,0 @@ tester.run('no-styled-tagged-template-expression', noStyledTaggedTemplateExpressionRule, { |
@@ -20,3 +20,3 @@ import { basename } from 'path'; | ||
export const tester = new RuleTester({ | ||
const baseTesterConfig = { | ||
parser: require.resolve('babel-eslint'), | ||
@@ -27,2 +27,9 @@ parserOptions: { | ||
}, | ||
}; | ||
export const tester = new RuleTester(baseTesterConfig); | ||
export const typeScriptTester = new RuleTester({ | ||
...baseTesterConfig, | ||
parser: require.resolve('@typescript-eslint/parser'), | ||
}); | ||
@@ -73,1 +80,12 @@ | ||
}; | ||
export const createTypedInvalidTestCase = ( | ||
test: RuleTester.InvalidTestCase, | ||
replaceCode: (code: string) => string, | ||
replaceOutput: (output: string) => string | ||
): RuleTester.InvalidTestCase => ({ | ||
...test, | ||
filename: `typed-${basename(test.filename!)}.ts`, | ||
code: replaceCode(test.code), | ||
output: replaceOutput(test.output!), | ||
}); |
@@ -28,3 +28,3 @@ import type { Rule, Scope } from 'eslint'; | ||
*fix(fixer: RuleFixer) { | ||
const { quasi, tag } = node; | ||
const { quasi } = node; | ||
const source = context.getSourceCode(); | ||
@@ -43,8 +43,20 @@ | ||
yield fixer.insertTextBefore( | ||
node, | ||
source.getText(tag) + | ||
// Indent the arguments after the tagged template expression range | ||
generate(toArguments(source, quasi), getTaggedTemplateExpressionOffset(node)) | ||
); | ||
const oldCode = source.getText(node); | ||
// Remove quasi: | ||
// styled.div<Props>` | ||
// color: red; | ||
// ` | ||
// becomes | ||
// styled.div<Props> | ||
const withoutQuasi = oldCode.replace(source.getText(quasi), ''); | ||
const newCode = | ||
withoutQuasi + | ||
// Indent the arguments after the tagged template expression range | ||
generate(toArguments(source, quasi), getTaggedTemplateExpressionOffset(node)); | ||
if (oldCode === newCode) { | ||
return; | ||
} | ||
yield fixer.insertTextBefore(node, newCode); | ||
yield fixer.remove(node); | ||
@@ -51,0 +63,0 @@ }, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
219391
1.43%5499
0.84%5
25%