From fc629beb4d1336a0fb6b2ed61e831ad0eb4040fd Mon Sep 17 00:00:00 2001 From: Natalie Weizenbaum Date: Wed, 4 Sep 2024 18:05:40 -0700 Subject: [PATCH] Add common Sass TypeScript configuration to the analysis package --- .github/dependabot.yml | 5 ++- analysis/.eslintrc | 23 ++++++++++ analysis/.prettierrc.js | 3 ++ analysis/README.md | 58 ++++++++++++++++++++++++-- analysis/index.js | 6 +++ analysis/package.json | 10 +++++ analysis/pubspec.yaml | 2 +- analysis/tsconfig.json | 3 ++ pkg/sass-parser/.eslintrc | 13 +----- pkg/sass-parser/.prettierrc.js | 2 +- pkg/sass-parser/lib/src/stringifier.ts | 7 ++-- pkg/sass-parser/package.json | 2 + pkg/sass-parser/tsconfig.json | 2 +- 13 files changed, 112 insertions(+), 24 deletions(-) create mode 100644 analysis/.eslintrc create mode 100644 analysis/.prettierrc.js create mode 100644 analysis/index.js create mode 100644 analysis/package.json create mode 100644 analysis/tsconfig.json diff --git a/.github/dependabot.yml b/.github/dependabot.yml index a3137eb60..fc4f94ce6 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -1,7 +1,9 @@ version: 2 updates: - package-ecosystem: "pub" - directory: "/" + directories: + - "/" + - "/analysis" schedule: interval: "weekly" - package-ecosystem: "github-actions" @@ -13,6 +15,7 @@ updates: - package-ecosystem: "npm" directories: - "/" + - "/analysis" - "/package" - "/pkg/sass-parser" ignore: diff --git a/analysis/.eslintrc b/analysis/.eslintrc new file mode 100644 index 000000000..cdcb3da2e --- /dev/null +++ b/analysis/.eslintrc @@ -0,0 +1,23 @@ +{ + "extends": "./node_modules/gts/", + "plugins": ["import"], + "rules": { + "@typescript-eslint/explicit-function-return-type": [ + "error", + {"allowExpressions": true} + ], + "func-style": ["error", "declaration"], + "prefer-const": ["error", {"destructuring": "all"}], + // It would be nice to sort import declaration order as well, but that's not + // autofixable and it's not worth the effort of handling manually. + "sort-imports": ["error", {"ignoreDeclarationSort": true}], + }, + "overrides": [ + { + "files": ["**/*.ts"], + "rules": { + "import/no-commonjs": "error" + } + } + ] +} diff --git a/analysis/.prettierrc.js b/analysis/.prettierrc.js new file mode 100644 index 000000000..c5166c2ae --- /dev/null +++ b/analysis/.prettierrc.js @@ -0,0 +1,3 @@ +module.exports = { + ...require('gts/.prettierrc.json'), +}; diff --git a/analysis/README.md b/analysis/README.md index 87427c39e..123305e47 100644 --- a/analysis/README.md +++ b/analysis/README.md @@ -1,11 +1,14 @@ -This package provides a shared set of analysis options for use by Sass team -packages. To use it, add it as a Git dependency to your `pubspec.yaml`: +This package provides a shared static analysis configuration for use by Sass +team Dart and TypeScript packages. + +## Use from Dart + +Add this as a Git dependency to your `pubspec.yaml`: ```yaml dev_dependencies: sass_analysis: - git: {url: - https://github.com/sass/dart-sass.git, path: analysis} + git: {url: https://github.com/sass/dart-sass.git, path: analysis} ``` and include it in your `analysis_options.yaml`: @@ -13,3 +16,50 @@ and include it in your `analysis_options.yaml`: ```yaml include: package:sass_analysis/analysis_options.yaml ``` + +## Use from TypeScript + +Add this and [gts] as Git dependencies to your `package.json`, then initialize +gts: + +[gts]: https://github.com/google/gts + +```sh +$ npm i --save-dev gts 'https://gitpkg.vercel.app/sass/dart-sass/analysis?main' +$ npx gts init +``` + +Then edit the configuration files to use Sass-specific customizations instead of +the gts defaults: + +* `eslintrc.json`: + + ```json + { + "extends": "./node_modules/sass-analysis/" + } + ``` + +* `.prettierrc.js`: + + ```json + module.exports = { + ...require(sass-analysis/.prettierrc.js') + } + ``` + +* `tsconfig.json`: + + ```json + { + "extends": "./node_modules/sass-analysis/tsconfig.json", + "compilerOptions": { + "rootDir": ".", + "outDir": "build" + }, + "include": [ + "src/**/*.ts", + "test/**/*.ts" + ] + } + ``` diff --git a/analysis/index.js b/analysis/index.js new file mode 100644 index 000000000..48081e16b --- /dev/null +++ b/analysis/index.js @@ -0,0 +1,6 @@ +const fs = require('node:fs'); +const jsonc = require('jsonc-parser'); + +module.exports = jsonc.parse( + fs.readFileSync(require.resolve('./.eslintrc'), 'utf8') +); diff --git a/analysis/package.json b/analysis/package.json new file mode 100644 index 000000000..33e2fa7bf --- /dev/null +++ b/analysis/package.json @@ -0,0 +1,10 @@ +{ + "name": "sass-analysis", + "version": "0.0.0", + "description": "Shared static analysis options for Sass team packages.", + "dependencies": { + "eslint-plugin-import": "^2.30.0", + "gts": "^5.0.0", + "jsonc-parser": "^3.3.1" + } +} diff --git a/analysis/pubspec.yaml b/analysis/pubspec.yaml index cbf04dedc..a7c203489 100644 --- a/analysis/pubspec.yaml +++ b/analysis/pubspec.yaml @@ -1,7 +1,7 @@ name: sass_analysis version: 0.0.0 description: Shared analysis options for Sass team packages. -homepage: https://github.com/sass/dart-sass/tree/master/analysis +homepage: https://github.com/sass/dart-sass/tree/main/analysis publish_to: none diff --git a/analysis/tsconfig.json b/analysis/tsconfig.json new file mode 100644 index 000000000..badc28135 --- /dev/null +++ b/analysis/tsconfig.json @@ -0,0 +1,3 @@ +{ + "extends": "./node_modules/gts/tsconfig-google.json" +} diff --git a/pkg/sass-parser/.eslintrc b/pkg/sass-parser/.eslintrc index 3b5b91232..13fed1733 100644 --- a/pkg/sass-parser/.eslintrc +++ b/pkg/sass-parser/.eslintrc @@ -1,14 +1,3 @@ { - "extends": "./node_modules/gts/", - "rules": { - "@typescript-eslint/explicit-function-return-type": [ - "error", - {"allowExpressions": true} - ], - "func-style": ["error", "declaration"], - "prefer-const": ["error", {"destructuring": "all"}], - // It would be nice to sort import declaration order as well, but that's not - // autofixable and it's not worth the effort of handling manually. - "sort-imports": ["error", {"ignoreDeclarationSort": true}], - } + "extends": "./node_modules/sass-analysis/" } diff --git a/pkg/sass-parser/.prettierrc.js b/pkg/sass-parser/.prettierrc.js index c5166c2ae..dc8356cf2 100644 --- a/pkg/sass-parser/.prettierrc.js +++ b/pkg/sass-parser/.prettierrc.js @@ -1,3 +1,3 @@ module.exports = { - ...require('gts/.prettierrc.json'), + ...require('sass-analysis/.prettierrc.js'), }; diff --git a/pkg/sass-parser/lib/src/stringifier.ts b/pkg/sass-parser/lib/src/stringifier.ts index c732b8dde..e3f93db68 100644 --- a/pkg/sass-parser/lib/src/stringifier.ts +++ b/pkg/sass-parser/lib/src/stringifier.ts @@ -27,13 +27,12 @@ // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. import * as postcss from 'postcss'; +import PostCssStringifier from 'postcss/lib/stringifier'; import {AnyStatement} from './statement'; import {GenericAtRule} from './statement/generic-at-rule'; import {Rule} from './statement/rule'; -const PostCssStringifier = require('postcss/lib/stringifier'); - /** * A visitor that stringifies Sass statements. * @@ -67,7 +66,7 @@ export class Stringifier extends PostCssStringifier { )(statement, semicolon); } - private atrule(node: GenericAtRule, semicolon: boolean): void { + atrule(node: GenericAtRule, semicolon: boolean): void { const start = `@${node.nameInterpolation}` + (node.raws.afterName ?? (node.paramsInterpolation ? ' ' : '')) + @@ -82,7 +81,7 @@ export class Stringifier extends PostCssStringifier { } } - private rule(node: Rule): void { + rule(node: Rule): void { this.block(node, node.selectorInterpolation.toString()); } } diff --git a/pkg/sass-parser/package.json b/pkg/sass-parser/package.json index 4f2536035..ae68ddbb4 100644 --- a/pkg/sass-parser/package.json +++ b/pkg/sass-parser/package.json @@ -37,12 +37,14 @@ "devDependencies": { "@types/jest": "^29.5.12", "copyfiles": "^2.4.1", + "eslint-plugin-import": "^2.30.0", "expect": "^29.7.0", "gts": "^5.0.0", "jest": "^29.4.1", "jest-extended": "^4.0.2", "npm-run-all": "^4.1.5", "rimraf": "^6.0.1", + "sass-analysis": "file:../../analysis", "ts-jest": "^29.0.5", "ts-node": "^10.2.1", "typedoc": "^0.26.5", diff --git a/pkg/sass-parser/tsconfig.json b/pkg/sass-parser/tsconfig.json index f0cf2e4c8..ab8cf0c67 100644 --- a/pkg/sass-parser/tsconfig.json +++ b/pkg/sass-parser/tsconfig.json @@ -1,5 +1,5 @@ { - "extends": "./node_modules/gts/tsconfig-google.json", + "extends": "./node_modules/sass-analysis/tsconfig.json", "compilerOptions": { "lib": ["es2022"], "allowJs": true,